Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. HELP! QStandardItemModel::findItems(keyword, Qt::MatchContains | Qt::MatchRecursive, column /*= 2*/ )
Forum Updated to NodeBB v4.3 + New Features

HELP! QStandardItemModel::findItems(keyword, Qt::MatchContains | Qt::MatchRecursive, column /*= 2*/ )

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Navca LinN Offline
    Navca LinN Offline
    Navca Lin
    wrote on last edited by
    #1

    In QTreeView, I cannot use QStandardItemModel::findItems() when search column =2 to search the match items which at column 2.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #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?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      Navca LinN 2 Replies Last reply
      2
      • VRoninV VRonin

        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?

        Navca LinN Offline
        Navca LinN Offline
        Navca Lin
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • VRoninV VRonin

          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?

          Navca LinN Offline
          Navca LinN Offline
          Navca Lin
          wrote on last edited by
          #4

          @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 LinN 1 Reply Last reply
          0
          • Navca LinN Navca Lin

            @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 LinN Offline
            Navca LinN Offline
            Navca Lin
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              Navca LinN 1 Reply Last reply
              1
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #7

                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;
                }
                

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                Navca LinN 1 Reply Last reply
                1
                • VRoninV VRonin

                  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;
                  }
                  
                  Navca LinN Offline
                  Navca LinN Offline
                  Navca Lin
                  wrote on last edited by
                  #8

                  @VRonin
                  thank you very very very very much~
                  I got your code~~

                  1 Reply Last reply
                  0
                  • SGaistS SGaist

                    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.

                    Navca LinN Offline
                    Navca LinN Offline
                    Navca Lin
                    wrote on last edited by
                    #9

                    @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

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved