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. [SOLVED] Why QDialog does not receive a closeEvent on hitting Esc?
QtWS25 Last Chance

[SOLVED] Why QDialog does not receive a closeEvent on hitting Esc?

Scheduled Pinned Locked Moved General and Desktop
12 Posts 5 Posters 20.6k 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
    ScumCoder
    wrote on last edited by
    #1

    I've reimplemented the closeEvent method in my QDialog subclass. When the dialog is closed by clicking on the 'close' button with the mouse, this method is called OK. But if it is closed by hitting Esc key on the keyboard, the QCloseEvent is not delivered. Why?
    I'm using Qt 4.8.5 on openSuSE 13.1 x64, Qt Creator 2.8.1 and gcc version 4.8.1 20130909.

    1 Reply Last reply
    0
    • EddyE Offline
      EddyE Offline
      Eddy
      wrote on last edited by
      #2

      Hi and welcome to Devnet,

      can you show us your closeEvent implementation?

      Qt Certified Specialist
      www.edalsolutions.be

      1 Reply Last reply
      0
      • S Offline
        S Offline
        ScumCoder
        wrote on last edited by
        #3

        @void SettingsDialogClass::closeEvent(QCloseEvent *event)
        {
        emit WindowClosed(); // emit a signal
        this->QDialog::closeEvent(event);
        }@
        [quote author="Eddy" date="1396256429"]Hi and welcome to Devnet,

        can you show us your closeEvent implementation?[/quote]

        1 Reply Last reply
        0
        • IamSumitI Offline
          IamSumitI Offline
          IamSumit
          wrote on last edited by
          #4

          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

          Hope it helps

          PS :- From Qt Docs

          Be Cute

          1 Reply Last reply
          0
          • EddyE Offline
            EddyE Offline
            Eddy
            wrote on last edited by
            #5

            Hi,

            you should only connect the buttons clicked signal with the dialog's close Slot.

            no need to subclass closeEvent.

            on a side note, suppose you need to subclass, you only need to use
            @
            QDialog::closeEvent(event);
            @

            without "this"

            hope it helps

            Qt Certified Specialist
            www.edalsolutions.be

            1 Reply Last reply
            0
            • C Offline
              C Offline
              cincirin
              wrote on last edited by
              #6

              To answer to @ScumCoder question:
              closeEvent is not called on Esc key press. It's not a bug, it's documented:
              "Escape Key":http://qt-project.org/doc/qt-4.8/qdialog.html#escape-key

              1 Reply Last reply
              0
              • S Offline
                S Offline
                ScumCoder
                wrote on last edited by
                #7

                Thanks all.
                I didn't quite understand why the QCloseEvent is not delivered, but I just switched to hooking the QHideEvent instead and everything works now.

                1 Reply Last reply
                0
                • IamSumitI Offline
                  IamSumitI Offline
                  IamSumit
                  wrote on last edited by
                  #8

                  Hello ScumCoder..
                  This is very easy to understand

                  by default QDialog has Escape button in order to close the dialog.
                  if you use closeEvent() the it will not have default behavior like escaping button.

                  [quote author="ScumCoder" date="1396256479"]@void SettingsDialogClass::closeEvent(QCloseEvent *event)
                  {
                  emit WindowClosed(); // emit a signal
                  this->QDialog::closeEvent(event);
                  }@
                  [quote author="Eddy" date="1396256429"]Hi and welcome to Devnet,

                  can you show us your closeEvent implementation?[/quote]

                  [/quote]
                  -The reason is -

                  -Here you are reimplementing closeEvent() which will override the standard close or reject behavior.
                  hope you would understand- .

                  Be Cute

                  1 Reply Last reply
                  0
                  • EddyE Offline
                    EddyE Offline
                    Eddy
                    wrote on last edited by
                    #9

                    bq. Here you are reimplementing closeEvent() which will override the standard close or reject behavior.

                    That's not correct since he is calling the default implementation in his custom closeEvent. The only thing extra here is emitting a signal which is not needed.

                    @
                    void Dialog::closeEvent(QCloseEvent * event)
                    {
                    emit WindowClosed();
                    QDialog::closeEvent(event); // calls the default
                    }
                    @

                    This code should works. The ESC is not blocked by this. You can test it by making a small project with a QDialog and use the same closeEvent as above. But as already said it's not needed to subclass closeEvent.

                    There must be something else blocking the ESC key.

                    Qt Certified Specialist
                    www.edalsolutions.be

                    1 Reply Last reply
                    0
                    • IamSumitI Offline
                      IamSumitI Offline
                      IamSumit
                      wrote on last edited by
                      #10

                      Thanks Eddy,
                      I tested by making simple project ans it's working and i found

                      No need to use subclass closeevent/protected void closeEvent(QCloseEvent *);.

                      it is bydefault(Escape).

                      But my question : what is it mean ?
                      from docs.
                      The closeEvent() function should only be reimplemented to preserve the dialog’s position or to override the standard close or reject behavior(QDialog).

                      Be Cute

                      1 Reply Last reply
                      0
                      • EddyE Offline
                        EddyE Offline
                        Eddy
                        wrote on last edited by
                        #11

                        It means that you should only reimplement it if

                        1. you want to save somehow the last position of the dialog for later use.
                        2. close the dialog in another way
                        3. cancel the dialog in another way

                        Hope this helps

                        Qt Certified Specialist
                        www.edalsolutions.be

                        1 Reply Last reply
                        0
                        • B Offline
                          B Offline
                          bootchk
                          wrote on last edited by
                          #12

                          I have a related problem? On Ubuntu13.10 (Unity) and 14.04 Gnome, Qt5.2.1 and PyQt5, the close icon of the main window stops working after the user operates on the view (e.g. zoom). It could be something I am doing wrong, but I am reimplementing closeEvent().

                          Is the same advice pertinent for QMainWindow, to not reimplement closeEvent()? (I haven't studied what other signal I might connect.)

                          I'm thinking it is a problem with interfacing with the window manager, since this seems to have started happening since I upgraded from earlier Ubuntu. And that I should start a new thread.

                          @def closeEvent(self, event):
                          ''' Handler for closeEvent from window manager. '''
                          if self.reallyQuit:
                          event.accept()
                          # Do not call _writeWindowAttributeSettings()
                          # Do not call app.quit(). Since WA_QuitOnClose is set, Qt will emit lastWindowClosed(), which will invoke quit().
                          else:
                          config.guiApp.closeDocument() # windowModal sheet on OSX, modal elsewhere
                          event.ignore() # wait to be called again after result of MaybeSave@

                          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