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 :
    0_1533185159593_02b08c35-56a7-4408-881b-f75abe7c1781-image.png

    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=finditems

    the Qt version is 5.5, as the findItems cannot search column>0 even with Qt::MathRecursive.



  • @Navca-Lin
    Qt::MatchRecursive,
    not Qt::MathRecursive.


  • Lifetime Qt Champion

    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 in column which triggers the clash if column>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:
    0_1533207316269_f3f7a8cd-85d6-444b-acd9-440b465e6802-image.png

    0_1533207378467_00f731ce-c232-473e-9881-2906d9aa0dd1-image.png

    0_1533207407124_0fd2287d-008d-4f74-a27a-a4b6abb1fbea-image.png

    0_1533207517015_76713547-fa50-4591-8d4e-f734c8804de8-image.png


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.