QTableWidget中添加按鈕


添加按鈕

void QTableWidget::setCellWidget ( int row, int column, QWidget * widget )

widget可以是自己定義的按鈕

class MyPushButton : public QPushButton
{
    Q_OBJECT
public:
    explicit MyPushButton(int i, int j, int flag);
    ~MyPushButton();
    void mySetText();//i對應端口信息
}
mySetText()函數中:

setStyleSheet("background-color: rgb(255, 100, 0);");//橙色 setToolTip(tr("端口未授權"));//配置的MAC地址和采集卡不符合 setText(tr("端口%1").arg(m_no + 1));

按鈕添加完之后怎么去觸發它的點擊信號呢?嘗試了很多中方法,比如直接調用  QTableWidget的itemclicked函數,都失敗了,

最后從Stackoverflow上找到了類似的方法,將comboBox的觸發方法改成pushButton就好了

QSignalMapper* signalMapper = new QSignalMapper(this);

connect(newBtn, SIGNAL(clicked()), signalMapper, SLOT(map()));

//存儲按鈕
signalMapper->setMapping(newBtn, QString("%1-%2").arg(rowCount).arg(i + 2));

connect(signalMapper, SIGNAL(mapped(const QString &)),this, SLOT(changed(const QString &)));


//槽函數
void MainWindow::changed(QString position)
{
    QStringList coordinates = position.split("-");
    int row = coordinates[0].toInt();
    int col = coordinates[1].toInt();
    disPlayPort(row,col);//row 和col就是對應的行和列,對應到相應的按鈕
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM