C++ Qt - QTableView - 表格行中的可点击按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4412796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Qt - QTableView - Clickable button in table row
提问by cweston
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.
我需要一个表格行中的按钮/链接QTableView
。这是为了打开一个对话框以允许更有效地编辑该行。
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.
我知道这很可能是使用 a 来完成的QItemDelegate
,但我不确定如何在不强制项目进入编辑模式的情况下在行中拥有一个功能小部件。
Any help would be greatly appreciated.
任何帮助将不胜感激。
采纳答案by GatorGuy
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.
您可以通过在可点击文本下划线来模拟链接的功能,然后通过 cellClicked(row, col) 信号捕获单元格点击并检查 col == editColumn。然后行将对应于您正在编辑的项目。
For example,
例如,
Data Name | Value 1 | Value 2 | Edit
数据名称 | 值 1 | 值 2 | 编辑
connect (tableWidget, SIGNAL(cellClicked(int,int)), this, SLOT(editSlot(int, int)));
...
void ClassName::editSlot(int row, int col){
if (col == 3) {
doWork(row);
}
}
回答by Ton van den Heuvel
You can use setIndexWidget
for that, see the Qt documentationfor more information.
您可以使用setIndexWidget
它,有关更多信息,请参阅Qt 文档。
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);