gpt4 book ai didi

Qt - QTableView 中的 SelectedItems

转载 作者:行者123 更新时间:2023-12-04 13:10:59 27 4
gpt4 key购买 nike

我正在尝试从 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);
}
}

谢谢

最佳答案

QAbstractItemView(QTableView 的基类)为此提供了一个QItemSelectionModel。您可以通过 QTableView::itemSelectionModel() 访问该模型然后通过 QItemSelectionModel::selectedRows() 检索选定的行:

QModelIndexList selectedRows = yourTableView->selectionModel()->selectedRows();

foreach( QModelIndex index, selectedRows )
{
int row = index.row();
}

关于Qt - QTableView 中的 SelectedItems,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13724520/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com