如何在tablewidget添加按钮?

时间:2023-01-11 19:32:08
1 定义一个按钮

QPushButton * pBtn = new QPushButton();

2 链接信号与曹

connect(pBtn, SIGNAL(clicked()), this, SLOT(OnBtnClicked()));

3 按钮添加到单元格内

table->setCellWidget(0,0,pBtn); //如果点击按钮出现崩溃现象,就添加QTableWidgetItem  到按钮的那个单元格

4 实现按钮的事件

void myPic::OnBtnClicked(void)
{
QPushButton * senderObj=qobject_cast<QPushButton *>(sender());
if(senderObj == 0)
{
return;
}
QModelIndex index =table->indexAt(QPoint(senderObj->frameGeometry().x(),senderObj->frameGeometry().y()));
int row=index.row();
qDebug()<<"row:"<<row;
}