问题: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