I’m currently trying to port my Qt application to Mac OSX and just came across the following problem:
One of my window classes (derived from QMainWindow) displays a dialog box when the close button is pressed, where you can save/discard changes or abort. This is done by reimplementing closeEvent as follows:
void ResultsWindow::closeEvent(QCloseEvent* event)
{
if (isWindowModified())
{
activateWindow();
QMessageBox::StandardButton button = QMessageBox::warning(
this,
APPLICATION_NAME,
"Warning: data have been modified. Do you want to save the "
"changes?",
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Abort,
QMessageBox::Abort);
switch (button)
{
case QMessageBox::Save:
if (!Save())
{
event->ignore();
return;
}
break;
case QMessageBox::Discard:
break;
case QMessageBox::Abort:
default:
event->ignore();
return;
}
}
event->accept();
}
The expected behavior is as follows: If you click “Abort”, the event is ignored and the window stays open. This works as expected on Linux and Windows. On OSX, however, the window is hidden, but still active in the background. What am I missing here? How can I make sure the window stays open?
Update:
I somehow think that this is a problem with Qt5. If the closeEvent
method consists of nothing but event->ignore();
the window is still closed. I don’t think this is the intended behavior. Maybe I should post this problem to the Qt mailing list…
Update: This was a bug in Qt and is solved since version 5.1.
I have Qt 4.7 on Mac 10.7 and I can't reproduce this error.
I believe simply putting show()
between the Q_OS_MAC
macro every time you ignore the event
should do the trick. Something like this:
event->ignore();
#ifdef Q_OS_MAC
show();
#endif
This may not be the perfect solution, but certainly a fix for this problem.
Same here with Qt 5.0.0 or Qt 5.0.1. However if you set WindowModality as ApplicationModal or WindowModal it works. Press Cmd+Q to quit and to obtain the message box.
I presume the problem is originated by the differences between Mac OS X UI and other UI. Perhaps some additional work on Qt will enable them to resolve the problem.
Even if you reimplement QMainWindow::event() you obtain the same result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With