Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Window is hidden even though QCloseEvent is ignored

Tags:

c++

macos

qt

qt5

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.

like image 420
AbuBakr Avatar asked Feb 01 '13 15:02

AbuBakr


2 Answers

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.

like image 134
zeFree Avatar answered Nov 10 '22 15:11

zeFree


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.

windowModality in Qt Creator

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.

like image 1
asclepix Avatar answered Nov 10 '22 16:11

asclepix