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

Disable close for fullscreen window on Windows ?



  • Hi, I have a fullscreen window that the user should not be able to close using alt+f4 on a keyboard. I have tried the following 2 options:

    Option 1: Disable window close button.

    myWindow->setWindowFlags(windowFlags() &= ~Qt::WindowCloseButtonHint);
    

    This works fine on Ubuntu 16.04 but for some reason it does not work on WIndows ?

    Option 2: Overwrite closeEvent()

    void MyWindow::closeEvent (QCloseEvent *event)
    {
        event->ignore();
    }
    

    This works, however I need to call this->close() on certain events to close the window and overwriting closeEvent() stops this from working.

    Disabling the close button works fine for me except that it doesn't work on Windows ? Can this be fixed or are there any other options ? Thanks.


  • Moderators

    @R-P-H said in Disable close for fullscreen window on Windows ?:

    Would you say there's a more popular way of achieving this rather than adding a flag to all my classes ?

    It's such a small problem I don't think there's any established pattern for this. The gist of it is that you want closeEvent to behave differently depending on the trigger for it so a flag to indicate that seems reasonable.

    If there's more than one such class you have you can make a base class with that functionality so that you don't copy/paste it everywhere. You could give it a custom closeInternal() method or something that you'd use from code and it would set the flag and then call close().

    If you don't want to add extra flag to your class you can use the built in dynamic properties of QObject and do something like this:

    void MyWindow::reallyClose()
    {
       setProperty("really_close", true);
       close();
    }
    
    void MyWindow::closeEvent(QCloseEvent* event)
    {
       if (!property("really_close").toBool())
           event->ignore();
    }
    

  • Moderators

    This works fine on Ubuntu 16.04 but for some reason it does not work on WIndows ?

    This is window manager specific. It might work on one and not on another, as you observed. It might not even work on another Linux system with different window manager.

    This works, however I need to call this->close() on certain events to close the window and overwriting closeEvent() stops this from working.

    Just add a boolean flag to your class indicating it's an internal call of yours and check it in the closeEvent.

    Btw. if you're making some sort of Kiosk application keep in mind that close button and system shortcut are just two of many many other ways to close a window so that's not even the minimum you need to do.



  • @Chris-Kawa Thanks for that ! Would you say there's a more popular way of achieving this rather than adding a flag to all my classes ?



  • If you want to remove all the buttons (also minimize) on top you could also remove the entire TitleBar, by setWindowFlags() to frameless window.
    However as Chris Kawa already mentioned the user will still be able to close the app by e.g. Alt+Tab to the Desktop and close it from the taskbar or by using the task manager or ...



  • @gde23 Yes I understand, thanks. That won't be suitable since the user will still be able to close via alt+f4.


  • Moderators

    @R-P-H said in Disable close for fullscreen window on Windows ?:

    Would you say there's a more popular way of achieving this rather than adding a flag to all my classes ?

    It's such a small problem I don't think there's any established pattern for this. The gist of it is that you want closeEvent to behave differently depending on the trigger for it so a flag to indicate that seems reasonable.

    If there's more than one such class you have you can make a base class with that functionality so that you don't copy/paste it everywhere. You could give it a custom closeInternal() method or something that you'd use from code and it would set the flag and then call close().

    If you don't want to add extra flag to your class you can use the built in dynamic properties of QObject and do something like this:

    void MyWindow::reallyClose()
    {
       setProperty("really_close", true);
       close();
    }
    
    void MyWindow::closeEvent(QCloseEvent* event)
    {
       if (!property("really_close").toBool())
           event->ignore();
    }
    


  • @Chris-Kawa Thanks, it works !