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 4.3k 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 Crag_Hack

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

    I took a peek... if there's no code for offloading to a worker thread how does one adopt the same approach in their own models? Do you have to inherit from QFileSystemModel?

    Unless my own model doesn't work properly I'd like to continue using it the way I am... it works great as far as I can tell and there's no critical flaws in the approach right? Are there any caveats to constructing and processing the model in the worker thread the way I am now? I only care that it works and is 'good enough'.. not seeking absolute perfection for now.

    When you say "Suffice, you'll have to check but moving back and forth your model at any moment will likely create issues in the long run." do you just mean I have to see if my tree view works properly empirically and determine sufficiency from that?

    Hopefully this wraps up this thread, thanks for the help.

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

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

    I took a peek... if there's no code for offloading to a worker thread how does one adopt the same approach in their own models? Do you have to inherit from QFileSystemModel?

    No you don't. As for the integration, it depends on what you do in your thread. The QThread documentation shows the two possible techniques (worker object and QThread subclass).

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

    When you say "Suffice, you'll have to check but moving back and forth your model at any moment will likely create issues in the long run." do you just mean I have to see if my tree view works properly empirically and determine sufficiency from that?

    I would recommend running dedicated tests rather than just checking if it's working on a workstation.

    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
      #27

      The QThread documentation shows the two possible techniques (worker object and QThread subclass).

      That's what I've been doing all along! Lol. I use a worker object but I said I used a worker thread sorry if I didn't make that clear.

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

        From your description your worker object seems to be the model itself, isn't it ?

        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
          #29

          Nope I have a worker object initialized in the constructor of the program object as below. Then I create the models in the worker thread as in the previously posted code and signal/slot them back and forth between the Worker thread and main GUI thread as needed. Given this should I still avoid those moveToThread() calls? Also is this a proper way of doing things?

          workerThread = new QThread(this);
          workerThread->start();
          worker = new Worker;
          worker->moveToThread(workerThread);
          
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #30

            That's what makes it "unclean". The worker object should do the heavy processing and then populate the model through signals and slots, not create it.

            Out of curiosity, what kind of heavy lifting must be done ?

            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
            1
            • SGaistS SGaist

              That's what makes it "unclean". The worker object should do the heavy processing and then populate the model through signals and slots, not create it.

              Out of curiosity, what kind of heavy lifting must be done ?

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

              That's what makes it "unclean". The worker object should do the heavy processing and then populate the model through signals and slots, not create it.

              How does one populate through signals/slots? I am dealing with potentially multiple tens of thousands of items in the trees so it doesn't make sense to signal/slot each item does it?

              Also what's so bad about this approach that makes things 'unclean'. Doesn't it work fine? Are there any critical flaws?

              Out of curiosity, what kind of heavy lifting must be done ?

              This particular tree displays file/directory structures and allows the user to manipulate them.

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

                You don't have to signal each and every change, that's when you make your code smart and do batch updates for example.

                Also, performance wise, there's the question of the interest of having tens of thousands of items loaded at the same time in memory. Are they worth being all loaded or would a subset make more sense ? For example, would it make sense to load all the files from the root of your disk ? Also, do your users really need to have all the data pre-loaded or would "just in time" details be enough ?

                Then, why is there a need to remove the model from the view when there's an action to be done with its data ? Here is the potential code smell. There should be no need for that in the first place.

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

                C 2 Replies Last reply
                0
                • SGaistS SGaist

                  You don't have to signal each and every change, that's when you make your code smart and do batch updates for example.

                  Also, performance wise, there's the question of the interest of having tens of thousands of items loaded at the same time in memory. Are they worth being all loaded or would a subset make more sense ? For example, would it make sense to load all the files from the root of your disk ? Also, do your users really need to have all the data pre-loaded or would "just in time" details be enough ?

                  Then, why is there a need to remove the model from the view when there's an action to be done with its data ? Here is the potential code smell. There should be no need for that in the first place.

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

                  You don't have to signal each and every change, that's when you make your code smart and do batch updates for example.

                  Can you show me some brief code/pseudocode or elaborate? I still don't know how to populate in this regard.

                  Also, performance wise, there's the question of the interest of having tens of thousands of items loaded at the same time in memory. Are they worth being all loaded or would a subset make more sense ? For example, would it make sense to load all the files from the root of your disk ? Also, do your users really need to have all the data pre-loaded or would "just in time" details be enough ?

                  I just did a benchmark and loading a tree of 50,000 items several levels deep only increased memory consumption from 12 MB to 51 MB. Also the tree loaded in 10 seconds or so. So maybe down the road I can change things but for now I'm just looking forward to completion sooner rather than later.

                  Then, why is there a need to remove the model from the view when there's an action to be done with its data ? Here is the potential code smell. There should be no need for that in the first place.

                  The items have state in member variables for processing and also once the user has completed interaction with the view the model gets sent to the worker thread for processing. The view is no longer needed at this point; hence my original question about if I need to do anything to the views before I send the model off for processing.

                  Here is the potential code smell.

                  Is this the 'unclean' part? Is the rest a proper approach? This is the most important question for me for now.

                  Also I was hoping this thread would serve for other Googlers out there looking for a quick and easy simple solution to this scenario; this scenario must be pretty common.

                  Thanks for your time :)

                  1 Reply Last reply
                  0
                  • SGaistS SGaist

                    You don't have to signal each and every change, that's when you make your code smart and do batch updates for example.

                    Also, performance wise, there's the question of the interest of having tens of thousands of items loaded at the same time in memory. Are they worth being all loaded or would a subset make more sense ? For example, would it make sense to load all the files from the root of your disk ? Also, do your users really need to have all the data pre-loaded or would "just in time" details be enough ?

                    Then, why is there a need to remove the model from the view when there's an action to be done with its data ? Here is the potential code smell. There should be no need for that in the first place.

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

                    You don't have to signal each and every change, that's when you make your code smart and do batch updates for example.

                    Thought about this a little... do I just create the tree model's items in the worker thread then pass the root model item to the main GUI thread for use in the model?

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

                      Are you creating a custom data structure ?

                      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

                        Are you creating a custom data structure ?

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

                        Yes just a tree item structure guy with the relevant parent and children functions and data relevant to the state of the items.

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

                          Sounds good.

                          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

                            Sounds good.

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

                            Awesome thanks now I know how to do things... I'm left with one question out of curiosity: what kinda problems can passing the model between threads cause?
                            Thanks for everything

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

                              Data access from different threads without proper locking for example. In your case, you are removing and adding the model back to the view but it's not good practice in the sense that there should be no need for that. And as I wrote before, a model can be accessed by multiple views or other objects so you would also have to track these properly.

                              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