天天看點

如何在tablewidget添加按鈕?

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;
}