我正在尝试从QTableView小部件中返回所选行的向量(代码片段如下),但返回的值与选择不对应,我认为我没有正确理解QModelIndexList / QModelIndex与QTableView的关系。您能告诉我哪里出错了或者从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);
}
}
谢谢
foreach(QModelIndex index, indexes) { int row = index.row(); int column = index.column(); for(int j = 0; j < c_model->columnCount(); j++) { QVariant q = c_model->data(c_model->index(row,j+column)); printf(" %s\t ", q.toString().toLocal8Bit().constData()); } printf("\n"); }
- rahulv_selectedItems.clear(); foreach(QModelIndex index, indexes) { std::vector<std::string> l_row; for(int l = 0; l < c_model->columnCount(); l++) l_row.push_back(index.sibling(index.row(),l).data().toString().toLocal8Bit().constData()); v_selectedItems.push_back(l_row);
- rahul