Qt quit both c++ and UI
-
Hello,
I have a Qt5 interface that runs both C++ and a UI made with QML.
At one point, I have to quit the program. Before, what I was doing was Qt.quit() in the QML and everything was ok.However, now I have some threads that I must close in the main.cpp before quitting. Instead of doing Qt.quit() in the QML, I want to close everything from my main.cpp such as
*main.cpp loop1.quit(); loop2.quit(); quitqt ?
the "quitqt" is what I miss. I tried different things from information I found on forums but the QML UI never quits and continue to run.
I tried for instance: app.quit() and QApplication.quit()What could I do ?
Thank you very much for your help,
Alex
-
Hi,
What about :
QGuiApplication app(argc, argv); // your stuff int ret = app.exec(); // Stop your threads cleanly return ret;
-
Thank you for your answer. However, the QML UI still remains.
That seems to be normal with this code. The following is the template code for a new Qt project:*main.cpp int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QLatin1String("qrc:/main.qml"))); return app.exec(); }
This main.cpp program will run and execute "return app.exec();" and the QML UI will remain.
In QML, the function "Qt.quit()" closes everythin but I don't know how to do the same through the main.cpp.Thank you for your help !
Alex
-
If I understand things correctly you would like to be able to quit your threads before ending the application, right ?
Hence my suggestion to store the return value from
app.exec()
, then quit your threads and then return the value you stored before.Or do you want your threads to quit your application ?
-
Sorry, I think I was not clear.
I want to quit my threads (this is done ok) and then I want to quit the application.
The problem is that in the main.cpp, only doing "return app.exec();" does not close my Qt application. The QML UI remains open. I want to close this QML UI. I can do it in the QML code with Qt.Quit() but I would like to do it in my main.cpp instead.Thank you very much,
Alex
-
Then call
qApp->quit()
once you're done with your threads. -
Can you show the code you are using ?
-
Here is the main.cpp code:
#include <QGuiApplication> #include <QQmlApplicationEngine> int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QLatin1String("qrc:/main.qml"))); qApp->quit(); return app.exec(); }
The QML code is a simple empty window.
When I run this main.cpp code, even with "qApp->quit();", the program does not quit (the QML window remains open).
From this forum, there seems to be other solutions. I tried "exit(1)" which seems to work but I am not sure it exits cleanly.
http://stackoverflow.com/questions/10038372/how-to-terminate-a-qt-programming-if-initialization-failedThank you again for your time !
Alex
-
@alecs26 said in Qt quit both c++ and UI:
When I run this main.cpp code, even with "qApp->quit();", the program does not quit (the QML window remains open).
Then there's something fishy. Either some thread is hanging or your QML code is hanging. As a side note, how do you ensure the threads had quit before returning and how do you start them (there's nothing even remotely resembling this in the C++ code you pasted).
By the way
qApp->quit();
does nothing here, there's no event loop running. You could try:
QMetaObject::invokeMethod(qApp, "quit", Qt::QueuedConnection);
instead.
I tried "exit(1)" which seems to work but I am not sure it exits cleanly.
Never do that without a good reason.
::exit
's purpose is to terminate the application in extreme circumstances, not whenever one feels like it. For one it will not call any of your object's (app
andengine
) destructors, which is bad enough by itself and has serious implications. -
@kshegunov @SGaist
The code I posted is for a simple program that just launches a qml window. It was to simplify the use case. Even with this simple case, I can't get the QML window to close (except with "exit(1)").
The QML code is simply:import QtQuick 2.7 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.0 ApplicationWindow { visible: true width: 640 height: 480 title: qsTr("Hello World") }
Your suggestion:
QMetaObject::invokeMethod(qApp, "quit", Qt::QueuedConnection);
seems to work. Can you tell me what it does different of qApp->quit ?
I guess it quits cleanly ?Thank you !
Alex
-
Calling
qApp->quit()
beforeexec()
won't have any effect since the event loop isn't even started.