Moving QAbstractItemModel Subclass Instance from Main GUI Thread to Worker Thread When Used in a QTreeView
-
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???
-
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)
-
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.
-
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.
@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.
-
From your description your worker object seems to be the model itself, isn't it ?
-
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);
-
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 ?
-
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 ?
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.
-
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.
-
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.
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 :)
-
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.
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?
-
Are you creating a custom data structure ?
-
Sounds good.
-
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.