Qt - QTableView - Clickable button in table row

蹲街弑〆低调 提交于 2019-11-29 05:44:05

问题


I require a button/link within a table row of a QTableView. This is to open a dialog to allow that row to be edited more efficiently.

After hours of looking on the web I am yet to find a decent example.

I am aware that this is likely to be done using a QItemDelegate, but I am unsure how to have a functional widget within the row without forcing the item into edit mode first.

Any help would be greatly appreciated.


回答1:


You could emulate the functionality of a link by underlining the clickable text, then capturing the cell click via the cellClicked(row, col) signal and check that col == editColumn. Then row would correspond to which item you are editing.

For example,

Data Name | Value 1 | Value 2 | Edit

connect (tableWidget, SIGNAL(cellClicked(int,int)), this, SLOT(editSlot(int, int)));

...

void ClassName::editSlot(int row, int col){
  if (col == 3) {
    doWork(row);
  }
}



回答2:


You can use setIndexWidget for that, see the Qt documentation for more information.

As an example, to embed a push button in the first column of the second row (untested code):

tableView->setIndexWidget(tableView->model()->index(2, 1), new QPushButton);


来源:https://stackoverflow.com/questions/4412796/qt-qtableview-clickable-button-in-table-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!