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. Moving QAbstractItemModel Subclass Instance from Main GUI Thread to Worker Thread When Used in a QTreeView
Forum Updated to NodeBB v4.3 + New Features

Moving QAbstractItemModel Subclass Instance from Main GUI Thread to Worker Thread When Used in a QTreeView

Scheduled Pinned Locked Moved Unsolved General and Desktop
39 Posts 3 Posters 3.6k Views 2 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.
  • C Offline
    C Offline
    Crag_Hack
    wrote on last edited by
    #5

    @SGaist I am using a QAbstractItemModel subclass tree as my model. Creating it can take a while as can traversing through the tree after it's been created.

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

      That's why that part should be handled asynchronously / using a thread. The model itself should not need to move around. You may have several views on the same model which would make your current implementation impractical.

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

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Crag_Hack
        wrote on last edited by Crag_Hack
        #7

        @SGaist OK right now I do have the model creation in a worker thread and can move the tree traversal there easily as well. I thought I needed to use moveToThread() and a signal/slot for the model to move it to the GUI thread in order to use it for a QTreeView. See here for a related thread.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Crag_Hack
          wrote on last edited by
          #8

          @SGaist How do I use the model from a worker thread without using moveToThread() and a signal slot connection then? Thx

          SGaistS 1 Reply Last reply
          0
          • C Crag_Hack

            @SGaist How do I use the model from a worker thread without using moveToThread() and a signal slot connection then? Thx

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #9

            @Crag_Hack said in Moving QAbstractItemModel Subclass Instance from Main GUI Thread to Worker Thread When Used in a QTreeView:

            @SGaist How do I use the model from a worker thread without using moveToThread() and a signal slot connection then? Thx

            My suggestion is to do things the other way around: the model uses a separate thread for the heavy lifting.

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

            C 1 Reply Last reply
            0
            • SGaistS SGaist

              @Crag_Hack said in Moving QAbstractItemModel Subclass Instance from Main GUI Thread to Worker Thread When Used in a QTreeView:

              @SGaist How do I use the model from a worker thread without using moveToThread() and a signal slot connection then? Thx

              My suggestion is to do things the other way around: the model uses a separate thread for the heavy lifting.

              C Offline
              C Offline
              Crag_Hack
              wrote on last edited by
              #10

              @SGaist said in Moving QAbstractItemModel Subclass Instance from Main GUI Thread to Worker Thread When Used in a QTreeView:

              My suggestion is to do things the other way around: the model uses a separate thread for the heavy lifting.

              That's what I'm doing now... create the model in a worker thread for heavy lifting, pass to GUI thread for the QTreeView, have the user interact with it, then send it back to the worker thread for processing (tree traversal) to interpret the interactions. Am I confused as to what you are suggesting? If so could you elaborate?

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

                No it's not.

                My suggestion is that your model manages the thread. It does not move between threads.

                Take a look at the implementation of QFileSystemModel for inspiration.

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

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Crag_Hack
                  wrote on last edited by
                  #12

                  Will my current way of doing things suffice? I might be in a little over my head. I can take a peak at QFileSystemModel however I've had trouble interpreting Qt source code before.

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

                    Suffice, you'll have to check but moving back and forth your model at any moment will likely create issues in the long run.

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

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      Crag_Hack
                      wrote on last edited by Crag_Hack
                      #14

                      What kinda issues in the long run? No race conditions right?

                      1 Reply Last reply
                      0
                      • C Offline
                        C Offline
                        Crag_Hack
                        wrote on last edited by
                        #15

                        Forgot to mention I am signal/slotting pointers to the models not objects themselves... race condition possible? Do I need to mutex the model? Does this approach work well? Does this make more sense were you guys under the interpretation that I was signal/slotting objects? Thanks

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

                          Can you explain your model design ?

                          You just explained that you are moving pointer to objects around one or more models (it was only one until now but you used plural in your last message) that you move from one thread to another.

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

                          1 Reply Last reply
                          0
                          • C Offline
                            C Offline
                            Crag_Hack
                            wrote on last edited by Crag_Hack
                            #17

                            @SGaist Thanks I am using 5 different tree models and 5 different QTreeViews, one model per view, thus the original statement about only one instance. I dusted off my Computer Systems book from college and reread the concurrency chapter; I'm pretty sure race conditions aren't and issue but I'm by no means an expert. I posted some code below. The Worker object lives in a worker thread for long-lived operations. The Program object is the main program object living in the main GUI thread to handle all GUI display. Any input would be much appreciated.

                            void Worker::createModels()
                            {
                                TreeModel *treeFilesModel = new TreeModel();
                                //other 4 models created after this
                            
                                //code here to add items to the models
                            
                                treeFilesModel->moveToThread(QApplication::instance()->thread());
                                //other 4 models moved to main GUI thread
                                emit passModels(treeFilesModel,...other 4 models); //signal to move models to main GUI thread for display, target slot is passModelsAndSetupTree
                            }
                            
                            void Program::passModelsAndSetupTree(TreeModel *treeFilesModel,...other 4 models)
                            {
                                treeFilesView = new TreeView();
                                treeFilesView->setModel(treeFilesModel);
                                //other 4 views set up
                                //after everybody is set up user can interact with views to choose actions on model items displayed in views
                            }
                            
                            void Program::traverseTrees()
                            {
                                traverseTree(treeFilesModel->getFirstItem());
                                //other 4 models traversed
                            
                                //should I delete the treeviews here?
                                //should I move models to worker thread here using moveToThread()?
                            
                                emit process(treeFilesModel,...other 4 models); //signal to move models to worker thread for processing
                            }
                            
                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #18

                              I insist, stop moving your models around. There should be no need for that.

                              The name of your model variables makes it look like you are implementing your own QFileSystemModel, is that the case ?

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

                              1 Reply Last reply
                              0
                              • C Offline
                                C Offline
                                Crag_Hack
                                wrote on last edited by
                                #19

                                @SGaist said in Moving QAbstractItemModel Subclass Instance from Main GUI Thread to Worker Thread When Used in a QTreeView:

                                I insist, stop moving your models around. There should be no need for that.

                                So get rid of moveToThread() calls but leave signal/slots (obviously) right? If so we should update the other thread since I was told to use those calls there.

                                The name of your model variables makes it look like you are implementing your own QFileSystemModel, is that the case ?

                                I made the models from scratch borrowing code from this book. I never looked at how QFileSystemModel was constructed to influence my own models.

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

                                  Indeed, keep the signals and slots and stop moving your model around. Note that you can make use of threads within your model. Just that it does not make sense to move your model itself back and forth between threads.

                                  That's a very good book.

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

                                  1 Reply Last reply
                                  0
                                  • C Offline
                                    C Offline
                                    Crag_Hack
                                    wrote on last edited by Crag_Hack
                                    #21

                                    Thanks, sounds like I'm doing things in a acceptable fashion other than the moveToThread() calls right?

                                    Are moveToThread() calls only relevant for objects with event processing? Do they serve any other purpose?

                                    An alternative way of doing things would be to use threads within the model like QFileSystemModel does correct? I'll take a peak at the source.

                                    Before passing the models back to the worker thread for processing should I delete the associated QTreeViews or does it matter?

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

                                      I am currently only discussing the model move between different threads. For the rest I do not know what you do with it.

                                      The goal is to change thread affinity and thus call to any of the object function would then happen in the new thread.

                                      It's not really an alternate rather the correct way.

                                      There's no need to delete any view. If anything, set a null model.

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

                                      1 Reply Last reply
                                      0
                                      • C Offline
                                        C Offline
                                        Crag_Hack
                                        wrote on last edited by Crag_Hack
                                        #23

                                        I checked out the source code and can't find any thread usage in the model; I must be missing something the model looks almost the same as mine with the same virtual function overrides I use. Can you point me to the relevant code? Thanks. I have a design pattern book that I could dust off (ha another old dusty book) and reread the MVC chapter.... Also what threw me off in the past looking at Qt source was the non-descript class member variable names like d and q... why use names like that???

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

                                          There's no explicit QThread used, but if you look for just thread, you'll find more information.

                                          As for the d and q pointers, long story short, it because Qt uses the PIMPL idiom.

                                          For all the details, see this Wiki page about that matter and why Qt uses it. The short version is to allow the forward and backward compatibility within a major release that you can enjoy using Qt i.e. you can just switch the libraries without having to recompile your application (provided that the version you switch to has all the symbols you are using)

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

                                          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