DEKKER
DEKKER

Reputation: 921

Selecting different QMainWindow at program startup but facing strange QMessageBox exec() behavior

I am working on a simple Qt Widget application where I have 2 different QMainWindow classes to show to user (at the moment only one is implemented).

My idea is to show a message box with custom buttons to ask the user which mode of the program to be executed.

I came up with the code below, but I am facing strange problems. You can try this code easily if you make a simple qt widget project.

So the problem is, the message box is being shown and the buttons are shown correctly. When the user selects "Debug Mode" the correct MainWindow will show up for a fraction of a second and then vanishes! but the program stays open and the return will not be reached!

For the "Operation Mode" the critical message box is shown but when user clicks ok all the message boxes go away but again return code is not reached!

Same happens for the "Exit" option...

#include "mainwindow.h"
#include <QApplication>

#include <QMessageBox>
#include <QAbstractButton>
#include <QPushButton>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    // At the start of the progam ask user which mode to show
    QMessageBox msgBoxModeSelection;
    msgBoxModeSelection.setIcon(QMessageBox::Question);
    msgBoxModeSelection.setText("Please select the program mode:");

    QAbstractButton* btnModeDebug = msgBoxModeSelection.addButton(
                                    "Debug Mode", QMessageBox::YesRole);
    QAbstractButton* btnModeOperation = msgBoxModeSelection.addButton(
                                    "Operation Mode", QMessageBox::NoRole);
    QAbstractButton* btnModeExit = msgBoxModeSelection.addButton(
                                    "Exit", QMessageBox::RejectRole);

    msgBoxModeSelection.exec();

    // Check which mode is being selected by user and continue accordingly
    if(msgBoxModeSelection.clickedButton() == btnModeDebug) {
        MainWindow w;
        w.show();
    } else if(msgBoxModeSelection.clickedButton() == btnModeOperation){ // Operation Mode
        //TODO implement...for now just inform user that it is not implemented
        QMessageBox::critical(nullptr, "Error", "Operation Mode is not yet implemented");
        return a.exec();
    } else if(msgBoxModeSelection.clickedButton() == btnModeExit){ // Just exit
        // Just exit the program
        QMessageBox::critical(nullptr, "Goodbye!", "Sorry to see you go :(");
        return a.exec();
    }

    return a.exec();
}

So basically the program vanishes but it is still somehow open and is being processed. The only way to terminate it is to stop the debugger or kill its process from the operating system.

So I want the correct form to be shown as in there were no messageBox involved and the return code and exit of the program be normal again!

Upvotes: 0

Views: 90

Answers (1)

taminob
taminob

Reputation: 259

Your MainWindow only exists inside the if statement. So it is destructed as soon as it shows up and leaves its scope.

Change it to e.g.:

MainWindow w;
if(msgBoxModeSelection.clickedButton() == btnModeDebug) {
    w.show();
}

or

if(msgBoxModeSelection.clickedButton() == btnModeDebug) {
    MainWindow w;
    w.show();
    return a.exec();
}

Upvotes: 2

Related Questions