Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Can't show() QWindow when it was hidden with cmd + h on MacOS



  • I have an issue that I cannot raise my app Window when it got hidden with MacOS shortcut. It work correctly in all other cases.

    In my app i have 1 main qWindow called QWindow* mMainWindow; and following code added to tray button

    mMenu->addAction(createAction("Show", [=] {
        if (mMainWindow) {
            mMainWindow->show();
            mMainWindow->raise();
            mMainWindow->requestActivate();
        }
    

    When I just use qt mMainWindow->hide() and then raise it back, mMainWindow works fine. Method mMainWindow->isActive() return correct true state when app is active and false when it is hidden.

    But when I hide app using build-in in mac "cmd + h", mMainWindow->isActive() return true regardless if app app is hidden or not. Calling my action item does nothing, mMainWindow stay all the time hidden.

    Is there any solution to fix this issue? I have seen people recommend using QWidget instead of QWindow and calling widget->activateWindow() but it is not solution i can use in my case.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Which version of Qt are you using ?
    On which version of macOS ?
    Can you provide a minimal compilable example that shows that behaviour ?



  • Which version of Qt are you using ? 5.15.0
    On which version of macOS ? Mojave
    Can you provide a minimal compilable example that shows that behaviour ?

    Here is quick example of the problem (mine tray is written in cpp, but i can see that problem also appear when using qml one).

    Click Hide then Show and it work fine.
    Use "cmd + h" then Show and it doesnt.

    import QtQuick 2.15
    import QtQuick.Window 2.15
    import Qt.labs.platform 1.1
    
    Window {
        id: window
        visible: true
        width: 640
        height: 480
    
        SystemTrayIcon {
            visible: true
    
            menu: Menu {
                MenuItem {
                    text: qsTr("Show")
                    onTriggered: {window.show(); window.raise(); }
                }
                MenuItem {
                    text: qsTr("Hide")
                    onTriggered: window.hide()
                }
            }
        }
    
    }
    

    After writing this example i have realised that if you call Hide after Window was hidden with Mac shortcut, Show will work. (didn't realise sooner because my app has no Hide button)
    So looks like i will just add to my show button extra hide() call as a work-around to this bug and my code will looks like this.

        mMenu->addAction(createAction("Show", [=] {
            if (mMainWindow) {
                mMainWindow->hide();
                mMainWindow->show();
                mMainWindow->raise();
                mMainWindow->requestActivate();
            }
        }));
    

  • Lifetime Qt Champion

    I currently do not know whether the CMD+H action provides any feedback to the application. Your workaround was what came to mind. Glad it is working.


Log in to reply