我在从 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/