Nominate our 2022 Qt Champions!

Hide & Show QWidget in closeEvent with Keyboard key

  • Hello,

    My goal is to let the user decide in the closeEvent whether to hide the window or not. And if with window is hidden the window can be shown using a keyboard shortcut again.

    What I've done so far:


    void closeEvent(QCloseEvent *event);


    void Widget::closeEvent(QCloseEvent *event)
    QMessageBox::StandardButton dialog;
    dialog = QMessageBox::warning(this, "Close?",
    "Do you want to close the Window?\nElse it will be hidden!\nShow using INSERT key",
    QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
    if (dialog == QMessageBox::Yes)
    else if (dialog == QMessageBox::No)

    Now I'm trying to find a way to show the window again using a keyboard hook...

    Thanks in advance

  • Lifetime Qt Champion


    Do you mean you are hiding the main window of your application or a sub-window ?

  • Hey,

    I'm hiding the MainWindow of my application. Hiding the Window is no problem but showing it again with a keyinput is the problem...

  • Lifetime Qt Champion

    Do you want that from another part of your application ? If so you can use e.g. QShortcut

  • Actually I have just one Window and in the closeEvent and I want the user to decide whether to close or hide it which works fine...
    But once the window is hidden there is no other window opened at that time. So in order to show it back I want to use a keyboard shortcut like "INSERT". I've already tried KeyEvent but it only works if the window is visible... I hope it's more clear now

  • Lifetime Qt Champion

    Are you using a QSystemTrayIcon so that user has a possibility to get the window back ?

  • Thanks you so much for the idea :D The problem is solved, I simply implemented a QSystemTrayIcon :)!

  • Lifetime Qt Champion

    You're welcome !

    As for marking the thread as solved, you can now use the "Topic Tool" button, it will keep the title clean.

    Also, while browsing the forum, please consider up-voting answers that helped you, that will make them easier to find for other forum users :)