PDA

View Full Version : Dialog in a QThread



bpetty
9th January 2007, 01:50
I have been trying to get a spawned thread to create a dialog! Everytime I run the code it asserts saying it can not create a widget in a non GUI thread.

Is there any way to do this?

Pretty much I just have a class that inherits from QThread. In run() I am wanting to initialize and show a dialog.

If you are wondering WHY am I doing this... I want to call a method in this class to open the dialog (which will be doing a lot of stuff) and then start a search. When the search is over it will call another method from the class to stop the thread, essentially closing the dialog.

So pretty much I am wanting to create a splash screen that I can turn on and off at will.

Brandybuck
9th January 2007, 03:29
There are issues on some of the platforms that currently limit Qt to only one GUI thread. This is being worked on, and the Trolls expect that this limitation will be gone in Qt5. In the meantime, however, all GUI operations need to be performed in the main GUI thread. That means no widgets, dialogs, painting, and a few not-so-obvious things like no QPixmaps either.

There is an easy workaround, however. In Qt4 you can send signals to objects owned by another thread. This is sent asynchronously. So you can create a dialog in the main GUI thread, show it in response to a signal sent when the worker thread starts, and hide it in response to a signal sent when the worker thread finishes. If you're using Qt3, you can create a similar solution by using your favorite inter-thread communication mechanism.

The Qt4 mandelbrot demo has an example of inter-thread signals and slots.