Qt - QTableView - 表行中的可单击按钮

发布于 2024-10-07 01:42:35 字数 186 浏览 0 评论 0原文

我需要 QTableView 的表格行内有一个按钮/链接。这是为了打开一个对话框,以便更有效地编辑该行。

在网上浏览了几个小时后,我还没有找到一个合适的例子。

我知道这可能是使用 QItemDelegate 来完成的,但我不确定如何在行中拥有一个功能性小部件而不首先强制项目进入编辑模式。

任何帮助将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

¢蛋碎的人ぎ生 2024-10-14 01:42:35

您可以使用 setIndexWidget 来实现此目的,请参阅Qt 文档< /a> 了解更多信息。

例如,要在第二行的第一列中嵌入一个按钮(未经测试的代码):

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

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);
悲歌长辞 2024-10-14 01:42:35

您可以通过在可点击文本下划线来模拟链接的功能,然后通过 cellClicked(row, col) 信号捕获单元格点击并检查 col == editColumn。然后行将对应于您正在编辑的项目。

例如,

数据名称 |值 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);
  }
}

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);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文