Showing and hiding multiple dialogs
-
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(); }
-
@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. -
@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
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? -
@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.
-
@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. -
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.
-
@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.
-
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.
-
@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