PDA

View Full Version : MainWindow disappearing when loading a QMessageBox



fabietto
24th June 2008, 18:47
Dear all,

I'm experiencing a strange issue, never happened to me before.

In a class of my application I've created a simple SIGNAL-SLOT mechanism to manage the pressing of the "Exit" button by the user:


// Exit buttons
QObject::connect(mainWindow->pushButton_Exit, SIGNAL(released()), this, SLOT(exitApplication()));
QObject::connect(this, SIGNAL(confirmClosing()), qApp, SLOT(quit()));

mainWindow is obviously the main window of my application, defined as a member of a class created through the designer (and then made child of QMainWindow).

The exitApplication() slot provides to prompt the user with a QMessageBox, asking him to confirm the exiting:


// Show a dialog box asking confirmation to the user
int reply;
reply = QMessageBox::warning(this, tr("Application exit"),
tr("An evolution is in progress.\nAre you really sure you want to exit now?"),
QMessageBox::No | QMessageBox::Default, QMessageBox::Yes |
QMessageBox::Escape);

(the code continue with something like: if yes then ::exit(0))

The problem is that, when this QMessageBox appears, the application's main window disappears. Even if I press "No" on the dialog, the main window doesn't re-appears. The application still runs.

Any clue about the reason of this behaviour?

Many thanks,
Fabio

estanisgeyer
24th June 2008, 19:22
Please, see Event Close Window (http://doc.trolltech.com/4.0/qwidget.html#closeEvent). The example is found in MainWindow examples and demos.

Marcelo Geyer
Brazil.

aamer4yu
24th June 2008, 19:26
Can u show the code of the exitApplication slot ?

fabietto
24th June 2008, 21:22
Please, see Event Close Window (http://doc.trolltech.com/4.0/qwidget.html#closeEvent). The example is found in MainWindow examples and demos.

Marcelo Geyer
Brazil.

Thanks for the link Marcelo, but if possible I would like to use my mechanism, which had already provided to be working in a different application.

Cheers,
Fabio

fabietto
24th June 2008, 21:23
Can u show the code of the exitApplication slot ?

Here it is:


// Exit application SLOT
void Simulator::exitApplication() {

// Check if an evolution or a test are in progress
if ((evolutionInProgress) || (testInProgress)) {

// Pause the simulation
mainWindow->checkBox_PauseEvolution->setChecked(true);

// Show a dialog box asking confirmation to the user
int reply;
if (evolutionInProgress) reply = QMessageBox::warning(this, tr("Application exit"),
tr("An evolution is in progress.\nAre you really sure you want to exit now?"),
QMessageBox::No | QMessageBox::Default, QMessageBox::Yes | QMessageBox::Escape);
else reply = QMessageBox::warning(this, tr("Application exit"),
tr("A test is in progress.\nAre you really sure you want to exit now?"),
QMessageBox::No | QMessageBox::Default, QMessageBox::Yes | QMessageBox::Escape);

// Check the answer provided by the user (exit or not)
if (reply == QMessageBox::Yes) {

// If an evolution is running, ask to the user if he'd like to save the population
if ((currentGeneration != 0) && (evolutionInProgress)) {
int answer = QMessageBox::question(this,
tr("Save current population"), tr("Do you want to save the current population, in order to restore the simulation later?"),
QMessageBox::Yes | QMessageBox::Default, QMessageBox::No | QMessageBox::Escape);
if (answer == QMessageBox::Yes) {
savePopulation();
QString directoryName;
directoryName = seedDirectory + "/Gen_" + QString::number(currentGeneration);
QMessageBox::information(this,
tr("Population saved"), tr("The population has been saved into the directory:\n%1").arg(directoryName));
}
}

// Quit the application
::exit(0);

} else {

// Return to the simulation
mainWindow->checkBox_PauseEvolution->setChecked(false);

}

} else { // If no simulations or tests are in progress, directly quit the application

emit confirmClosing();

}

}


Many thanks for your help. It's really appreciated!

Cheers,
Fabio

PS: sorry but I had few very long lines that I had to copy here inserting few returns.

jacek
24th June 2008, 21:57
The problem is that, when this QMessageBox appears, the application's main window disappears. Even if I press "No" on the dialog, the main window doesn't re-appears.
What happens when you comment out all of the code in Simulator::exitApplication() and click the exit button?

fabietto
24th June 2008, 22:47
What happens when you comment out all of the code in Simulator::exitApplication() and click the exit button?

The application quits! :eek:

marcel
24th June 2008, 22:54
Yes, probably an access violation.
Do you get any runtime debug warning messages from connect?
Try running the debug version from a console.

fabietto
24th June 2008, 22:58
Yes, probably an access violation.
Do you get any runtime debug warning messages from connect?
Try running the debug version from a console.

No, no... it quits correctly!

Maybe that since I first created the main window through the designer the exit button I'm using has got some particular characteristics? :confused:

jacek
24th June 2008, 23:16
the exit button I'm using has got some particular characteristics? :confused:
Yes, it might be connected to some slot that closes the window.

fabietto
24th June 2008, 23:29
Yes, it might be connected to some slot that closes the window.

It is! I was missing a line in the .h file generated via Qt Designer/UIC, which was implementing exactly what you said. I removed that line and now everything works smoothly. and as expected.

Thank you very much to all of you! :)

Cheers,
Fabio

jacek
24th June 2008, 23:33
It is! I was missing a line in the .h file generated via Qt Designer/UIC, which was implementing exactly what you said. I removed that line and now everything works smoothly. and as expected.
Don't remove any lines from autogenerated code or the problem will come back. Better fix the .ui file.

fabietto
25th June 2008, 08:45
Don't remove any lines from autogenerated code or the problem will come back. Better fix the .ui file.

Sure, Jacek. I've used the Qt Designer only at the beginning in order to create the skeleton of the main windows. Now I don't generate the .h file automatically, but I simply use a progressively modified version of it.

Cheers,
Fabio

pherthyl
25th June 2008, 20:20
That's an interesting way to use Designer.. :)

fabietto
25th June 2008, 23:41
That's an interesting way to use Designer.. :)

Probably not the most intelligent, I know... but it's so handy... :o