<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Qt quit both c++ and UI]]></title><description><![CDATA[<p dir="auto">Hello,</p>
<p dir="auto">I have a Qt5 interface that runs both C++ and a UI made with QML.<br />
At one point, I have to quit the program. Before, what I was doing was Qt.quit() in the QML and everything was ok.</p>
<p dir="auto">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</p>
<pre><code>*main.cpp
loop1.quit();
loop2.quit();
quitqt ?
</code></pre>
<p dir="auto">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.<br />
I tried for instance: app.quit()   and    QApplication.quit()</p>
<p dir="auto">What could I do ?</p>
<p dir="auto">Thank you very much for your help,</p>
<p dir="auto">Alex</p>
]]></description><link>https://forum.qt.io/topic/76082/qt-quit-both-c-and-ui</link><generator>RSS for Node</generator><lastBuildDate>Mon, 14 Sep 2026 21:26:14 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/76082.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 11 Feb 2017 19:19:45 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Qt quit both c++ and UI on Sun, 12 Feb 2017 16:14:05 GMT]]></title><description><![CDATA[<p dir="auto">Calling <code>qApp-&gt;quit()</code> before <code>exec()</code>  won't have any effect since the event loop isn't even started.</p>
]]></description><link>https://forum.qt.io/post/375257</link><guid isPermaLink="true">https://forum.qt.io/post/375257</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Sun, 12 Feb 2017 16:14:05 GMT</pubDate></item><item><title><![CDATA[Reply to Qt quit both c++ and UI on Sun, 12 Feb 2017 15:07:43 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kshegunov">@<bdi>kshegunov</bdi></a> <a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a><br />
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)").<br />
The QML code is simply:</p>
<pre><code>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")

}
</code></pre>
<p dir="auto">Your suggestion:</p>
<pre><code>QMetaObject::invokeMethod(qApp, "quit", Qt::QueuedConnection);
</code></pre>
<p dir="auto">seems to work. Can you tell me what it does different of qApp-&gt;quit ?<br />
I guess it quits cleanly ?</p>
<p dir="auto">Thank you !</p>
<p dir="auto">Alex</p>
]]></description><link>https://forum.qt.io/post/375237</link><guid isPermaLink="true">https://forum.qt.io/post/375237</guid><dc:creator><![CDATA[alecs26]]></dc:creator><pubDate>Sun, 12 Feb 2017 15:07:43 GMT</pubDate></item><item><title><![CDATA[Reply to Qt quit both c++ and UI on Sun, 12 Feb 2017 14:24:02 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/alecs26">@<bdi>alecs26</bdi></a> said in <a href="/post/375188">Qt quit both c++ and UI</a>:</p>
<blockquote>
<p dir="auto">When I run this main.cpp code, even with "qApp-&gt;quit();", the program does not quit (the QML window remains open).</p>
</blockquote>
<p dir="auto">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).</p>
<p dir="auto">By the way</p>
<pre><code>qApp-&gt;quit();
</code></pre>
<p dir="auto">does nothing here, there's no event loop running. You could try:</p>
<pre><code>QMetaObject::invokeMethod(qApp, "quit", Qt::QueuedConnection);
</code></pre>
<p dir="auto">instead.</p>
<blockquote>
<p dir="auto">I tried "exit(1)" which seems to work but I am not sure it exits cleanly.</p>
</blockquote>
<p dir="auto">Never do that without a good reason. <code>::exit</code>'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 (<code>app</code> and <code>engine</code>) destructors, which is bad enough by itself and has serious implications.</p>
]]></description><link>https://forum.qt.io/post/375219</link><guid isPermaLink="true">https://forum.qt.io/post/375219</guid><dc:creator><![CDATA[kshegunov]]></dc:creator><pubDate>Sun, 12 Feb 2017 14:24:02 GMT</pubDate></item><item><title><![CDATA[Reply to Qt quit both c++ and UI on Sun, 12 Feb 2017 12:21:42 GMT]]></title><description><![CDATA[<p dir="auto">Here is the main.cpp code:</p>
<pre><code>#include &lt;QGuiApplication&gt;
#include &lt;QQmlApplicationEngine&gt;

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-&gt;quit();

    return app.exec();
}
</code></pre>
<p dir="auto">The QML code is a simple empty window.</p>
<p dir="auto">When I run this main.cpp code, even with "qApp-&gt;quit();", the program does not quit (the QML window remains open).</p>
<p dir="auto">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.<br />
<a href="http://stackoverflow.com/questions/10038372/how-to-terminate-a-qt-programming-if-initialization-failed" target="_blank" rel="noopener noreferrer nofollow ugc">http://stackoverflow.com/questions/10038372/how-to-terminate-a-qt-programming-if-initialization-failed</a></p>
<p dir="auto">Thank you again for your time !</p>
<p dir="auto">Alex</p>
]]></description><link>https://forum.qt.io/post/375188</link><guid isPermaLink="true">https://forum.qt.io/post/375188</guid><dc:creator><![CDATA[alecs26]]></dc:creator><pubDate>Sun, 12 Feb 2017 12:21:42 GMT</pubDate></item><item><title><![CDATA[Reply to Qt quit both c++ and UI on Sat, 11 Feb 2017 23:10:06 GMT]]></title><description><![CDATA[<p dir="auto">Can you show the code you are using ?</p>
]]></description><link>https://forum.qt.io/post/375152</link><guid isPermaLink="true">https://forum.qt.io/post/375152</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Sat, 11 Feb 2017 23:10:06 GMT</pubDate></item><item><title><![CDATA[Reply to Qt quit both c++ and UI on Sat, 11 Feb 2017 23:04:32 GMT]]></title><description><![CDATA[<p dir="auto">Thank you again for your time.<br />
However it's still the same, the UI remains...</p>
]]></description><link>https://forum.qt.io/post/375149</link><guid isPermaLink="true">https://forum.qt.io/post/375149</guid><dc:creator><![CDATA[alecs26]]></dc:creator><pubDate>Sat, 11 Feb 2017 23:04:32 GMT</pubDate></item><item><title><![CDATA[Reply to Qt quit both c++ and UI on Sat, 11 Feb 2017 22:40:40 GMT]]></title><description><![CDATA[<p dir="auto">Then call <code>qApp-&gt;quit()</code> once you're done with your threads.</p>
]]></description><link>https://forum.qt.io/post/375142</link><guid isPermaLink="true">https://forum.qt.io/post/375142</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Sat, 11 Feb 2017 22:40:40 GMT</pubDate></item><item><title><![CDATA[Reply to Qt quit both c++ and UI on Sat, 11 Feb 2017 22:39:12 GMT]]></title><description><![CDATA[<p dir="auto">Sorry, I think I was not clear.<br />
I want to quit my threads (this is done ok) and then I want to quit the application.<br />
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.</p>
<p dir="auto">Thank you very much,</p>
<p dir="auto">Alex</p>
]]></description><link>https://forum.qt.io/post/375141</link><guid isPermaLink="true">https://forum.qt.io/post/375141</guid><dc:creator><![CDATA[alecs26]]></dc:creator><pubDate>Sat, 11 Feb 2017 22:39:12 GMT</pubDate></item><item><title><![CDATA[Reply to Qt quit both c++ and UI on Sat, 11 Feb 2017 22:32:22 GMT]]></title><description><![CDATA[<p dir="auto">If I understand things correctly you would like to be able to quit your threads before ending the application, right ?</p>
<p dir="auto">Hence my suggestion to store the return value from <code>app.exec()</code>, then quit your threads and then return the value you stored before.</p>
<p dir="auto">Or do you want your threads to quit your application ?</p>
]]></description><link>https://forum.qt.io/post/375137</link><guid isPermaLink="true">https://forum.qt.io/post/375137</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Sat, 11 Feb 2017 22:32:22 GMT</pubDate></item><item><title><![CDATA[Reply to Qt quit both c++ and UI on Sat, 11 Feb 2017 20:48:06 GMT]]></title><description><![CDATA[<p dir="auto">Thank you for your answer. However, the QML UI still remains.<br />
That seems to be normal with this code. The following is the template code for a new Qt project:</p>
<pre><code>*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();
}
</code></pre>
<p dir="auto">This main.cpp program will run and execute  "return app.exec();" and the QML UI will remain.<br />
In QML, the function "Qt.quit()" closes everythin but I don't know how to do the same through the main.cpp.</p>
<p dir="auto">Thank you for your help !</p>
<p dir="auto">Alex</p>
]]></description><link>https://forum.qt.io/post/375111</link><guid isPermaLink="true">https://forum.qt.io/post/375111</guid><dc:creator><![CDATA[alecs26]]></dc:creator><pubDate>Sat, 11 Feb 2017 20:48:06 GMT</pubDate></item><item><title><![CDATA[Reply to Qt quit both c++ and UI on Sat, 11 Feb 2017 20:22:42 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">What about :</p>
<pre><code>QGuiApplication app(argc, argv);

// your stuff

int ret = app.exec();

// Stop your threads cleanly

return ret;
</code></pre>
]]></description><link>https://forum.qt.io/post/375110</link><guid isPermaLink="true">https://forum.qt.io/post/375110</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Sat, 11 Feb 2017 20:22:42 GMT</pubDate></item></channel></rss>