Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Showing and hiding multiple dialogs

    General and Desktop
    4
    12
    468
    Loading More Posts
    • 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
      Akllu last edited by

      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();
      }
      
      jsulm 1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @Akllu last edited by

        @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 Reply Quote 1
        • A
          Akllu @jsulm last edited by

          @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?
          }
          
          jsulm 1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion @Akllu last edited by

            @Akllu Something like this, yes

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

            A 1 Reply Last reply Reply Quote 1
            • A
              Akllu @jsulm last edited by

              @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?

              jsulm 1 Reply Last reply Reply Quote 0
              • jsulm
                jsulm Lifetime Qt Champion @Akllu last edited by

                @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 Reply Quote 0
                • A
                  Akllu @jsulm last edited by

                  @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 Reply Quote 0
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

                    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 Reply Quote 1
                    • A
                      Akllu @SGaist last edited by

                      @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.Hilk 1 Reply Last reply Reply Quote 0
                      • SGaist
                        SGaist Lifetime Qt Champion last edited by

                        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 Reply Quote 1
                        • J.Hilk
                          J.Hilk Moderators @Akllu last edited by

                          @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

                          Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

                          1 Reply Last reply Reply Quote 2
                          • A
                            Akllu @SGaist last edited by

                            @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 Reply Quote 1
                            • First post
                              Last post