Multi-process or multi-threaded start QApplication, The GUI does not work properly
-
- The first scene:
In main() function,I create a thread using pthread_create(), then start QApplication and main widget in this thread,exit the thread after I close the main widget. When I create a thread and start QApplication and main widget again,I‘m trying to drag a text item in main widget,the main widget will be stuck.
code:
void *test(void *) { QApplication a(m_argc, m_argv); MainWindow w; w.show(); a.exec(); return NULL; } int main(int argc, char *argv[]) { Q_UNUSED(argc); Q_UNUSED(argv); while(1) { pthread_t gui_thread; int err = pthread_create(&gui_thread, NULL, test, NULL); if(err) { printf("Error: failed to create thread, exit\n"); exit(1); } pthread_join(gui_thread, NULL); } return 0; }
I am debugging it with Qt source code, the MouseRelease event cannot be captured,so the event loop cannot be exited.
- The second scene:
In a non-Qt process,I use fork() to create a new process, in subprocess,I start a QApplication and main widget,It's working normally.If I use pthread_create() create a new thread in the main process,and start QApplication in sub thread. I use fork() to create a sub process after the sub-thread exit, then start QApplication and main widget in sub process, the main widget cannot be showed.
- The first scene:
-
@wang-stone
I don't know why you are trying to do things this way. For a start I don't think you can re-create aQApplication
instance more than once, I don't think Qt infrastructure is designed for this. Nor do I know what state it is in if you createQApplication
in afork()
ed sub-process (noexec...()
) where it's still sharing data with the caller.The normal way of doing things is to allow the Qt event loop to run on the main thread, and do anything else computational in secondary threads if desired.
You might also consider using Qt's
QThread
etc. support rather than worrying about any compatibility issues withpthread
s. -
@wang-stone said in Multi-process or multi-threaded start QApplication, The GUI does not work properly:
- The second scene:
In a non-Qt process,I use fork() to create a new process, in subprocess,I start a QApplication and main widget,It's working normally.If I use pthread_create() create a new thread in the main process,and start QApplication in sub thread. I use fork() to create a sub process after the sub-thread exit, then start QApplication and main widget in sub process, the main widget cannot be showed.
I don't understand the question. Code will almost certainly make it clearer.
@JonB said in Multi-process or multi-threaded start QApplication, The GUI does not work properly:
@wang-stone
I don't know why you are trying to do things this way. For a start I don't think you can re-create aQApplication
instance more than once, I don't think Qt infrastructure is designed for this.There can only be one Q*Application at a time (otherwise, what would QCoreApplication::instance() return?), but it can be repeatedly created and destroyed.
The normal way of doing things is to allow the Qt event loop to run on the main thread, and do anything else computational in secondary threads if desired.
The QApplication can be created in a thread that wasn't created to execute main(), except for macOS, and possibly other platforms. Some global objects are created in the same thread as the QApplication. Trying to create a new QApplication instance in another thread may run into problems attempting to access these objects.
- The second scene:
-
@jeremy_k said in Multi-process or multi-threaded start QApplication, The GUI does not work properly:
There can only be one Q*Application at a time (otherwise, what would QCoreApplication::instance() return?), but it can be repeatedly created and destroyed.
I always respect your expertise. However I would suggest it may not be as straightforward as you imply to create, fully destroy, clean up and then recreate. See for example https://forum.qt.io/topic/153620/please-destroy-the-qapplication-singleton-before-creating-a-new-qapplication-instance. Maybe that is only a PySide/Python thing, I don't know.
-
@JonB said in Multi-process or multi-threaded start QApplication, The GUI does not work properly:
@jeremy_k said in Multi-process or multi-threaded start QApplication, The GUI does not work properly:
There can only be one Q*Application at a time (otherwise, what would QCoreApplication::instance() return?), but it can be repeatedly created and destroyed.
I always respect your expertise.
Ouch! Am I that old? ;-)
That emoticon may confirm it.However I would suggest it may not be as straightforward as you imply to create, fully destroy, clean up and then recreate. See for example https://forum.qt.io/topic/153620/please-destroy-the-qapplication-singleton-before-creating-a-new-qapplication-instance. Maybe that is only a PySide/Python thing, I don't know.
I saw that, and see
23 potential issues.- I don't know if python
del
means that the object is destroyed before the next "sequence point", or is merely unavailable to further python code. I've run into problems in my own code with large memory leaks when the garbage collector had not been executed recently. - There's the global objects caveat. This example appears to be in the application programmer's code, but I believe that there has been an instance or two in Qt library code.
- macOS, but then it wouldn't even appear to work for a single round.
- I don't know if python