Moving QAbstractItemModel Subclass Instance from Main GUI Thread to Worker Thread When Used in a QTreeView
-
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.
-
-
@SGaist How do I use the model from a worker thread without using moveToThread() and a signal slot connection then? Thx
@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.
-
@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.
@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?
-
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.
-
Suffice, you'll have to check but moving back and forth your model at any moment will likely create issues in the long run.
-
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
-
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.
-
@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 }
-
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 ?
-
@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.
-
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.
-
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?
-
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.
-
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)