Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. open new QMainWindow and close the old one, by keeping QApplication alive.
Forum Updated to NodeBB v4.3 + New Features

open new QMainWindow and close the old one, by keeping QApplication alive.

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 6.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • FranciscoSantiagoF Offline
    FranciscoSantiagoF Offline
    FranciscoSantiago
    wrote on last edited by FranciscoSantiago
    #1

    Hi all,
    I’m quite new in Qt world.. so thank you in advance to everyone will help me.
    The question is simple:
    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.
    I have a “login” QMainWindow with a button. When this button is pressed, the login window should disappear and a new QMainWindow appears.

    So, this is the main:

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Login w;
        w.show();
        return a.exec();
    }
    

    This is the code in the slot ButtonPressed (I defined Market *m in “login.h” as private pointer)

        {
            this->close();
            m=new Market (this);
            m->show();
        }
    

    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.
    The same issue I found if I use this.hide() or this.setVisible(false);

    Which is the correct way to implement this?

    I hope I was clear.
    Thank you again. ;)

    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..

    raven-worxR kshegunovK 2 Replies Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      a.setQuitOnLastWindowClosed(false);
          Login w;
          w.show();
          return a.exec();
      }
      

      Then you'll have to call QApplication::quit when you want to actually finish

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      FranciscoSantiagoF 1 Reply Last reply
      2
      • VRoninV VRonin
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
        a.setQuitOnLastWindowClosed(false);
            Login w;
            w.show();
            return a.exec();
        }
        

        Then you'll have to call QApplication::quit when you want to actually finish

        FranciscoSantiagoF Offline
        FranciscoSantiagoF Offline
        FranciscoSantiago
        wrote on last edited by FranciscoSantiago
        #3

        @VRonin
        thnak you for your support Ronin,
        I've already tried this solution, but the problem was still present..

        1 Reply Last reply
        0
        • FranciscoSantiagoF FranciscoSantiago

          Hi all,
          I’m quite new in Qt world.. so thank you in advance to everyone will help me.
          The question is simple:
          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.
          I have a “login” QMainWindow with a button. When this button is pressed, the login window should disappear and a new QMainWindow appears.

          So, this is the main:

          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
              Login w;
              w.show();
              return a.exec();
          }
          

          This is the code in the slot ButtonPressed (I defined Market *m in “login.h” as private pointer)

              {
                  this->close();
                  m=new Market (this);
                  m->show();
              }
          

          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.
          The same issue I found if I use this.hide() or this.setVisible(false);

          Which is the correct way to implement this?

          I hope I was clear.
          Thank you again. ;)

          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..

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @FranciscoSantiago said:

          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.
          The same issue I found if I use this.hide() or this.setVisible(false);

          what drove you to the conclusion that the app closes? Did your executable finished? Meaning that QApp's exec() returns?
          This shouldn't happen when you just hide the widgets.
          When you close the widgets you need to call setQuitOnLastWindowClosed(false) on the QApp.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          FranciscoSantiagoF 1 Reply Last reply
          0
          • FranciscoSantiagoF FranciscoSantiago

            Hi all,
            I’m quite new in Qt world.. so thank you in advance to everyone will help me.
            The question is simple:
            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.
            I have a “login” QMainWindow with a button. When this button is pressed, the login window should disappear and a new QMainWindow appears.

            So, this is the main:

            int main(int argc, char *argv[])
            {
                QApplication a(argc, argv);
                Login w;
                w.show();
                return a.exec();
            }
            

            This is the code in the slot ButtonPressed (I defined Market *m in “login.h” as private pointer)

                {
                    this->close();
                    m=new Market (this);
                    m->show();
                }
            

            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.
            The same issue I found if I use this.hide() or this.setVisible(false);

            Which is the correct way to implement this?

            I hope I was clear.
            Thank you again. ;)

            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..

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #5

            Hello @FranciscoSantiago ,
            You can set the QGuiApplication::quitOnLastWindowClosed 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 QCoreApplication::quit when appropriate.

            Here's an example:

            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->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(&loginWindow, &LoginWindow::loginSuccessful, &loginWindow, &QMainWindow::close);
                QObject::connect(&loginWindow, &LoginWindow::loginSuccessful, &mainWindow, &QMainWindow::show);
            
                // If login failed, just close the login window
                QObject::connect(&loginWindow, &LoginWindow::loginFailed, &loginWindow, &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(&mainWindow, &MainWindow::closed, &app, &QCoreApplication::quit);
                QObject::connect(&loginWindow, &LoginWindow::loginFailed, &app, &QCoreApplication::quit);
            
                login.show();
                return QApplication::exec();
            }
            

            Alternatively, you can set the Qt::WA_QuitOnClose 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.

            Kind regards.

            Read and abide by the Qt Code of Conduct

            FranciscoSantiagoF 1 Reply Last reply
            1
            • raven-worxR raven-worx

              @FranciscoSantiago said:

              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.
              The same issue I found if I use this.hide() or this.setVisible(false);

              what drove you to the conclusion that the app closes? Did your executable finished? Meaning that QApp's exec() returns?
              This shouldn't happen when you just hide the widgets.
              When you close the widgets you need to call setQuitOnLastWindowClosed(false) on the QApp.

              FranciscoSantiagoF Offline
              FranciscoSantiagoF Offline
              FranciscoSantiago
              wrote on last edited by
              #6

              @raven-worx
              Hi raven,
              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..

              Thank you anyway

              raven-worxR 1 Reply Last reply
              0
              • kshegunovK kshegunov

                Hello @FranciscoSantiago ,
                You can set the QGuiApplication::quitOnLastWindowClosed 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 QCoreApplication::quit when appropriate.

                Here's an example:

                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->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(&loginWindow, &LoginWindow::loginSuccessful, &loginWindow, &QMainWindow::close);
                    QObject::connect(&loginWindow, &LoginWindow::loginSuccessful, &mainWindow, &QMainWindow::show);
                
                    // If login failed, just close the login window
                    QObject::connect(&loginWindow, &LoginWindow::loginFailed, &loginWindow, &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(&mainWindow, &MainWindow::closed, &app, &QCoreApplication::quit);
                    QObject::connect(&loginWindow, &LoginWindow::loginFailed, &app, &QCoreApplication::quit);
                
                    login.show();
                    return QApplication::exec();
                }
                

                Alternatively, you can set the Qt::WA_QuitOnClose 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.

                Kind regards.

                FranciscoSantiagoF Offline
                FranciscoSantiagoF Offline
                FranciscoSantiago
                wrote on last edited by
                #7

                @kshegunov
                this is a very interesting advice.
                As soon as I try it, I will inform you.

                Thank you man! ;)

                1 Reply Last reply
                0
                • FranciscoSantiagoF FranciscoSantiago

                  @raven-worx
                  Hi raven,
                  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..

                  Thank you anyway

                  raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by raven-worx
                  #8

                  @FranciscoSantiago said:

                  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.

                  try to create m=new Market (this); without a parent. This should give you the icon in the taskbar for this window back.
                  Also you may want to set the Qt::WA_DeleteOnClose attribute on this window, to make sure it will be deleted once it is closed.

                  So you may want to change your code to this:

                  {
                          this->close();
                          m=new Market;
                             m->setWindowFlags( Qt::Window );
                             m->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->show();
                      }
                  

                  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..

                  you mean the system tray icon? This has to be explicitly set. This happens in the Market class implementation?

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved