QT 控件和ID绑定 QObjectUserData

来源:互联网 发布:槟榔淘宝店铺怎么装修 编辑:程序博客网 时间:2024/05/16 11:17
#ifndef CDATABIND_H
#define CDATABIND_H


#include <QObjectUserData>


class CDatabind : public QObjectUserData
{
public:
    CDatabind();


    void SetUserData(int ROw, int col);


public:
    int m_nRow;
    int m_nCol;






};


#endif // CDATABIND_H


#include "cdatabind.h"


CDatabind::CDatabind()
{
    m_nRow = -1;
    m_nCol = -1;
}

void CDatabind::SetUserData(int row, int col)
{
    m_nRow = row;
    m_nCol = col;
}



使用

 QPushButton *open = new QPushButton("打开");
        CDatabind *bindopen = new CDatabind();
        bindopen->SetUserData(i, 4);
        open->setUserData(0, bindopen);
        ui->tableWidget->setCellWidget(i, 4, open);
        connect(open, SIGNAL(clicked()), this, SLOT(OnBtnOpendoor()));










        QPushButton *lock = new QPushButton("锁定");
        CDatabind *bindlock = new CDatabind();
        bindlock->SetUserData(i, 5);
        lock->setUserData(0, bindlock);
        ui->tableWidget->setCellWidget(i, 5, lock);
        connect(lock, SIGNAL(clicked()), this, SLOT(OnBtnLockdoor()));





void CDoorctl::OnBtnOpendoor()
{
    QPushButton *btn = (QPushButton *)sender();


    CDatabind *open = (CDatabind *)btn->userData(0);


    int row = open->m_nRow;
    int col = open->m_nCol;
    int m=0;
}




void CDoorctl::OnBtnLockdoor()
{
    QPushButton *btn = (QPushButton *)sender();


    CDatabind *open = (CDatabind *)btn->userData(0);


    int row = open->m_nRow;
    int col = open->m_nCol;
    int m=0;
}






0 0
原创粉丝点击