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. QDialog exec no longer modal after show
Forum Update on Monday, May 27th 2025

QDialog exec no longer modal after show

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 674 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.
  • S Offline
    S Offline
    SamiV123
    wrote on last edited by
    #1

    So the problem is that if have my own dialog such as

    class DlgFoobar : public QDialog {... }

    then let's say in my MainWindow I have the following

    void MainWindow::something()
    {
    DlgFoobar dlg(this);

    dlg.show();

    ...

    dlg.exec();
    }

    Simsalabim.. the dialog is no longer modal. In other words the user can click on other controls in the mainwindow.

    What gives?

    Comments on exec()

    So calling exec() without show() being called first works as expected. The dialog remains modal and the user is restricted to only applying input on the dialog.

    The problem is that using exec() is not adequate.

    Consider a case where you wish to restore the dialog state to a previous state, i.e. populate the dialog and its data in some meaningful way.

    To do this with smooth UX you really need to:

    1. Load and restore the dialog geometry before showing!
      • if you first show and then restore the resizing will cause ugly visual flicker and disturbance.
    2. Show it after the geometry has been restored
    3. Then load any other state, including state that might fail (for example imagine a file is no longer available)
      3.1 You need to have the dialog open so that you can open a QMessageBox properly using the dialog as the window parent in order to open the msg box properly.
    4. Finally call exec() and block the user to the dialog.

    Is there a way to do this or is this yet again something that has to be hacked together to workaround qt issues?

    Thanks!

    SGaistS 1 Reply Last reply
    0
    • S SamiV123

      So the problem is that if have my own dialog such as

      class DlgFoobar : public QDialog {... }

      then let's say in my MainWindow I have the following

      void MainWindow::something()
      {
      DlgFoobar dlg(this);

      dlg.show();

      ...

      dlg.exec();
      }

      Simsalabim.. the dialog is no longer modal. In other words the user can click on other controls in the mainwindow.

      What gives?

      Comments on exec()

      So calling exec() without show() being called first works as expected. The dialog remains modal and the user is restricted to only applying input on the dialog.

      The problem is that using exec() is not adequate.

      Consider a case where you wish to restore the dialog state to a previous state, i.e. populate the dialog and its data in some meaningful way.

      To do this with smooth UX you really need to:

      1. Load and restore the dialog geometry before showing!
        • if you first show and then restore the resizing will cause ugly visual flicker and disturbance.
      2. Show it after the geometry has been restored
      3. Then load any other state, including state that might fail (for example imagine a file is no longer available)
        3.1 You need to have the dialog open so that you can open a QMessageBox properly using the dialog as the window parent in order to open the msg box properly.
      4. Finally call exec() and block the user to the dialog.

      Is there a way to do this or is this yet again something that has to be hacked together to workaround qt issues?

      Thanks!

      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Are you explicitly setting the modality of your dialog ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Christian EhrlicherC S 2 Replies Last reply
      0
      • SGaistS SGaist

        Hi,

        Are you explicitly setting the modality of your dialog ?

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Show() creates a modeless dialog. So don't use it when you want a modal one.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        S 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          Are you explicitly setting the modality of your dialog ?

          S Offline
          S Offline
          SamiV123
          wrote on last edited by
          #4

          @SGaist

          @SGaist said in QDialog exec no longer modal after show:

          Hi,

          Are you explicitly setting the modality of your dialog ?

          No. setModal(true) doesn't fix the problem though.

          1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            Show() creates a modeless dialog. So don't use it when you want a modal one.

            S Offline
            S Offline
            SamiV123
            wrote on last edited by
            #5

            @Christian-Ehrlicher said in QDialog exec no longer modal after show:

            Show() creates a modeless dialog. So don't use it when you want a modal one.

            Uh uh, did you read why it's needed?

            Christian EhrlicherC 1 Reply Last reply
            0
            • S SamiV123

              @Christian-Ehrlicher said in QDialog exec no longer modal after show:

              Show() creates a modeless dialog. So don't use it when you want a modal one.

              Uh uh, did you read why it's needed?

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @SamiV123 Yes. Maybe you can convince the devs to change this behavior but I doubt it.
              http://bugreports.qt.io

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Did you experiment with open as well ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                S 1 Reply Last reply
                0
                • SGaistS SGaist

                  Did you experiment with open as well ?

                  S Offline
                  S Offline
                  SamiV123
                  wrote on last edited by
                  #8

                  @SGaist said in QDialog exec no longer modal after show:

                  Did you experiment with open as well ?

                  I did, but I don't know what's going on with open(). the whole dialog just disappeared and was not visible. Weird.

                  SGaistS M 2 Replies Last reply
                  0
                  • S SamiV123

                    @SGaist said in QDialog exec no longer modal after show:

                    Did you experiment with open as well ?

                    I did, but I don't know what's going on with open(). the whole dialog just disappeared and was not visible. Weird.

                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Check your dialog lifetime. If it's a function local object then it will be destroyed after calling open since it's the last method call in the function.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    S 1 Reply Last reply
                    1
                    • S SamiV123

                      @SGaist said in QDialog exec no longer modal after show:

                      Did you experiment with open as well ?

                      I did, but I don't know what's going on with open(). the whole dialog just disappeared and was not visible. Weird.

                      M Offline
                      M Offline
                      mpergand
                      wrote on last edited by
                      #10

                      @SamiV123
                      try this:

                      class DlgMsg : public QDialog
                      {
                      	public:
                      
                      		DlgMsg() : QDialog(nullptr) {}
                      
                      	void showMessage(const QString& str)
                      	{
                      		QTimer::singleShot(1000,this,[str,this]()
                      		{
                      		auto msg=QMessageBox(this);
                      		msg.setWindowModality(Qt::WindowModal);
                      		msg.setText(str);
                      		msg.exec();
                      		});
                      	}
                      };
                      ...
                      DlgMsg dmsg;
                      dmsg.resize(400,200);
                      dmsg.showMessage("Something wrong happened!");
                      
                      dmsg.exec();
                      
                      

                      1 Reply Last reply
                      0
                      • SGaistS SGaist

                        Check your dialog lifetime. If it's a function local object then it will be destroyed after calling open since it's the last method call in the function.

                        S Offline
                        S Offline
                        SamiV123
                        wrote on last edited by
                        #11

                        @SGaist said in QDialog exec no longer modal after show:

                        Check your dialog lifetime. If it's a function local object then it will be destroyed after calling open since it's the last method call in the function.

                        Doh, you're right, so open() returns immediately so of course that will that not work.

                        Actually open() seems very cumbersome to use, it's a lot easier to simply create the Dialog on the stack and call exec()

                        SGaistS 1 Reply Last reply
                        0
                        • S SamiV123

                          @SGaist said in QDialog exec no longer modal after show:

                          Check your dialog lifetime. If it's a function local object then it will be destroyed after calling open since it's the last method call in the function.

                          Doh, you're right, so open() returns immediately so of course that will that not work.

                          Actually open() seems very cumbersome to use, it's a lot easier to simply create the Dialog on the stack and call exec()

                          SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          As explained in the method documentation, exec has some caveats to take into account when you design your application.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          1

                          • Login

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