<?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[open new QMainWindow and close the old one, by keeping QApplication alive.]]></title><description><![CDATA[<p dir="auto">Hi all,<br />
I’m quite new in Qt world.. so thank you in advance to everyone will help me.<br />
The question is simple:<br />
I would like to understand how I can open a new QMainWindow (and close the old one) by keeping the QApplication alive. In the specific a give you my real example.<br />
I have a “login” QMainWindow with a button. When this button is pressed, the login window should disappear and a new QMainWindow appears.</p>
<p dir="auto">So, this is the main:</p>
<pre><code>int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Login w;
    w.show();
    return a.exec();
}
</code></pre>
<p dir="auto">This is the code in the slot ButtonPressed (I defined   Market *m    in “login.h” as private pointer)</p>
<pre><code>    {
        this-&gt;close();
        m=new Market (this);
        m-&gt;show();
    }
</code></pre>
<p dir="auto">The issue I get is when the second window appears and the first disappears, the QApplication seems to close. The window is visible, but for example I don’t see the program in Windows taskbar. I'm sure that there is a better mode to program it.<br />
The same issue I found if I use <strong>this.hide()</strong> or <strong>this.setVisible(false)</strong>;</p>
<p dir="auto">Which is the correct way to implement this?</p>
<p dir="auto">I hope I was clear.<br />
Thank you again. ;)</p>
<p dir="auto">P.S.: I don't want to use a QDialog window or something similar. This problem will occur if I want open a third window and close the second one by pressing a button in the second window..</p>
]]></description><link>https://forum.qt.io/topic/70354/open-new-qmainwindow-and-close-the-old-one-by-keeping-qapplication-alive</link><generator>RSS for Node</generator><lastBuildDate>Sun, 05 Apr 2026 06:06:57 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/70354.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 18 Aug 2016 13:21:55 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to open new QMainWindow and close the old one, by keeping QApplication alive. on Fri, 19 Aug 2016 05:54:46 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/franciscosantiago">@<bdi>FranciscoSantiago</bdi></a> said:</p>
<blockquote>
<p dir="auto">The only abnormal behavior that I see is that when the first window is running, the bar in my Windows taskbar is present, but when the second window runs and the first one is close, I'm not able to see the bar.</p>
</blockquote>
<p dir="auto">try to create <code>m=new Market (this);</code> without a parent. This should give you the icon in the taskbar for this window back.<br />
Also you may want to set the <code>Qt::WA_DeleteOnClose</code> attribute on this window, to make sure it will be deleted once it is closed.</p>
<p dir="auto">So you may want to change your code to this:</p>
<pre><code>{
        this-&gt;close();
        m=new Market;
           m-&gt;setWindowFlags( Qt::Window );
           m-&gt;setAttribute( Qt::WA_DeleteOnClose, true );   //delete itself on closing
           QObject::connect( m, SIGNAL(destroyed(QObject*)), this, SLOT(show()) );  //in case you want to show the login window again once the window is closed
        m-&gt;show();
    }
</code></pre>
<blockquote>
<p dir="auto">and if I reduce to icon the window I'll see a very small icon (maybe the smallest achievable) in the bottom right corner of my screen..</p>
</blockquote>
<p dir="auto">you mean the system tray icon? This has to be explicitly set. This happens in the Market class implementation?</p>
]]></description><link>https://forum.qt.io/post/343118</link><guid isPermaLink="true">https://forum.qt.io/post/343118</guid><dc:creator><![CDATA[raven-worx]]></dc:creator><pubDate>Fri, 19 Aug 2016 05:54:46 GMT</pubDate></item><item><title><![CDATA[Reply to open new QMainWindow and close the old one, by keeping QApplication alive. on Thu, 18 Aug 2016 14:11:12 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kshegunov">@<bdi>kshegunov</bdi></a><br />
this is a very interesting advice.<br />
As soon as I try it, I will inform you.</p>
<p dir="auto">Thank you man! ;)</p>
]]></description><link>https://forum.qt.io/post/343015</link><guid isPermaLink="true">https://forum.qt.io/post/343015</guid><dc:creator><![CDATA[FranciscoSantiago]]></dc:creator><pubDate>Thu, 18 Aug 2016 14:11:12 GMT</pubDate></item><item><title><![CDATA[Reply to open new QMainWindow and close the old one, by keeping QApplication alive. on Thu, 18 Aug 2016 13:55:00 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/raven-worx">@<bdi>raven-worx</bdi></a><br />
Hi raven,<br />
Maybe I was not clear. I'm not sure that the app closes itself. The only abnormal behavior that I see is that when the first window is running, the bar in my Windows taskbar is present, but when the second window runs and the first one is close, I'm not able to see the bar..and if I reduce to icon the window I'll see a very small icon (maybe the smallest achievable) in the bottom right corner of my screen..</p>
<p dir="auto">Thank you anyway</p>
]]></description><link>https://forum.qt.io/post/343010</link><guid isPermaLink="true">https://forum.qt.io/post/343010</guid><dc:creator><![CDATA[FranciscoSantiago]]></dc:creator><pubDate>Thu, 18 Aug 2016 13:55:00 GMT</pubDate></item><item><title><![CDATA[Reply to open new QMainWindow and close the old one, by keeping QApplication alive. on Thu, 18 Aug 2016 13:49:14 GMT]]></title><description><![CDATA[<p dir="auto">Hello <a class="plugin-mentions-user plugin-mentions-a" href="/user/franciscosantiago">@<bdi>FranciscoSantiago</bdi></a> ,<br />
You can set the <a href="http://doc.qt.io/qt-5/qguiapplication.html#quitOnLastWindowClosed-prop" target="_blank" rel="noopener noreferrer nofollow ugc">QGuiApplication::quitOnLastWindowClosed</a> property to false. This way, exec will not return when the main window's closed. However in this case you should also take care to call <a href="http://doc.qt.io/qt-5/qcoreapplication.html#quit" target="_blank" rel="noopener noreferrer nofollow ugc">QCoreApplication::quit</a> when appropriate.</p>
<p dir="auto">Here's an example:</p>
<pre><code>class LoginWindow : public QMainWindow
{
    // ...
signals:
    void loginSuccessful();
    void loginFailed();
}

