我正在尝试从QTableView小部件(下面复制的代码段)返回所选行的向量,但返回的值与选择不对应,我相信我不了解QTableView的QModelIndexList/QModelIndex.你能让我知道我错在哪里或从QTableView访问所选项目的正确方法吗?C_model的类型为QStandardItemModel
for(int i = 0; i < c_model->rowCount(); i++)
{
if (selectionModel->isRowSelected(i, QModelIndex()))
{
QStringList selection;
std::vector<std::string> currentIndexValues;
for (int j = 0; j < c_model->columnCount(); j++)
{
QVariant q = c_model->data(c_model->index(i, j));
selection.append(q.toString());
currentIndexValues.push_back(q.toString().toLocal8Bit().constData());
printf(" %s\t ", currentIndexValues[j].c_str());
}
printf("\n");
v_selectedItems.push_back(currentIndexValues);
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢
QAbstractItemView(基类QTableView)提供了一个QItemSelectionModel为此目的.您可以通过QTableView :: itemSelectionModel()访问该模型 ,然后通过QItemSelectionModel :: selectedRows()检索所选行:
QModelIndexList selectedRows = yourTableView->selectionModel()->selectedRows();
foreach( QModelIndex index, selectedRows )
{
int row = index.row();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2994 次 |
| 最近记录: |