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. How to disable all options of QDialog closure
QtWS25 Last Chance

How to disable all options of QDialog closure

Scheduled Pinned Locked Moved Unsolved General and Desktop
qdialog
8 Posts 4 Posters 2.0k 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.
  • D Offline
    D Offline
    Dariusz
    wrote on 29 Jan 2021, 20:42 last edited by
    #1

    Hey

    I'm in need of a QDialog that would not be closed by anything user do. How can I properly set up the widget to do it?

    I tried capturing closeEvent() but esc seem to skip it somehow?

    Any hints?

    TIA

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 30 Jan 2021, 08:56 last edited by
      #2

      Hi
      Docs says
      In order to modify your dialog's close behavior, you can reimplement the functions accept(), reject() or done(). The "closeEvent() function should only be reimplemented to preserve the dialog's position or to override the standard close or reject behavior."

      So i think you should override accept(), reject() and handle it that way.

      1 Reply Last reply
      2
      • C Offline
        C Offline
        Cobra91151
        wrote on 30 Jan 2021, 11:03 last edited by
        #3

        Hello!

        Additionally, you can try to make the dialog without controls by setting the code below:

        setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint);
        

        The user can not minimize/maximize or close such dialog:

        2021-01-30_125741.png

        Also, you can modify the nativeEvent to intercept the dialog closure: https://forum.qt.io/topic/104533/how-to-ignore-the-event-when-i-click-on-the-button-to-maximize-the-window-windowmaximizebuttonhint/9

        Happy coding!

        J 1 Reply Last reply 30 Jan 2021, 11:07
        1
        • C Cobra91151
          30 Jan 2021, 11:03

          Hello!

          Additionally, you can try to make the dialog without controls by setting the code below:

          setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint);
          

          The user can not minimize/maximize or close such dialog:

          2021-01-30_125741.png

          Also, you can modify the nativeEvent to intercept the dialog closure: https://forum.qt.io/topic/104533/how-to-ignore-the-event-when-i-click-on-the-button-to-maximize-the-window-windowmaximizebuttonhint/9

          Happy coding!

          J Offline
          J Offline
          JonB
          wrote on 30 Jan 2021, 11:07 last edited by JonB
          #4

          @Cobra91151 said in How to disable all options of QDialog closure:

          The user can not minimize/maximize or close such dialog:

          I cannot test this, but OOI what does pressing Alt+F4 under Windows on this dialog do?

          C 1 Reply Last reply 30 Jan 2021, 11:21
          1
          • J JonB
            30 Jan 2021, 11:07

            @Cobra91151 said in How to disable all options of QDialog closure:

            The user can not minimize/maximize or close such dialog:

            I cannot test this, but OOI what does pressing Alt+F4 under Windows on this dialog do?

            C Offline
            C Offline
            Cobra91151
            wrote on 30 Jan 2021, 11:21 last edited by
            #5

            @JonB

            Hello! Here is how it intercepts the Alt+F4.

            alt_f4_dlg_example.gif

            So, as you can see, setting window flags and overriding the native event will help to prevent the dialog from closing.

            J 1 Reply Last reply 30 Jan 2021, 11:37
            1
            • C Cobra91151
              30 Jan 2021, 11:21

              @JonB

              Hello! Here is how it intercepts the Alt+F4.

              alt_f4_dlg_example.gif

              So, as you can see, setting window flags and overriding the native event will help to prevent the dialog from closing.

              J Offline
              J Offline
              JonB
              wrote on 30 Jan 2021, 11:37 last edited by JonB
              #6

              @Cobra91151 said in How to disable all options of QDialog closure:

              and overriding the native event

              Indeed! My question was whether this was required to make it work robustly; if you only do the setWindowFlags() I wondered whether the keyboard shortcut would still cause closure?

              C 1 Reply Last reply 30 Jan 2021, 11:50
              0
              • J JonB
                30 Jan 2021, 11:37

                @Cobra91151 said in How to disable all options of QDialog closure:

                and overriding the native event

                Indeed! My question was whether this was required to make it work robustly; if you only do the setWindowFlags() I wondered whether the keyboard shortcut would still cause closure?

                C Offline
                C Offline
                Cobra91151
                wrote on 30 Jan 2021, 11:50 last edited by
                #7

                @JonB

                Even when setWindowFlags are set, you have to intercept the dialog closure using the nativeEvent.

                Code:

                bool TestDlg::nativeEvent(const QByteArray &eventType, void *message, long *) {
                    qDebug() << eventType;
                    MSG *msg = static_cast<MSG *>(message);
                
                #ifdef __linux__
                    //Linux code here
                #elif _WIN32
                    if (msg->message == WM_SYSCOMMAND) {
                        if (msg->wParam == SC_CLOSE) {
                            QMessageBox::information(this, "Test", "Closing...", QMessageBox::Ok);
                            //Add your code here
                            return true;
                        }
                    }
                #endif
                
                    return false;
                }
                
                J 1 Reply Last reply 30 Jan 2021, 11:54
                1
                • C Cobra91151
                  30 Jan 2021, 11:50

                  @JonB

                  Even when setWindowFlags are set, you have to intercept the dialog closure using the nativeEvent.

                  Code:

                  bool TestDlg::nativeEvent(const QByteArray &eventType, void *message, long *) {
                      qDebug() << eventType;
                      MSG *msg = static_cast<MSG *>(message);
                  
                  #ifdef __linux__
                      //Linux code here
                  #elif _WIN32
                      if (msg->message == WM_SYSCOMMAND) {
                          if (msg->wParam == SC_CLOSE) {
                              QMessageBox::information(this, "Test", "Closing...", QMessageBox::Ok);
                              //Add your code here
                              return true;
                          }
                      }
                  #endif
                  
                      return false;
                  }
                  
                  J Offline
                  J Offline
                  JonB
                  wrote on 30 Jan 2021, 11:54 last edited by JonB
                  #8

                  @Cobra91151
                  Which is what I suspected, hence the question.

                  Your original answer made the nativeEvent interception look "optional" ("you can modify"), now the OP knows it is mandatory.

                  1 Reply Last reply
                  0

                  1/8

                  29 Jan 2021, 20:42

                  • Login

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