call w.show() in another Thread
-
Hi
I Have a QtApplication and in the main.cpp
I want to call w.show() function that opens the main window in another thread
in fact, I want to open a window in a new thread
how can I do that ? -
Hi
I Have a QtApplication and in the main.cpp
I want to call w.show() function that opens the main window in another thread
in fact, I want to open a window in a new thread
how can I do that ?@S_Hamzeh74 said in call w.show() in another Thread:
how can I do that ?
you can't
ALL QWidget based classes have to live in the same thread as the QCore/QGui/QApplication
you may connect a signal from a different thread to the show slot of a QWidget, but nothing more.
-
All GUI calls have to go through the main thread. You can, however, put the call to w.show() from another thread into the main thread. Using signals and slots was already mentioned. I personally don't like to write a whole bunch of signals and slots for this. What I do instead is to use
QMetaObject::invokeMethod(qApp, [&w](){ w.show(); });
.qApp
will give you the instance ofQApplication
and invoke the lambda in the context (i.e. the thread) ofqApp
). You need to make sure that any work related to your window still executes inside another thread. I have put all my knowledge and different use cases particularly related to this question into a library: https://github.com/SimonSchroeder/QtThreadHelper