Why is it not possible to use Widget Classes inside a QThread?
-
Hi,
I was looking into QThread documentation to solve a problem at hand in our application and saw a detail saying that we cannot use widget classes inside a thread.
One would ask why I want to do this : I want to add a splash screen in our application which QSplashCreen should help with but for some reason it is crashing the application when run on our test system .I have been trying to get around this for two days to no avail so I decided to build my own splash screen.Threads came to mind as I want the splash screen not to be disturbed by the application start up overhead but the doc warns against any widget class inside a thread.I am currently looking for other possible solutions to the problem but am curious as to why this cannot be done.
Any input will be appreciated.Thanks.
-
I suggest you better find out why it is crashing. Restriction is due to UI screens everywhere and getting executed with different thread contexts. This will lead to many application control flow.
Now QApplication context is required to create UI objects. Worker threads don't have this object. You can create QApplication inside the worker thread and try to create the UI object. This is purely technical capability. It is not a good programming practice.
In summary better to troubleshoot the splash screen issue.
-
Thanks Dheerendra for the reply,
[quote author="Dheerendra" date="1389060817"]
In summary better to troubleshoot the splash screen issue.[/quote]This was my approach for the last two days as but the timing of the project does not allow me this luxury anymore.It is a project that was handled by other developers before .I hope for things to get better as I dig more into it.
[quote author="Dheerendra" date="1389060817"]
You can create QApplication inside the worker thread and try to create the UI object. This is purely technical capability. It is not a good programming practice.[/quote]Will try this just for experimentation :)
-
Could be useful : Splash Screen Sample : http://qtforall.blogspot.in/p/splash-screen-with-induced-delay.html
-
Thanks Rahul,
The link gives a good starting example. -
Hi,
[quote author="musimbate" date="1389058628"]for some reason it is crashing the application when run on our test system[/quote]Run your program in a debugger. What are the functions calls that lead up to the crash?
[quote author="musimbate" date="1389058628"]I am currently looking for other possible solutions to the problem but am curious as to why this cannot be done.[/quote]There was a discussion about this topic at http://qt-project.org/forums/viewthread/29030 . A simplified answer is:
- The OS's graphics system runs in the GUI thread. It reads QWidgets' member variables from the GUI thread.
- If a QWidget's member variables are modified in a secondary thread, they could be modified while the graphics system is in the middle of reading those variables.
[quote author="Rahul Das" date="1389076575"]Could be useful : Splash Screen Sample : http://qtforall.blogspot.in/p/splash-screen-with-induced-delay.html[/quote]You don't have to subclass QThread to access sleep() any more. In Qt 5, QThread::sleep() is public.
But more importantly, calling sleep(5) this way will freeze the application -- it will not perform any initialization while it is sleeping.
[quote author="musimbate" date="1389058628"]I want the splash screen not to be disturbed by the application start up overhead[/quote]Can you elaborate what you mean by "disturbed"? Can you give more details on how you want your splash screen to behave?
If you want your splash screen to provide progress reports during initialization, a quick and dirty solution is to:
Create and show your custom splash screen in the main thread
Run the initialization in the main thread
In your initialization code, call your splash screen's member functions directly to update it
-
Thanks for the detailed response JKSH,
[quote author="JKSH" date="1389093289"]
Can you elaborate what you mean by “disturbed”?Can you give more details on how you want your splash screen to behave?
[/quote]
I meant to have the spash screen implemented in its own thread of execution.The behaviour we want for the moment is to simply show our logo as the application starts.I was able to spot out the issue and the splash screen is working now.Thanks again for valuable responses.