Create an object instance in separate thread
Hi Community,
At the time of running my application, I need to instantiate an object.
The object creation takes time (about 10 seconds) and this leads to the consequence that the GUI is not shown until the object is created.
To work around this I thought of creating the instance in a second thread (thread B) but in this case the thread B is the owner of the instance is that right?
How can I transfer the ownership of the object to the main thread once it's been created? Is there any better solution?
Regards,
Franco
Re: Create an object instance in separate thread
Hi, you can transfer the object to the main thread using .
Another option would be to show a splashscreen while your object is initializing. Maybe even show some progress using QSplashScreen::showMessage().
Ginsengelf
Re: Create an object instance in separate thread
Quote:
To work around this I thought of creating the instance in a second thread
Keep in mind that GUI instances (inherited from QWidget) can only be created in the main thread, AFAIK. I am not sure if you can create one in another thread and then transfer it to the main thread unless it can be created without a parent and then transferred and given a parent from the main thread.
Every QWidget instance has a QWidget parent, and they can only live in the main thread.
You could also do things in reverse - create the object in the main thread, initialize it in the second thread, then move it back.
Personally, I use a splash screen where there are slow startup issues and keep the user entertained that way.
Re: Create an object instance in separate thread
Quote:
Originally Posted by
d_stranz
Keep in mind that GUI instances (inherited from QWidget) can only be created in the main thread, AFAIK. I am not sure if you can create one in another thread and then transfer it to the main thread unless it can be created without a parent and then transferred and given a parent from the main thread.
Every QWidget instance has a QWidget parent, and they can only live in the main thread.
You could also do things in reverse - create the object in the main thread, initialize it in the second thread, then move it back.
Personally, I use a splash screen where there are slow startup issues and keep the user entertained that way.
The object I need to create is not derived from QWidget
Re: Create an object instance in separate thread
Quote:
The object I need to create is not derived from QWidget
Then you should be OK if you follow @Ginsengelf's suggestion. Still, you should be careful to set up a mechanism where your initialization thread signals the main thread when it is finished so you do not start using the object you are initializing before that process is complete.
A splash screen still seems like a simpler approach :)