我试图在给定字符串值的情况下在 Qt 中选择抽象项目视图的项目。我已经编写了QModelIndex
根据字符串内容查找任何内容的函数。
我现在正试图将QModelIndex
我找到的所有这些 es 放入单个选择中。我的方法签名:
// Will select all items that contain any of the strings
// given by 1st argument
virtual void selectItems(const QStringList&) override;
我的实现看起来像这样(但不能正常工作):
void QAbstractItemViewResult::selectItems(const QStringList& list)
{
if(list.size() > 0) {
QItemSelectionModel::SelectionFlags flags = QItemSelectionModel::ClearAndSelect;
QItemSelection selection;
Q_FOREACH(const QString text, list) {
// Find index by string is a method I implemented earlier
// The method works correctly
QModelIndex index(findIndexByString(targetView_, list[0]));
if(index.isValid()) {
// This is an attempt to add model indx into selection
selection.select(index, index);
}
}
// When the selection is created, this should select it in index
targetView_->selectionModel()->select(selection, flags);
}
}
问题是,这段代码总是只选择列表中的第一项,例如。因为"B1","C1","A1"
它看起来像这样:
该表启用了多选:
那么如何以编程方式正确选择多个项目?如果您需要findIndexByString
,可以在这里找到:https ://github.com/Darker/qt-gui-test/blob/master/results/QAbstractItemViewResult.cpp#L5