Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Detecting if X is clicked on a dialog

    General and Desktop
    4
    7
    3654
    Loading More Posts
    • 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.
    • T
      tony67 last edited by

      Hi All,
      When I call a dialog with exec( ) how can I tell if the X is clicked to close the dialog? I can see dialog returns a value, however it always seems to be zero. So if I close by clicking a OK button it sends 0, if I close with the X it still returns 0. I need to detect the X rather than another way of closing. Thanks :)

      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by mrjj

        hi
        exec returns
        DialogCode { Accepted, Rejected }
        QDialog::Accepted 1
        QDialog::Rejected 0
        Why do u need to know if X was pressed?

        1 Reply Last reply Reply Quote 0
        • ValentinMichelet
          ValentinMichelet last edited by

          Hi,

          Is by any chance a QDialog open as a delegate?

          1 Reply Last reply Reply Quote 0
          • Chris Kawa
            Chris Kawa Moderators last edited by

            So if I close by clicking a OK button it sends 0

            This means the OK button code is wrong. Clicking on an "Ok" button should either be connected to, or in other way call accept() method of the QDialog. This closes the dialog and sets a return code of exec() to Qt::Accepted (as @mrjj pointed out).
            Similarly, clicking a "Cancel" button should call reject(). This closes the dialog and sets the return code of exec() to Qt::Rejected. Clicking on the "x" already calls reject() for you.

            ValentinMichelet 1 Reply Last reply Reply Quote 1
            • ValentinMichelet
              ValentinMichelet @Chris Kawa last edited by ValentinMichelet

              @Chris-Kawa
              There is a known bug when using a dialog as delegate: the dialog returns 0 regardless of what button is clicked.

              See
              https://bugreports.qt.io/browse/QTBUG-6018
              https://bugreports.qt.io/browse/QTBUG-12156
              https://bugreports.qt.io/browse/QTBUG-14430

              Even though it is set to closed since it has been detected under Qt4, I noticed that the QDialog code source from Qt5 has still not been fixed. So there is a workaround described in bug report.

              Still, it could be quite simple to fix in Qt directly: there are only two lines to swap in QDialog class. I don't know how to make Qt team fix this tho.

              @tony67
              How did you create your QDialog? Did you use QtDesigner? Did you modify the QDialogButtonBox?
              Could you show us your code so we can know what the problem is?

              1 Reply Last reply Reply Quote 1
              • T
                tony67 last edited by

                Hi basically the reason I need to know is to determine if an new program is started, basically in the dialog you set the name of the new program and it's version, click OK, close this name dialog, then start the new program up with the name and version. If I click X ( or cancel ) I want it to not try to start the program, that is I've accidental clicked new program, so don't try to run. I thought if I pressed OK and that returned a value, while X returned a 0 I could do something like (pseudo) if ( dialog->exec() > 0 ) start_new_program, else close.

                1 Reply Last reply Reply Quote 0
                • ValentinMichelet
                  ValentinMichelet last edited by ValentinMichelet

                  Well, then simply do this:

                  MyDialog dialog(this);
                  if (dialog.exec() == QDialog::Accepted) {
                      // Start your program here
                  }
                  

                  Clicking on the X button (as well as clicking on Cancel button) must return QDialog::Rejected. So just don't do anything if it doesn't return QDIalog::Accepted, and you're good to go.

                  Also, please tell us more about this dialog, and paste your code here. It's really important if you want us to help you:

                  • Did you use a pure QDialog?
                  • Did you use a Qt dialog inherited by QDialog? (QFileDialog, QInputDialog...)
                  • Did you do your own class inheriting a Qt one?
                  • Did you create it with QtDesigner?
                  • Did you use it as a delegate?

                  Basically, either you forgot to connect the buttons clicked signal to the QDialog accept/reject SLOTS, or it's the known bug with delegate I was talking about.
                  But again, without your code, it is really hard to guess.

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post