Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Disable close for fullscreen window on Windows ?
QtWS25 Last Chance

Disable close for fullscreen window on Windows ?

Scheduled Pinned Locked Moved Solved General and Desktop
closeeventclose windowwindowsubuntu
7 Posts 3 Posters 2.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    R-P-H
    wrote on last edited by
    #1

    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.

    Chris KawaC 1 Reply Last reply
    0
    • R R-P-H

      @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 ?

      Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #6

      @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();
      }
      
      R 1 Reply Last reply
      2
      • R R-P-H

        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.

        Chris KawaC Online
        Chris KawaC Online
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #2

        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.

        R 1 Reply Last reply
        3
        • Chris KawaC Chris Kawa

          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.

          R Offline
          R Offline
          R-P-H
          wrote on last edited by
          #3

          @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 ?

          Chris KawaC 1 Reply Last reply
          0
          • gde23G Offline
            gde23G Offline
            gde23
            wrote on last edited by
            #4

            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 ...

            R 1 Reply Last reply
            0
            • gde23G gde23

              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 ...

              R Offline
              R Offline
              R-P-H
              wrote on last edited by
              #5

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

              1 Reply Last reply
              0
              • R R-P-H

                @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 ?

                Chris KawaC Online
                Chris KawaC Online
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by Chris Kawa
                #6

                @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();
                }
                
                R 1 Reply Last reply
                2
                • Chris KawaC Chris Kawa

                  @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();
                  }
                  
                  R Offline
                  R Offline
                  R-P-H
                  wrote on last edited by
                  #7

                  @Chris-Kawa Thanks, it works !

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved