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. How to use QStandardItemModel + QStandardItem sort two types data independent
Forum Updated to NodeBB v4.3 + New Features

How to use QStandardItemModel + QStandardItem sort two types data independent

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 717 Views
  • 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.
  • A Offline
    A Offline
    Aizzzz
    wrote on last edited by Aizzzz
    #1

    Hi,I'm trying to write my own TreeWidget for inherite QStandardItemModel, and it will show data just list file and folder. I want to sort folder and file independently, just like:

    raw data: 
    @root
    |-- D file
    |-- C folder
    |-- B folder
    `-- A file
    

    after sort, i want to it look like

    raw data: 
    @root
    |-- A file
    |-- D file
    |-- B folder
    `-- C folder
    

    I try to write my own sort function refer to QTreeWidget::sortItems, but it not works when i call it

    class MyOwnTreeModel : public QStandardItemModel
    {    
        void sortChilds(QStandardItem*pItems, Qt::SortOrder order);
    }
    
    void MyOwnTreeModel::sortChilds(QStandardItem*pItem, Qt::SortOrder order)
    {
        QVector<QPair<QStandardItem*, int> > sorting;
    
        for (int i = 0; i < pItem->rowCount(); i++)
        {
            /* get one type item, eg. all file or all folder */
        }
    
    /*  do sorting*/
    
        QModelIndexList mFrom, mTo;
        for (int r = 0; r < sorting.count(); ++r)
        {
            int oldRow = sorting.at(r).second;
            if (oldRow == r)
                continue;
            QModelIndex from = createIndex(sorting[r].second, 0, sorting.at(r).first);
    
         /* it seems setChild not work */
            pItem->setChild(sorting[r].second, 0, sorting.at(r).first);
    
            if (persistentIndexList().contains(from))
            {
                QModelIndex to = createIndex(r, 0, sorting[r].first);
                mFrom.append(from);
                mTo.append(to);
            }
        }
        if (!mFrom.empty())
            changePersistentIndexList(mFrom, mTo);
    
    }
    

    when i call sortChilds in my own tree widget, just like ownModel->sortChilds(item), there is nothing happened. mTo and From shows the right order, it seems QStandardItem::setChild not work but i don't know why.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      is A file 2 columns or 1?

      "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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Aizzzz
        wrote on last edited by Aizzzz
        #3

        Just 1 column, file and folder just let's know there are two types, and identified by userRole

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          EDIT: wrong code, see below

          void MyOwnTreeModel::sortChilds(QStandardItem*pItem, Qt::SortOrder order)
          {
          if(!pItem) return;
          const int oldSortRole = sortRole();
          setSortRole(Qt::UserRole);
          pItem->sortChildren(0,order);
          setSortRole(Qt::DisplayRole);
          pItem->sortChildren(0,order);
          setSortRole(oldSortRole);
          }
          

          "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

          JonBJ 1 Reply Last reply
          0
          • VRoninV VRonin

            EDIT: wrong code, see below

            void MyOwnTreeModel::sortChilds(QStandardItem*pItem, Qt::SortOrder order)
            {
            if(!pItem) return;
            const int oldSortRole = sortRole();
            setSortRole(Qt::UserRole);
            pItem->sortChildren(0,order);
            setSortRole(Qt::DisplayRole);
            pItem->sortChildren(0,order);
            setSortRole(oldSortRole);
            }
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @VRonin
            Hmm. Cheaty!

            Does your code sort by UserRole and then by DisplayRole, is that why you write what you have? If so where does docs say that will work??

            VRoninV 1 Reply Last reply
            0
            • JonBJ JonB

              @VRonin
              Hmm. Cheaty!

              Does your code sort by UserRole and then by DisplayRole, is that why you write what you have? If so where does docs say that will work??

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              @JonB said in How to use QStandardItemModel + QStandardItem sort two types data independent:

              Cheaty!

              Indeed. The correct way would be to subclass QStandardItem and reimplement operator< but "aint nobody got time for that"

              "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

              JonBJ 1 Reply Last reply
              0
              • VRoninV VRonin

                @JonB said in How to use QStandardItemModel + QStandardItem sort two types data independent:

                Cheaty!

                Indeed. The correct way would be to subclass QStandardItem and reimplement operator< but "aint nobody got time for that"

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @VRonin said in How to use QStandardItemModel + QStandardItem sort two types data independent:

                The correct way would be to subclass QStandardItem and reimplement operator<

                Which is what I would have done...

                If yours works, where does sortChildren() say anything about being a "stable" re-sort?? I don't get how/why it would work, if I understand you correctly....

                VRoninV 1 Reply Last reply
                0
                • JonBJ JonB

                  @VRonin said in How to use QStandardItemModel + QStandardItem sort two types data independent:

                  The correct way would be to subclass QStandardItem and reimplement operator<

                  Which is what I would have done...

                  If yours works, where does sortChildren() say anything about being a "stable" re-sort?? I don't get how/why it would work, if I understand you correctly....

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  @JonB said in How to use QStandardItemModel + QStandardItem sort two types data independent:

                  where does sortChildren() say anything about being a "stable" re-sort??

                  It doesn't say it but that's what the code does: https://code.woboq.org/qt5/qtbase/src/gui/itemmodels/qstandarditemmodel.cpp.html#_ZN20QStandardItemPrivate12sortChildrenEiN2Qt9SortOrderE (uses std::stable_sort)

                  "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

                  JonBJ 1 Reply Last reply
                  1
                  • VRoninV VRonin

                    @JonB said in How to use QStandardItemModel + QStandardItem sort two types data independent:

                    where does sortChildren() say anything about being a "stable" re-sort??

                    It doesn't say it but that's what the code does: https://code.woboq.org/qt5/qtbase/src/gui/itemmodels/qstandarditemmodel.cpp.html#_ZN20QStandardItemPrivate12sortChildrenEiN2Qt9SortOrderE (uses std::stable_sort)

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @VRonin
                    LOL, internals... :)

                    But I still don't get it. Sorting by UserRole first is to get directories separated from files, right (or wrong)? But then don't they both have a DisplayRole value for their name? In which case the second sort would not have any "stables" to retain from the first one, every item has its own name for DisplayRole, no?

                    1 Reply Last reply
                    2
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #10

                      That's correct, I inverted the roles order, good catch!

                      void MyOwnTreeModel::sortChilds(QStandardItem*pItem, Qt::SortOrder order)
                      {
                      if(!pItem) return;
                      const int oldSortRole = sortRole();
                      setSortRole(Qt::DisplayRole);
                      pItem->sortChildren(0,order);
                      setSortRole(Qt::UserRole);
                      pItem->sortChildren(0,order);
                      setSortRole(oldSortRole);
                      }
                      

                      "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

                      JonBJ 1 Reply Last reply
                      2
                      • VRoninV VRonin

                        That's correct, I inverted the roles order, good catch!

                        void MyOwnTreeModel::sortChilds(QStandardItem*pItem, Qt::SortOrder order)
                        {
                        if(!pItem) return;
                        const int oldSortRole = sortRole();
                        setSortRole(Qt::DisplayRole);
                        pItem->sortChildren(0,order);
                        setSortRole(Qt::UserRole);
                        pItem->sortChildren(0,order);
                        setSortRole(oldSortRole);
                        }
                        
                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #11

                        @VRonin
                        Ahhh, now that would make sense, with a stable second sort! Get them all alphabetical first, then separate as-is out into a directory versus file group.

                        It's reassuring to learn that somebody else but me does not actually test the code they propose... ;-)

                        1 Reply Last reply
                        1
                        • A Offline
                          A Offline
                          Aizzzz
                          wrote on last edited by
                          #12

                          @VRonin @JonB It works well, great thanks! 😀

                          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