create new QApplication in different thread
-
running this very simple code
int main(int argc, char** argv) { qDebug() << "create app 1" << QThread::currentThreadId(); QApplication* a = new QApplication(argc, argv); qDebug() << "delete app 1"; delete a; qDebug() << "create app 2" << QThread::currentThreadId(); QApplication* b = new QApplication(argc, argv); qDebug() << "delete app 2"; delete b; auto func = [&] { qDebug() << "create app 3" << QThread::currentThreadId(); QApplication* c = new QApplication(argc, argv); qDebug() << "delete app 2"; delete c; }; std::thread t(func); t.join(); qDebug() << "done"; return 0; }I get the output
create app 1 0x420c delete app 1 create app 2 0x420c delete app 2 create app 3 0x75fc WARNING: QApplication was not created in the main() thread. Exception thrown at 0x00007FFE915B7BC9 (Qt6Guid.dll)Now I do not understand:
I read there can be only one instance of QApplication - Ok I take care of that
I read a QApplication can be created on a different thread, which then becomes sort of the application thread
Then why is this example not possible?Qt 6.11.1, Windows 11
-
running this very simple code
int main(int argc, char** argv) { qDebug() << "create app 1" << QThread::currentThreadId(); QApplication* a = new QApplication(argc, argv); qDebug() << "delete app 1"; delete a; qDebug() << "create app 2" << QThread::currentThreadId(); QApplication* b = new QApplication(argc, argv); qDebug() << "delete app 2"; delete b; auto func = [&] { qDebug() << "create app 3" << QThread::currentThreadId(); QApplication* c = new QApplication(argc, argv); qDebug() << "delete app 2"; delete c; }; std::thread t(func); t.join(); qDebug() << "done"; return 0; }I get the output
create app 1 0x420c delete app 1 create app 2 0x420c delete app 2 create app 3 0x75fc WARNING: QApplication was not created in the main() thread. Exception thrown at 0x00007FFE915B7BC9 (Qt6Guid.dll)Now I do not understand:
I read there can be only one instance of QApplication - Ok I take care of that
I read a QApplication can be created on a different thread, which then becomes sort of the application thread
Then why is this example not possible?Qt 6.11.1, Windows 11
@csab6597 this is bad coding . I think it is better to use QProcess as it seems you dont understand the purpose of your QApplication
-
running this very simple code
int main(int argc, char** argv) { qDebug() << "create app 1" << QThread::currentThreadId(); QApplication* a = new QApplication(argc, argv); qDebug() << "delete app 1"; delete a; qDebug() << "create app 2" << QThread::currentThreadId(); QApplication* b = new QApplication(argc, argv); qDebug() << "delete app 2"; delete b; auto func = [&] { qDebug() << "create app 3" << QThread::currentThreadId(); QApplication* c = new QApplication(argc, argv); qDebug() << "delete app 2"; delete c; }; std::thread t(func); t.join(); qDebug() << "done"; return 0; }I get the output
create app 1 0x420c delete app 1 create app 2 0x420c delete app 2 create app 3 0x75fc WARNING: QApplication was not created in the main() thread. Exception thrown at 0x00007FFE915B7BC9 (Qt6Guid.dll)Now I do not understand:
I read there can be only one instance of QApplication - Ok I take care of that
I read a QApplication can be created on a different thread, which then becomes sort of the application thread
Then why is this example not possible?Qt 6.11.1, Windows 11
@csab6597 said in create new QApplication in different thread:
I read a QApplication can be created on a different thread, which then becomes sort of the application thread
Yes, that's right.
If you don't create
aandbfirst, then yourcwill not produce any warnings and yourfuncthread will be considered the "main thread".Then why is this example not possible?
This is a current limitation in Qt.
QGuiApplicationcreates some internal QObjects that never get destroyed (here is one example: https://qt-project.atlassian.net/browse/QTBUG-141714), so it is currently not possible to re-createQGuiApplication/QApplicationin a different thread from your first instance.In contrast, your example should work fine if you use
QCoreApplication. -
@csab6597 this is bad coding . I think it is better to use QProcess as it seems you dont understand the purpose of your QApplication
@Ronel_qtmaster said in create new QApplication in different thread:
@csab6597 this is bad coding . I think it is better to use QProcess as it seems you dont understand the purpose of your QApplication
There are perfectly valid use-cases to destroy a QApplication and recreate it in another thread. For example, when Qt is used to implement a plugin for a non-Qt app.
-
Thanks for the clarification. That explains the behavior.
The limitation makes sense for my use case (Qt used as a plugin inside a non-Qt application). I had interpreted the documentation to mean that, as long as only one
QApplicationinstance exists at a time, recreating it in a different thread would be supported.Knowing that
QGuiApplicationleaves behind internal objects tied to the first thread, making cross-thread recreation currently impossible, answers my question. I'll adjust the design accordingly.Thanks for the detailed explanation and the Qt bug reference.