c++ - QTableWidget 中的 SelectedRows 列表

标签 c++ qt

我在从 QTableWidget 中获取选定的行时遇到问题。我有这样的表:

[id]  [ key ]
 0     test
 1     pass

我想获取每一行的键值。我试过 QTableWidget->selectedIndexes();但它说它受到保护,我无法访问它。当我尝试 QTableWidget->SelectionModel->selectedIndexes 时,我不知道如何遍历列表并获取键值。有谁知道更好的方法我该怎么做? 问候。

最佳答案

我假设您将表格小部件的选择行为设置为选择

您始终可以访问任何项目 View /小部件的所谓“选择模型”。 QTableWidget 继承自 QAbstractItemView,它给你 access to this special model .这个模型可以告诉你selected rows作为 QModelIndex 的列表,然后可以告诉您 the row .获得它们后,您可以访问 table content ,在您的例子中,索引为 1 的列中的文本(键列)。

static const KEY_COLUMN = 1;

QList<QString> selectedKeys;
QItemSelectionModel *selectionModel = ui->tableWidget->selectionModel();
foreach(QModelIndex index, selectionModel->selectedRows())
    selectedIDs << ui->tableWidget->item(index->row(), KEY_COLUMN)->text();

关于c++ - QTableWidget 中的 SelectedRows 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14114061/

相关文章:

c++ - 使用单个索引访问子矩阵的有效方法

c++ - 如何使用Boost解析ini文件

C++删除动态分配内存的问题

c++ - 如何将标题标题设置为包装在 QTableWidget 中

c++ - 我的程序意外结束

c++ - 限制 qpainter 在像素图上的绘制

c++ - 如何将 boost 数组表示为类型的指针?

c++ - 改变 QMenu 中子菜单的位置

c++ - 在 Qt 中获取系统用户名

c++ - Qt 如何管理另一个窗口?