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. QAbstractItemModel asserts & multithreading
QtWS25 Last Chance

QAbstractItemModel asserts & multithreading

Scheduled Pinned Locked Moved Unsolved General and Desktop
qabstractitemmoqassert
8 Posts 2 Posters 1.8k 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.
  • D Offline
    D Offline
    Dariusz
    wrote on last edited by
    #1

    Hey

    I'm trying to solve a problem with these 2 aseserts :

    ######
                            FATAL: ASSERT: "last < rowCount(parent)" in file itemmodels\qabstractitemmodel.cpp, line 2782
     (itemmodels\qabstractitemmodel.cpp:2782, (null))
    ######
    
    
    ######
                            FATAL: ASSERT: "!this->isEmpty()" in file c:\users\qt\work\qt\qtbase\include\qtcore\../../src/corelib/tools/qstack.h, line 62
     (c:\users\qt\work\qt\qtbase\include\qtcore\../../src/corelib/tools/qstack.h:62, (null))
    ######
    

    I've recently moved a lot of my logic to thread and when I load data all that happens in thread now. I'm not sure how to bite it as all I got are these 2 crash lines and
    d0aa29f9-0233-43af-a5c4-0ffb28082d04-image.png

    The error does not happen in debug-Release mode, only in debug mode...

    How can I bite it ?

    Regards
    Dariusz

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      A model must be in the same thread as the view(s) - so it must be in the main thread. If this is not the case you must add QMutexes (or similar) to protect access from two different threads.

      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
      5
      • D Offline
        D Offline
        Dariusz
        wrote on last edited by
        #3

        Yeah I've just dig in to it and found out the error location...

        It was beginRemoveRows/endRemoveRows being called from another thread while QporxyModel/filterModel were trying to refresh/update data...

        So I've learned that Q_EMIT beginRemoveRows() does not do what I though it did when it comes to signals..

        The signal is being called from Thread & executed from thread... but I need to force it to main thread, how can I do this ?

        Currently I'm thinking that I could do this >

        		QMetaObject::invokeMethod(qApp, [&]() { beginRemoveRows(index(),row,row);// + other code to finish the action... },Qt::BlockingQueuedConnection);
        

        But does this not have a lot of overhead? How can I handle case like this?

        Should I make my own signal/slot like

        signal void beginRemoveRowsThreaded(Const QModelIndex&index, const int start, const int right end);
        
        slot inline void handleRemoveRowsThread(Const QModelIndex&index, const int start, const int right end){Q_EMIT beginRemoveRows(index.start,end);}
        

        and do connection

        connect(this,&item::beginRemoveRowsThreaded,this,&item::handleRemoveRowsThread,Qt::BlockingQueuedConnection);
        
        

        and then call the internal signal to force it to main thread ? Will that be more efficient than the QMetaObject::invoke ?

        TIA

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I would send a signal to the model so it can fetch the new data from the thread - looks the easiest and cleanest solution for me - esp. since this will also work when the data fetching is not done in a separate thread, the logic will not change.

          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
          0
          • Christian EhrlicherC Christian Ehrlicher

            I would send a signal to the model so it can fetch the new data from the thread - looks the easiest and cleanest solution for me - esp. since this will also work when the data fetching is not done in a separate thread, the logic will not change.

            D Offline
            D Offline
            Dariusz
            wrote on last edited by
            #5

            @Christian-Ehrlicher said in QAbstractItemModel asserts & multithreading:

            I would send a signal to the model so it can fetch the new data from the thread - looks the easiest and cleanest solution for me - esp. since this will also work when the data fetching is not done in a separate thread, the logic will not change.

            Hmmmmmmmmmm I'm lost :- D So do I disable some automatic fetching and then configure manual fetching? I'm lost o.O how do I do dis?

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              What to you mean with automatic/manual fetching?

              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
              0
              • Christian EhrlicherC Christian Ehrlicher

                What to you mean with automatic/manual fetching?

                D Offline
                D Offline
                Dariusz
                wrote on last edited by
                #7

                @Christian-Ehrlicher I would send a signal to the model so it can fetch the new data from the thread
                Hmmm I think this might not work for me. Feels like I need it to work on per-item level. Essentially force any call changing child count to be main thread-based.

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Dariusz said in QAbstractItemModel asserts & multithreading:

                  Hmmm I think this might not work for me

                  Why not? It makes no difference if you push or pull the data... but it's up to you :)

                  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
                  1

                  • Login

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