class MainWindow : public QMainWindow
{
     // ...
signals:
    void closed();

protected:
    void closeEvent(QCloseEvent * event)
    {
        QMainWindow::closeEvent(event);

        if (event-&gt;isAccepted())
            emit closed();
    }
}

int main(int argc, char ** argv)
{
    QApplication app(argc, argv);
    app.setQuitOnLastWindowClosed(false);

    LoginWindow loginWindow;
    MainWindow mainWindow;

    // If login is successful, emit LoginWindow::loginSuccessful
    // This would both close the login window and open the main one
    QObject::connect(&amp;loginWindow, &amp;LoginWindow::loginSuccessful, &amp;loginWindow, &amp;QMainWindow::close);
    QObject::connect(&amp;loginWindow, &amp;LoginWindow::loginSuccessful, &amp;mainWindow, &amp;QMainWindow::show);

    // If login failed, just close the login window
    QObject::connect(&amp;loginWindow, &amp;LoginWindow::loginFailed, &amp;loginWindow, &amp;QMainWindow::close);

    // If login failed, or the main window was closed, quit the application
    // This is needed, because we explicitly set the QGuiApplication::quitOnLastWindowClosed property to false.
    QObject::connect(&amp;mainWindow, &amp;MainWindow::closed, &amp;app, &amp;QCoreApplication::quit);
    QObject::connect(&amp;loginWindow, &amp;LoginWindow::loginFailed, &amp;app, &amp;QCoreApplication::quit);

    login.show();
    return QApplication::exec();
}
</code></pre>
<p dir="auto">Alternatively, you can set the <code>Qt::WA_QuitOnClose</code> attribute to false for your login window. However I advise against it because if you don't handle the closing of the login window properly your application will be left hanging (i.e. running without GUI). At least in the above example the application quitting is made explicit.</p>
<p dir="auto">Kind regards.</p>
]]></description><link>https://forum.qt.io/post/343007</link><guid isPermaLink="true">https://forum.qt.io/post/343007</guid><dc:creator><![CDATA[kshegunov]]></dc:creator><pubDate>Thu, 18 Aug 2016 13:49:14 GMT</pubDate></item><item><title><![CDATA[Reply to open new QMainWindow and close the old one, by keeping QApplication alive. on Thu, 18 Aug 2016 13:42:12 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/franciscosantiago">@<bdi>FranciscoSantiago</bdi></a> said:</p>
<blockquote>
<p dir="auto">The issue I get is when the second window appears and the first disappears, the QApplication seems to close. The window is visible, but for example I don’t see the program in Windows taskbar. I'm sure that there is a better mode to program it.<br />
The same issue I found if I use <strong>this.hide()</strong> or <strong>this.setVisible(false)</strong>;</p>
</blockquote>
<p dir="auto">what drove you to the conclusion that the app closes? Did your executable finished? Meaning that QApp's exec() returns?<br />
This shouldn't happen when you just hide the widgets.<br />
When you close the widgets you need to call <code>setQuitOnLastWindowClosed(false)</code> on the QApp.</p>
]]></description><link>https://forum.qt.io/post/343006</link><guid isPermaLink="true">https://forum.qt.io/post/343006</guid><dc:creator><![CDATA[raven-worx]]></dc:creator><pubDate>Thu, 18 Aug 2016 13:42:12 GMT</pubDate></item><item><title><![CDATA[Reply to open new QMainWindow and close the old one, by keeping QApplication alive. on Thu, 18 Aug 2016 13:27:59 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a><br />
thnak you for your support Ronin,<br />
I've already tried this solution, but the problem was still present..</p>
]]></description><link>https://forum.qt.io/post/343003</link><guid isPermaLink="true">https://forum.qt.io/post/343003</guid><dc:creator><![CDATA[FranciscoSantiago]]></dc:creator><pubDate>Thu, 18 Aug 2016 13:27:59 GMT</pubDate></item><item><title><![CDATA[Reply to open new QMainWindow and close the old one, by keeping QApplication alive. on Thu, 18 Aug 2016 13:24:59 GMT]]></title><description><![CDATA[<pre><code>int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
a.setQuitOnLastWindowClosed(false);
    Login w;
    w.show();
    return a.exec();
}
</code></pre>
<p dir="auto">Then you'll have to call QApplication::quit when you want to actually finish</p>
]]></description><link>https://forum.qt.io/post/343002</link><guid isPermaLink="true">https://forum.qt.io/post/343002</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Thu, 18 Aug 2016 13:24:59 GMT</pubDate></item></channel></rss>