問題:QTableWidget,最后一列為刪除按鈕,點擊時刪除當前行。
1 設置自定義的唯一標記
QTableWidgetItem *item6 = new QTableWidgetItem();
item6->setData(Qt::UserRole + 2, yieldCurveList.at(i)->getCode());
2 在代理構造中設置QSignalMapper信號信號
signalMapper = new QSignalMapper(this);
connect(signalMapper, SIGNAL(mapped(QString)), this, SIGNAL(editorRowSignal(QString)));
在代理的createEidtor中設置按鈕和信號
QPushButton *editor = new QPushButton(tr("刪除"), parent);
connect(editor, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(editor, index.data(Qt::UserRole + 2).toString());
return editor;
3 刪除處理
for (int i = 0; i < ui->tbwCurveList->rowCount(); i++)

{
if (ui->tbwCurveList->item(i, HDelete)->data(Qt::UserRole + 2) == row)
ui->tbwCurveList->removeRow(i);
}
4 待優化:將其設置為model,通過index在model中刪除,submitAll。
其他相關資料
http://digikam.sourcearchive.com/lines/2:0.10.0-2ubuntu1/setupcollectionview_8cpp-source.html http://www.qtforum.org/article/24195/qpushbutton-as-delegate-in-a-qtableview.html http://kunalmaemo.blogspot.com/2010/12/creating-custom-qitemdelegate-with.html