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. [SOLVED] QPersistentModelIndex usage
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QPersistentModelIndex usage

Scheduled Pinned Locked Moved General and Desktop
24 Posts 2 Posters 15.8k 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.
  • K Offline
    K Offline
    Kobid
    wrote on 8 Nov 2013, 19:54 last edited by
    #21

    Maybe should I somehow refresh all persistent indexes after sorting?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      Kobid
      wrote on 8 Nov 2013, 20:26 last edited by
      #22

      I did some dirty test. Instead of qSort(), I'm moving new added item manually to destination row by:
      @beginMoveRows(parent->index, 4, 4, parent->index, 0);
      parent->childItems.move(4,0); // this is QList
      endMoveRows();@
      ... and now all persistent indexes are updated automatically. So I suppose that I need to do something with persistent indexes after qSort() call. Or just call modelreset (I don't know which items changed after sort anyway) and create new persistent indexes for all items. changePersistentIndex doesn't change anything
      And another question: where QPersistentModelIndex should be created? Now I'm creating it from "outside" model class (after childItems.append()) but maybe should I do this in QAbstractItemModel.createIndex() ?

      1 Reply Last reply
      0
      • R Offline
        R Offline
        raven-worx
        Moderators
        wrote on 8 Nov 2013, 22:26 last edited by
        #23

        [quote author="Kobid" date="1383942370"]
        And another question: where QPersistentModelIndex should be created? Now I'm creating it from "outside" model class (after childItems.append()) but maybe should I do this in QAbstractItemModel.createIndex() ?[/quote]
        Since QpersistenModelIndex are a bit of an overhead i would only create them when needed. Thus outside of the model is fine, thats what they are actually for. THe modle should only create QModelIndex objects.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • K Offline
          K Offline
          Kobid
          wrote on 9 Nov 2013, 16:00 last edited by
          #24

          Problem solved! I looked what QStandardItemModel do in sort method and I used similar algorithm. So here is complete solution if someone have same problem (note that my childs are based on QVectior, if you are using QList then you need to modify it a little):
          @void HCDirModel::sortChilds(HCDirItem *p)
          {
          if (p->childCount()==0) return;

          QVector<QPair<HCDirItem*, int> > l(p->childItems.count());
          for (int i=0; i < p->childItems.count(); ++i) {
              l[i] = QPair<HCDirItem*,int>(p->childItems[i], i);
          }
          qStableSort(l.begin(), l.end(), compareFilenameA);
          
          QModelIndexList mFrom, mTo;
          for (int i = 0; i < l.count(); ++i) {
              p->childItems[i] = l[i].first;
              QModelIndex from = createIndex(l[i].second, 0, l[i].first);
              if (persistentIndexList().contains(from)) {
                  QModelIndex to = createIndex(i, 0, l[i].first);
                  mFrom.append(from);
                  mTo.append(to);
              }
          }
          
          if (!mFrom.empty())
              changePersistentIndexList(mFrom,mTo);
          

          }@

          And usage:
          @emit layoutAboutToBeChanged();
          sortChilds(it);
          emit layoutChanged();@

          1 Reply Last reply
          1

          21/24

          8 Nov 2013, 19:54

          • Login

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