Unsolved HELP! QStandardItemModel::findItems(keyword, Qt::MatchContains | Qt::MatchRecursive, column /*= 2*/ )
-
In QTreeView, I cannot use QStandardItemModel::findItems() when search column =2 to search the match items which at column 2.
-
Hi,
The view should have nothing to do with findItems. Can you show us an example of what you want to happen and what happens instead? -
@VRonin
thank you very much!
I finished it at last by record all the QStandardItem * into a map as below :
so...now , I can only search the _allItems (which type is: map<QString, QStandardItem *>).
since QStandardItemModel::findItems () can only search column=0 recursively.
-
@VRonin
see this link:
http://www.qtcentre.org/threads/39658-Searching-a-hierarchical-QStandardItemModel?highlight=finditemsthe Qt version is 5.5, as the findItems cannot search column>0 even with Qt::MathRecursive.
-
@Navca-Lin
Qt::MatchRecursive,
not Qt::MathRecursive. -
Hi,
Can you provide a code example that shows how you build and search your model ? I currently don't see any reason why you can't search in any other column than 0, that's just the default value used if you don't pass that parameter.
-
Ok I think I got the problem. All the views in Qt only handle children of items in the first columns while
QAbstractItemModel::match
it will only search children of the index incolumn
which triggers the clash ifcolumn>0
. I'd go as far as to say it's an oversight in the code so feel free to post a bug report.In the meantime, you can just manually search the model, no need to store the items in a separate map:
QList<QStandardItem*> findItemsRecurse( QStandardItemModel* model, const QString &text, int column=0, cont QModelIndex& parent = QModelIndex()){ QList<QStandardItem*> result; for(int i=0, maxRow=model->rowCount(parent);i<maxRow;++i){ QModelIndex currIdx = model->index(i,column,parent); if(currIdx->data().toString().contains(text)) result << itemFromIndex(currIdx); for(int j=0, maxCol = model->columnCount(parent);j<maxCol;++j){ currIdx = model->index(i,j,parent); if(model->hasChildren(currIdx)) result.append(findItemsRecurse(model,text,column,currIdx)); } } return result; }
-
@VRonin
thank you very very very very much~
I got your code~~ -
@SGaist
thank you~it's like this: