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. Showing and hiding multiple dialogs
Forum Update on Monday, May 27th 2025

Showing and hiding multiple dialogs

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 1.8k Views
  • 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.
  • A Offline
    A Offline
    Akllu
    wrote on last edited by
    #1

    Hello!
    I am quite new to programming and I ran to a problem when executing multiple dialogs.

    So I'm trying to make an application where the mainwindow (this is like a welcome screen) is shown immediately after running the program, when I press a pushbutton, it hides the mainwindow and executes another dialog by pointer. That second dialog is my main menu where user can interact with different buttons. So if I'm executing my main menu and press one function, it hides the main menu and executes the chosen function and when the user is done with that function, he closes it with a button and it comes back to the main menu. So this main menu event loop will be executed as long as the user presses the logout button and then it should bring up the original mainwindow (welcome screen). So this should be infinite loop.

    My problem here is that when I close any of my dialogs inside my main menu, it emittes the quit() signal which should close the chosen dialog and bring up the main menu again, which it does, but it also brings up the mainwindow.

    I would appreciate any help, I have been stuck for this problem for a few days, what should I do to close the function dialog and show only the main menu, not both mainmenu and mainwindow? And also, is this a bad way to create programs like this, creating event loops inside event loop? Is there a better solution to develop these type of programs, I tried to read Qt documentations and found the open() function, but didn't know how to use that :D

    MainWindow.cpp

    void MainWindow::on_pushButton_clicked()
    {
        this->hide();
        ptrMainDialog->exec();
        this->show();
    }
    

    MainDialog.cpp

    void MainDialog::on_balanceButton_clicked()
    {
        this->hide();
        ptrBalance->exec();
        this->show();
    }
    
    void MainDialog::on_logoutButton_clicked()
    {
         this->close();
    }
    

    balanceDialog.cpp

    void balanceDialog::on_closeButton_clicked()
    {
        this->close();
    }
    
    jsulmJ 1 Reply Last reply
    0
    • A Akllu

      Hello!
      I am quite new to programming and I ran to a problem when executing multiple dialogs.

      So I'm trying to make an application where the mainwindow (this is like a welcome screen) is shown immediately after running the program, when I press a pushbutton, it hides the mainwindow and executes another dialog by pointer. That second dialog is my main menu where user can interact with different buttons. So if I'm executing my main menu and press one function, it hides the main menu and executes the chosen function and when the user is done with that function, he closes it with a button and it comes back to the main menu. So this main menu event loop will be executed as long as the user presses the logout button and then it should bring up the original mainwindow (welcome screen). So this should be infinite loop.

      My problem here is that when I close any of my dialogs inside my main menu, it emittes the quit() signal which should close the chosen dialog and bring up the main menu again, which it does, but it also brings up the mainwindow.

      I would appreciate any help, I have been stuck for this problem for a few days, what should I do to close the function dialog and show only the main menu, not both mainmenu and mainwindow? And also, is this a bad way to create programs like this, creating event loops inside event loop? Is there a better solution to develop these type of programs, I tried to read Qt documentations and found the open() function, but didn't know how to use that :D

      MainWindow.cpp

      void MainWindow::on_pushButton_clicked()
      {
          this->hide();
          ptrMainDialog->exec();
          this->show();
      }
      

      MainDialog.cpp

      void MainDialog::on_balanceButton_clicked()
      {
          this->hide();
          ptrBalance->exec();
          this->show();
      }
      
      void MainDialog::on_logoutButton_clicked()
      {
           this->close();
      }
      

      balanceDialog.cpp

      void balanceDialog::on_closeButton_clicked()
      {
          this->close();
      }
      
      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Akllu said in Showing and hiding multiple dialogs:

      And also, is this a bad way to create programs like this, creating event loops inside event loop?

      Yes.
      You should call show() instead of exec() and call show() on the previous dialog as soon as it needs to be shown again.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      1
      • jsulmJ jsulm

        @Akllu said in Showing and hiding multiple dialogs:

        And also, is this a bad way to create programs like this, creating event loops inside event loop?

        Yes.
        You should call show() instead of exec() and call show() on the previous dialog as soon as it needs to be shown again.

        A Offline
        A Offline
        Akllu
        wrote on last edited by
        #3

        @jsulm
        Thank you for your reply. :)
        You meant like this?
        So I need to create signals and connect them to show() the dialog what I want? Is that the correct way?

        MainWindow.cpp

        void MainWindow::on_pushButton_clicked()
        {
            this->hide();  //Hides the MainWindow
            ptrMainDialog->show();  //Shows the main menu
        }
        

        MainDialog.cpp

        void MainDialog::on_balanceButton_clicked()
        {
            this->hide();  //Hides the main menu
            ptrBalance->show();  //Shows the balance dialog
        }
        

        balanceDialog.cpp

        void WithdrawDialog::on_buttonClose_clicked()
        {
            this->close();  //Closes the balance dialog
            //emit signalhere?
        }
        
        jsulmJ 1 Reply Last reply
        0
        • A Akllu

          @jsulm
          Thank you for your reply. :)
          You meant like this?
          So I need to create signals and connect them to show() the dialog what I want? Is that the correct way?

          MainWindow.cpp

          void MainWindow::on_pushButton_clicked()
          {
              this->hide();  //Hides the MainWindow
              ptrMainDialog->show();  //Shows the main menu
          }
          

          MainDialog.cpp

          void MainDialog::on_balanceButton_clicked()
          {
              this->hide();  //Hides the main menu
              ptrBalance->show();  //Shows the balance dialog
          }
          

          balanceDialog.cpp

          void WithdrawDialog::on_buttonClose_clicked()
          {
              this->close();  //Closes the balance dialog
              //emit signalhere?
          }
          
          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Akllu Something like this, yes

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          A 1 Reply Last reply
          1
          • jsulmJ jsulm

            @Akllu Something like this, yes

            A Offline
            A Offline
            Akllu
            wrote on last edited by
            #5

            @jsulm
            This signal/slot works for showing dialogs between balance etc. and main menu. But if I connect the main menu logout button signal to MainWindow show() function, it doesn't show that anymore and quits the event loop. How can I show the MainWindow again when I close the main menu?

            jsulmJ 1 Reply Last reply
            0
            • A Akllu

              @jsulm
              This signal/slot works for showing dialogs between balance etc. and main menu. But if I connect the main menu logout button signal to MainWindow show() function, it doesn't show that anymore and quits the event loop. How can I show the MainWindow again when I close the main menu?

              jsulmJ Online
              jsulmJ Online
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Akllu said in Showing and hiding multiple dialogs:

              and quits the event loop

              How can it quit the event loop? Please show your current code.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              A 1 Reply Last reply
              0
              • jsulmJ jsulm

                @Akllu said in Showing and hiding multiple dialogs:

                and quits the event loop

                How can it quit the event loop? Please show your current code.

                A Offline
                A Offline
                Akllu
                wrote on last edited by
                #7

                @jsulm
                Well I didn't mention I have a dialog between the mainwindow and main menu, where I ask for PIN-code and if the password is correct, it shows the main menu dialog from inside the PIN-dialog by ptrMainDialog->show(). And then I tried to connect a new pointer of MainDialog inside MainWindow to show() function, which was emitted by the main menus logout button. So I think that's the problem here :D I really appreciate your help, I'll try to change my code.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi and welcome to devnet,

                  From the looks of it, you are doing a lot of back and forth with different pieces of your UI which is not ideal.

                  I would recommend to first draw the UI you want as well as the sequences of actions.

                  There's a mix between a starting phase, maybe a configuration phase, normal usage, etc. It's pretty rarely needed to do that much hide and show on an application main window.

                  Define exactly what must be done at startup, it might be mandatory to have that done before showing your main window. Then ensure that all interactions makes sense with hiding the main window. Maybe using a modal dialog might be enough and would be simpler to handle.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  A 1 Reply Last reply
                  1
                  • SGaistS SGaist

                    Hi and welcome to devnet,

                    From the looks of it, you are doing a lot of back and forth with different pieces of your UI which is not ideal.

                    I would recommend to first draw the UI you want as well as the sequences of actions.

                    There's a mix between a starting phase, maybe a configuration phase, normal usage, etc. It's pretty rarely needed to do that much hide and show on an application main window.

                    Define exactly what must be done at startup, it might be mandatory to have that done before showing your main window. Then ensure that all interactions makes sense with hiding the main window. Maybe using a modal dialog might be enough and would be simpler to handle.

                    A Offline
                    A Offline
                    Akllu
                    wrote on last edited by
                    #9

                    @SGaist
                    Thank you. :)

                    This is my first school project with Qt and the topic is a virtual ATM. The idea is to make an application where this software is running in infinite loop. So if the user is not interacting with the ATM, the main window is shown (welcome screen which tells to read a card). When the card is read, it should close/hide the main window and ask for a PIN-code in second dialog. After the user has given the correct password, it should close the PIN-dialog and open the next dialog (main menu). In the main menu there are few different functionalitys like in a real ATM (Show balance, Withdrawal, Log out etc..) When any of these is clicked, it should close/hide the main menu and bring up a new dialog again and after using that functionality, it should bring back the main menu. When the user presses log out button, it should start over again.

                    I hope that was well enough explained to know what's this all about. I'm sorry for grammar mistakes. So I understand that the back and forth showing hiding is not the common way. I also read the modal dialog document, which says "The most common way to display a modal dialog is to call its exec() function." And that's why I used the exec() function at first.

                    J.HilkJ 1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Ok, that's clearer now.

                      So you exactly do not want to have that many opening and closing happening.

                      Take a look at the QStackedWidget as well as the State Machine Framework.

                      You should properly model your application using the state machine. Each state will then show the corresponding widget and do not forget to cleanup every time since it's an ATM and you do not want to show anything that does not belong to the user unintentionally.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      A 1 Reply Last reply
                      1
                      • A Akllu

                        @SGaist
                        Thank you. :)

                        This is my first school project with Qt and the topic is a virtual ATM. The idea is to make an application where this software is running in infinite loop. So if the user is not interacting with the ATM, the main window is shown (welcome screen which tells to read a card). When the card is read, it should close/hide the main window and ask for a PIN-code in second dialog. After the user has given the correct password, it should close the PIN-dialog and open the next dialog (main menu). In the main menu there are few different functionalitys like in a real ATM (Show balance, Withdrawal, Log out etc..) When any of these is clicked, it should close/hide the main menu and bring up a new dialog again and after using that functionality, it should bring back the main menu. When the user presses log out button, it should start over again.

                        I hope that was well enough explained to know what's this all about. I'm sorry for grammar mistakes. So I understand that the back and forth showing hiding is not the common way. I also read the modal dialog document, which says "The most common way to display a modal dialog is to call its exec() function." And that's why I used the exec() function at first.

                        J.HilkJ Online
                        J.HilkJ Online
                        J.Hilk
                        Moderators
                        wrote on last edited by
                        #11

                        @Akllu so, question:

                        Does it have to be a new Window (with close min/max button etc) each and every time ?
                        Because usually something like this is done with a single window and something like a QStackWidget as its center to show different ui's


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        1 Reply Last reply
                        2
                        • SGaistS SGaist

                          Ok, that's clearer now.

                          So you exactly do not want to have that many opening and closing happening.

                          Take a look at the QStackedWidget as well as the State Machine Framework.

                          You should properly model your application using the state machine. Each state will then show the corresponding widget and do not forget to cleanup every time since it's an ATM and you do not want to show anything that does not belong to the user unintentionally.

                          A Offline
                          A Offline
                          Akllu
                          wrote on last edited by
                          #12

                          @SGaist @J-Hilk
                          Thank you both!
                          I didn't know about the QStackWidget, I'll take a look and try to create it with that.

                          1 Reply Last reply
                          1

                          • Login

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