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. Detecting if X is clicked on a dialog
QtWS25 Last Chance

Detecting if X is clicked on a dialog

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 4.5k 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.
  • T Offline
    T Offline
    tony67
    wrote on last edited by
    #1

    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
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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
      0
      • ValentinMicheletV Offline
        ValentinMicheletV Offline
        ValentinMichelet
        wrote on last edited by
        #3

        Hi,

        Is by any chance a QDialog open as a delegate?

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          ValentinMicheletV 1 Reply Last reply
          1
          • Chris KawaC Chris Kawa

            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.

            ValentinMicheletV Offline
            ValentinMicheletV Offline
            ValentinMichelet
            wrote on last edited by ValentinMichelet
            #5

            @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
            1
            • T Offline
              T Offline
              tony67
              wrote on last edited by
              #6

              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
              0
              • ValentinMicheletV Offline
                ValentinMicheletV Offline
                ValentinMichelet
                wrote on last edited by ValentinMichelet
                #7

                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
                0

                • Login

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