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

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 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
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on 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
      • Cobra91151C Offline
        Cobra91151C Offline
        Cobra91151
        wrote on 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!

        JonBJ 1 Reply Last reply
        1
        • Cobra91151C Cobra91151

          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!

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 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?

          Cobra91151C 1 Reply Last reply
          1
          • JonBJ JonB

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

            Cobra91151C Offline
            Cobra91151C Offline
            Cobra91151
            wrote on 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.

            JonBJ 1 Reply Last reply
            1
            • Cobra91151C Cobra91151

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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on 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?

              Cobra91151C 1 Reply Last reply
              0
              • JonBJ JonB

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

                Cobra91151C Offline
                Cobra91151C Offline
                Cobra91151
                wrote on 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;
                }
                
                JonBJ 1 Reply Last reply
                1
                • Cobra91151C Cobra91151

                  @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;
                  }
                  
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on 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

                  • Login

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