Alex Rivera | Logout

Window is hidden even though QCloseEvent is ignored

Asked 2013-02-01T15:29:17.213
12

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.

Edit
Report

1 Answer

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.

answered 2013-02-05T20:16:46.713

Your Answer