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就是对应的行和列,对应到相应的按钮 }