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. QSortFilterProxy & QIdentityProxyModel & QAbstractItemModel = Crash :- )
Forum Update on Monday, May 27th 2025

QSortFilterProxy & QIdentityProxyModel & QAbstractItemModel = Crash :- )

Scheduled Pinned Locked Moved Solved General and Desktop
qabstractitemmoqsortfilterproxqidentityproxymdrag and dropdrag&drop
26 Posts 5 Posters 4.3k 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.
  • C Offline
    C Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on 2 Jan 2020, 19:05 last edited by
    #2

    What is testModel ? a QAbstractItemModel? If so you have to call begin/endInsertRow on that model, not on something other.

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    D 1 Reply Last reply 2 Jan 2020, 19:09
    0
    • C Christian Ehrlicher
      2 Jan 2020, 19:05

      What is testModel ? a QAbstractItemModel? If so you have to call begin/endInsertRow on that model, not on something other.

      D Offline
      D Offline
      Dariusz
      wrote on 2 Jan 2020, 19:09 last edited by
      #3

      @Christian-Ehrlicher Yes testModel = QAbstractItemModel, everything happens on model level, nothing get happen on proxy/filter level.

      The list of items are list of items not modelIndexes so index() would return index of the base abstract model and not filter/other one...

      My head is going to explode, I cant see the error in this :- (( the simple append to group drag action that I perform is as simple as it gets. add/remove parents all it does. What do I dooo :- (

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 2 Jan 2020, 19:11 last edited by
        #4

        @Dariusz said in QSortFilterProxy & QIdentityProxyModel & QAbstractItemModel = Crash :- ):

        Yes testModel = QAbstractItemModel,

        So why do you call mGenericModel->beginInsertRows() then? You don't insert rows in mGenericModel at all...

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        D 1 Reply Last reply 2 Jan 2020, 19:14
        0
        • C Christian Ehrlicher
          2 Jan 2020, 19:11

          @Dariusz said in QSortFilterProxy & QIdentityProxyModel & QAbstractItemModel = Crash :- ):

          Yes testModel = QAbstractItemModel,

          So why do you call mGenericModel->beginInsertRows() then? You don't insert rows in mGenericModel at all...

          D Offline
          D Offline
          Dariusz
          wrote on 2 Jan 2020, 19:14 last edited by Dariusz 1 Feb 2020, 19:25
          #5

          @Christian-Ehrlicher said in QSortFilterProxy & QIdentityProxyModel & QAbstractItemModel = Crash :- ):

          @Dariusz said in QSortFilterProxy & QIdentityProxyModel & QAbstractItemModel = Crash :- ):

          Yes testModel = QAbstractItemModel,

          So why do you call mGenericModel->beginInsertRows() then? You don't insert rows in mGenericModel at all...

          eeee

          class myCustomItems{
              testModel *mGenericModel
          public:
                myCustomItems(const QString &displayString);
               ~myCustomItems();
               inline void setModel(testModel*modelPtr){mGenericModel=modelPtr};
          }
           
          

          I'm calling model signal from item to trigger the function thini... Or what should I do ? o.O

          edit Fudge I messed up class name... sigh. corrected 1st post. My bad :- ( was trying to simplify the code from clutter

          D 1 Reply Last reply 3 Jan 2020, 00:16
          0
          • D Dariusz
            2 Jan 2020, 19:14

            @Christian-Ehrlicher said in QSortFilterProxy & QIdentityProxyModel & QAbstractItemModel = Crash :- ):

            @Dariusz said in QSortFilterProxy & QIdentityProxyModel & QAbstractItemModel = Crash :- ):

            Yes testModel = QAbstractItemModel,

            So why do you call mGenericModel->beginInsertRows() then? You don't insert rows in mGenericModel at all...

            eeee

            class myCustomItems{
                testModel *mGenericModel
            public:
                  myCustomItems(const QString &displayString);
                 ~myCustomItems();
                 inline void setModel(testModel*modelPtr){mGenericModel=modelPtr};
            }
             
            

            I'm calling model signal from item to trigger the function thini... Or what should I do ? o.O

            edit Fudge I messed up class name... sigh. corrected 1st post. My bad :- ( was trying to simplify the code from clutter

            D Offline
            D Offline
            Dariusz
            wrote on 3 Jan 2020, 00:16 last edited by Dariusz 1 Mar 2020, 00:30
            #6

            @Christian-Ehrlicher Ok so hours latter... I added layoutAboutToBeChanged() and layoutChanged() and changePersistentIndexList(indexBefore, indexAfter);

            I noticed that I was getting now a weird qt warning...

            		WARNING: QObject::connect: Cannot queue arguments of type 'QList<QPersistentModelIndex>'
            

            (Make sure 'QList<QPersistentModelIndex>' is registered using qRegisterMetaType().)

            I tried registering them via >

            qRegisterMetaType<QList<QPersistentModelIndex>>("QList<QPersistentModelIndex>");
            qRegisterMetaTypeStreamOperators<QList<QPersistentModelIndex>>("QList<QPersistentModelIndex>");
            

            But I get error C2679: binary '>>': no operator found which takes a right-hand operand of type 'T' (or there is no acceptable conversion) I tried without const, same error. This happens when layoutChanged gets called... feels to me like that is the problem as sortModels/identityProxy needs to refresh properly...

            What do I do now? :- (

            Edit as a work around I ended up emiting myLayoutChanged() from my abstractModel to identityProxy to sortFilter which triggers invalidate() on sortFilter that "fixed" the issue. But as far as I'm conserned its a bug in Qt that the layoutAboutToBeChanged and layoutChanged does not work :- (

            I'm on 5.14 version.

            Any help would be amazing.

            C 1 Reply Last reply 3 Jan 2020, 07:56
            0
            • D Dariusz
              3 Jan 2020, 00:16

              @Christian-Ehrlicher Ok so hours latter... I added layoutAboutToBeChanged() and layoutChanged() and changePersistentIndexList(indexBefore, indexAfter);

              I noticed that I was getting now a weird qt warning...

              		WARNING: QObject::connect: Cannot queue arguments of type 'QList<QPersistentModelIndex>'
              

              (Make sure 'QList<QPersistentModelIndex>' is registered using qRegisterMetaType().)

              I tried registering them via >

              qRegisterMetaType<QList<QPersistentModelIndex>>("QList<QPersistentModelIndex>");
              qRegisterMetaTypeStreamOperators<QList<QPersistentModelIndex>>("QList<QPersistentModelIndex>");
              

              But I get error C2679: binary '>>': no operator found which takes a right-hand operand of type 'T' (or there is no acceptable conversion) I tried without const, same error. This happens when layoutChanged gets called... feels to me like that is the problem as sortModels/identityProxy needs to refresh properly...

              What do I do now? :- (

              Edit as a work around I ended up emiting myLayoutChanged() from my abstractModel to identityProxy to sortFilter which triggers invalidate() on sortFilter that "fixed" the issue. But as far as I'm conserned its a bug in Qt that the layoutAboutToBeChanged and layoutChanged does not work :- (

              I'm on 5.14 version.

              Any help would be amazing.

              C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 3 Jan 2020, 07:56 last edited by
              #7

              @Dariusz said in QSortFilterProxy & QIdentityProxyModel & QAbstractItemModel = Crash :- ):

              Any help would be amazing.

              Provide a minimal compilable example instead just some code fragments.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on 3 Jan 2020, 10:34 last edited by
                #8

                Try spawning a Model Test side by side your model. Most of the time the error is easily caught by the test and it gives you a very good clue on what's going wrong

                "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

                D 1 Reply Last reply 25 Jan 2020, 10:50
                1
                • VRoninV VRonin
                  3 Jan 2020, 10:34

                  Try spawning a Model Test side by side your model. Most of the time the error is easily caught by the test and it gives you a very good clue on what's going wrong

                  D Offline
                  D Offline
                  Dariusz
                  wrote on 25 Jan 2020, 10:50 last edited by
                  #9

                  @VRonin said in QSortFilterProxy & QIdentityProxyModel & QAbstractItemModel = Crash :- ):

                  Try spawning a Model Test side by side your model. Most of the time the error is easily caught by the test and it gives you a very good clue on what's going wrong

                  Wow, that's amazing! Thanks so much!

                  I've been testing my model for few days now, but I struggle with one error...

                  WARNING: FAIL! Compared values are not the same:
                     Actual (model->rowCount(parent)) 4
                     Expected (c.oldSize + (end - start + 1)) 5
                     (qabstractitemmodeltester.cpp:669)
                  

                  This happens during append function as far sa I can tell. I have 2 of them.

                  1st : - for appending vector of items

                          mGenericModel->beginInsertRows(index(), mChildren.size(), mChildren.size() + items.size() - 1);
                          mChildren.append(items);
                          mGenericModel->endInsertRows();
                  

                  2nd : - for appending 1 item.

                          int loc = mChildren.size();
                          mGenericModel->beginInsertRows(index(), loc, loc);
                          mChildren.append(nodePtr);
                          mGenericModel->endInsertRows();
                  

                  The model->rowCount(parent) runs this:

                  {
                      icGenericTreeItem *parentNode = getItemFromIndex(parent);
                      if (!parentNode)return 0;
                      return parentNode->getChildCount();
                  }
                  

                  Does any of the code above look wrong?

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    Dariusz
                    wrote on 25 Jan 2020, 10:59 last edited by Dariusz
                    #10

                    Another error that I'm getting reports off is :

                    WARNING: QObject::connect: Cannot queue arguments of type 'QAbstractItemModel::LayoutChangeHint'
                    (Make sure 'QAbstractItemModel::LayoutChangeHint' is registered using qRegisterMetaType().)
                    

                    No idea how to bite this one at all...

                    68c84439-7b15-41c2-b35c-fca919d6c9e9-image.png

                    I sweat I'm not sending some signals/info to my proxy models. But I have no idea which/where. The TestModesl does not report anything in this case.


                    Here is my code for drag/drop action to handle indexes/layouts...

                     Q_EMIT layoutAboutToBeChanged();
                    
                        QModelIndexList indexBefore;
                        QModelIndexList indexAfter;
                        std::reverse(newData.begin(), newData.end());
                    
                        /// Get old indexes
                        for (int x = 0; x < newData.size(); ++x) {
                            indexBefore.append(newData[x]->index());
                        }
                    
                        /// Action below will emit beginRemoveRow for each item 1 by 1 and then beginInsertRows for each item 1 by 1. 1st remove all 
                            rows then insert all rows.
                        if (row == -1) {
                            i->appendChildren(newData);
                        } else {
                            i->insertChildren(newData, row);
                        }
                        /// get new indexes
                        for (int x = 0; x < newData.size(); ++x) {
                            indexAfter.append(newData[x]->index());
                        }
                    
                        changePersistentIndexList(indexBefore, indexAfter);
                        Q_EMIT layoutChanged();
                    

                    I mean the question that I have now... when I'm adding 20 items from random selection order. Can I 1st remove them all 1 by 1 and then add them in all in 1 call?

                    So assuming vector<treeItem*> items;
                            for (auto &item:items) {
                                item->setParent(this); // this calls removeRows(item->parent.index(),item->row(),item->row()); and endRemoveRows(); + item->parent.mChildren().removeItem(item->row());
                                item->setModel(mGenericModel);
                            }
                    and then:
                            mGenericModel->beginInsertRows(index(), mChildren.size(), mChildren.size() + items.size() - 1);
                            mChildren.append(items);
                            mGenericModel->endInsertRows();
                    

                    Or I cant call removeRows in this order and I have to removeRow then insert then remove then insert and so on ?
                    TIA

                    kshegunovK 1 Reply Last reply 25 Jan 2020, 16:13
                    0
                    • D Dariusz
                      25 Jan 2020, 10:59

                      Another error that I'm getting reports off is :

                      WARNING: QObject::connect: Cannot queue arguments of type 'QAbstractItemModel::LayoutChangeHint'
                      (Make sure 'QAbstractItemModel::LayoutChangeHint' is registered using qRegisterMetaType().)
                      

                      No idea how to bite this one at all...

                      68c84439-7b15-41c2-b35c-fca919d6c9e9-image.png

                      I sweat I'm not sending some signals/info to my proxy models. But I have no idea which/where. The TestModesl does not report anything in this case.


                      Here is my code for drag/drop action to handle indexes/layouts...

                       Q_EMIT layoutAboutToBeChanged();
                      
                          QModelIndexList indexBefore;
                          QModelIndexList indexAfter;
                          std::reverse(newData.begin(), newData.end());
                      
                          /// Get old indexes
                          for (int x = 0; x < newData.size(); ++x) {
                              indexBefore.append(newData[x]->index());
                          }
                      
                          /// Action below will emit beginRemoveRow for each item 1 by 1 and then beginInsertRows for each item 1 by 1. 1st remove all 
                              rows then insert all rows.
                          if (row == -1) {
                              i->appendChildren(newData);
                          } else {
                              i->insertChildren(newData, row);
                          }
                          /// get new indexes
                          for (int x = 0; x < newData.size(); ++x) {
                              indexAfter.append(newData[x]->index());
                          }
                      
                          changePersistentIndexList(indexBefore, indexAfter);
                          Q_EMIT layoutChanged();
                      

                      I mean the question that I have now... when I'm adding 20 items from random selection order. Can I 1st remove them all 1 by 1 and then add them in all in 1 call?

                      So assuming vector<treeItem*> items;
                              for (auto &item:items) {
                                  item->setParent(this); // this calls removeRows(item->parent.index(),item->row(),item->row()); and endRemoveRows(); + item->parent.mChildren().removeItem(item->row());
                                  item->setModel(mGenericModel);
                              }
                      and then:
                              mGenericModel->beginInsertRows(index(), mChildren.size(), mChildren.size() + items.size() - 1);
                              mChildren.append(items);
                              mGenericModel->endInsertRows();
                      

                      Or I cant call removeRows in this order and I have to removeRow then insert then remove then insert and so on ?
                      TIA

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on 25 Jan 2020, 16:13 last edited by
                      #11

                      @Dariusz said in QSortFilterProxy & QIdentityProxyModel & QAbstractItemModel = Crash :- ):

                      Another error that I'm getting reports off is :
                      WARNING: QObject::connect: Cannot queue arguments of type 'QAbstractItemModel::LayoutChangeHint'
                      (Make sure 'QAbstractItemModel::LayoutChangeHint' is registered using qRegisterMetaType().)

                      Please tell me you are not using threads ...

                      Read and abide by the Qt Code of Conduct

                      D 1 Reply Last reply 25 Jan 2020, 16:39
                      0
                      • kshegunovK kshegunov
                        25 Jan 2020, 16:13

                        @Dariusz said in QSortFilterProxy & QIdentityProxyModel & QAbstractItemModel = Crash :- ):

                        Another error that I'm getting reports off is :
                        WARNING: QObject::connect: Cannot queue arguments of type 'QAbstractItemModel::LayoutChangeHint'
                        (Make sure 'QAbstractItemModel::LayoutChangeHint' is registered using qRegisterMetaType().)

                        Please tell me you are not using threads ...

                        D Offline
                        D Offline
                        Dariusz
                        wrote on 25 Jan 2020, 16:39 last edited by
                        #12

                        @kshegunov I've checked code, mostly nope except for selectionModel()->select(items); on my treeView.
                        Update() is called on QMetaInvole() via QueuedConnection.

                        Could the selection model cause it ? :O

                        JKSHJ kshegunovK C 3 Replies Last reply 25 Jan 2020, 16:49
                        0
                        • D Dariusz
                          25 Jan 2020, 16:39

                          @kshegunov I've checked code, mostly nope except for selectionModel()->select(items); on my treeView.
                          Update() is called on QMetaInvole() via QueuedConnection.

                          Could the selection model cause it ? :O

                          JKSHJ Offline
                          JKSHJ Offline
                          JKSH
                          Moderators
                          wrote on 25 Jan 2020, 16:49 last edited by
                          #13

                          @Dariusz said in QSortFilterProxy & QIdentityProxyModel & QAbstractItemModel = Crash :- ):

                          mostly nope except for...

                          It needs to be 100% nope. Models and views are not thread-safe. They can crash if you run read/write their data from other threads.

                          It is possible to create thread-safe models, but you need to explicitly write your model code in a way that is thread safe.

                          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                          1 Reply Last reply
                          2
                          • D Dariusz
                            25 Jan 2020, 16:39

                            @kshegunov I've checked code, mostly nope except for selectionModel()->select(items); on my treeView.
                            Update() is called on QMetaInvole() via QueuedConnection.

                            Could the selection model cause it ? :O

                            kshegunovK Offline
                            kshegunovK Offline
                            kshegunov
                            Moderators
                            wrote on 25 Jan 2020, 16:53 last edited by
                            #14

                            What @JKSH said - no threading in the views & models.

                            Read and abide by the Qt Code of Conduct

                            1 Reply Last reply
                            0
                            • D Offline
                              D Offline
                              Dariusz
                              wrote on 25 Jan 2020, 16:54 last edited by
                              #15

                              So all of the issues I'm getting above could be due to running selection function on thread? :O Gotta test it now ! Brb.

                              kshegunovK 1 Reply Last reply 25 Jan 2020, 16:56
                              0
                              • D Dariusz
                                25 Jan 2020, 16:54

                                So all of the issues I'm getting above could be due to running selection function on thread? :O Gotta test it now ! Brb.

                                kshegunovK Offline
                                kshegunovK Offline
                                kshegunov
                                Moderators
                                wrote on 25 Jan 2020, 16:56 last edited by kshegunov
                                #16

                                @Dariusz said in QSortFilterProxy & QIdentityProxyModel & QAbstractItemModel = Crash :- ):

                                So all of the issues I'm getting above could be due to running selection function on thread?

                                And this is surprising? I've had a program run over a year before it hit a race condition, and I'd like to think I know what I'm doing.

                                Read and abide by the Qt Code of Conduct

                                1 Reply Last reply
                                0
                                • D Dariusz
                                  25 Jan 2020, 16:39

                                  @kshegunov I've checked code, mostly nope except for selectionModel()->select(items); on my treeView.
                                  Update() is called on QMetaInvole() via QueuedConnection.

                                  Could the selection model cause it ? :O

                                  C Offline
                                  C Offline
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on 25 Jan 2020, 17:54 last edited by
                                  #17

                                  @Dariusz said in QSortFilterProxy & QIdentityProxyModel & QAbstractItemModel = Crash :- ):

                                  Could the selection model cause it

                                  It does, not could...

                                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                  Visit the Qt Academy at https://academy.qt.io/catalog

                                  1 Reply Last reply
                                  0
                                  • D Offline
                                    D Offline
                                    Dariusz
                                    wrote on 25 Jan 2020, 18:34 last edited by Dariusz
                                    #18

                                    Hey

                                    Ok so I disabled my threaded selection function but sadly I'm still getting crashes after large drag, dragging few items does not cause issue, but items with children/etc seems to cause crash more often. Its very random :- ((

                                    2d00a7ad-102d-43cc-9a7c-75bd9c09a60a-image.png
                                    a0e731bb-6ea4-40f6-a1ba-4cd4568d3e7d-image.png
                                    f2256f79-5661-4b89-848f-aa162904c9c9-image.png

                                    1 Reply Last reply
                                    0
                                    • C Offline
                                      C Offline
                                      Christian Ehrlicher
                                      Lifetime Qt Champion
                                      wrote on 25 Jan 2020, 18:58 last edited by
                                      #19

                                      This still looks like a threading issue since _q_sourceRowsInserted() is the slot which is connected to the signal rowsInserted() from the source model. When both classes would be in the same thread then this function would have been called directly and not through an event like in your case here.

                                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                      Visit the Qt Academy at https://academy.qt.io/catalog

                                      D 1 Reply Last reply 25 Jan 2020, 20:57
                                      0
                                      • C Christian Ehrlicher
                                        25 Jan 2020, 18:58

                                        This still looks like a threading issue since _q_sourceRowsInserted() is the slot which is connected to the signal rowsInserted() from the source model. When both classes would be in the same thread then this function would have been called directly and not through an event like in your case here.

                                        D Offline
                                        D Offline
                                        Dariusz
                                        wrote on 25 Jan 2020, 20:57 last edited by
                                        #20

                                        @Christian-Ehrlicher :O !

                                        I have now
                                        QAbstractItemModel = my model
                                        QIdentityProxyModel = my custom identity - no changes here just place holder
                                        QSortFilterProxyModel = this one is tweaked.

                                        When I setModels on each of them, do I have to make any connections between them signals/slots of any of it?
                                        I made one for selection/drag notifaction to re-select dropped items, but that is via QueuedConnection thus should not make any issue ?

                                        I did more testing, seems like now any drag/drop is broken for me. Jezus... what a weekend.

                                        1054252a-3739-42c0-8eed-5fb23625510d-image.png

                                        I'm dropping QT from 5.14.0 to 5.13.1, perhaps its library bug as I'm running out of ideas whats wrong now :- (((

                                        1 Reply Last reply
                                        0
                                        • C Offline
                                          C Offline
                                          Christian Ehrlicher
                                          Lifetime Qt Champion
                                          wrote on 25 Jan 2020, 21:06 last edited by
                                          #21

                                          Minimize your code until it does no longer occour so we can take a look at your model. Otherwise we can't help here anymore.

                                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                          Visit the Qt Academy at https://academy.qt.io/catalog

                                          D 1 Reply Last reply 25 Jan 2020, 21:20
                                          0

                                          • Login

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