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. Prevent QKeyEvent from propagating to parent widget (QDialog)

Prevent QKeyEvent from propagating to parent widget (QDialog)

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 1.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.
  • deisikD Offline
    deisikD Offline
    deisik
    wrote on last edited by deisik
    #1

    I have a QDialog which creates another QDialog, and when I hit Esc key both QDialogs close. I tried everything in the event handler of the child QDialog to prevent the parent QDialog from closing (though I need it to close when it receives Esc directly). Nothing works. I even tried destroying the QKeyEvent object itself in the child event handler. It just gives me a SegFault

    I can work around this issue by checking whether the Esc key was pressed in the child QDialog but it doesn't look like a nice way of preventing the parent QDialog from closing

    Is there a way to fix this issue without using some ugly workarounds?

    JonBJ 1 Reply Last reply
    0
    • deisikD deisik

      I have a QDialog which creates another QDialog, and when I hit Esc key both QDialogs close. I tried everything in the event handler of the child QDialog to prevent the parent QDialog from closing (though I need it to close when it receives Esc directly). Nothing works. I even tried destroying the QKeyEvent object itself in the child event handler. It just gives me a SegFault

      I can work around this issue by checking whether the Esc key was pressed in the child QDialog but it doesn't look like a nice way of preventing the parent QDialog from closing

      Is there a way to fix this issue without using some ugly workarounds?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @deisik
      Pressing Escape in one dialog should not be closing a second dialog. And there is no reason to be doing any key event stuff. Show your code which exhibits the behaviour.

      1 Reply Last reply
      1
      • deisikD Offline
        deisikD Offline
        deisik
        wrote on last edited by deisik
        #3

        Child QDialog:

        void Child::keyReleaseEvent(QKeyEvent *event) {
        if (Qt::Key(event->key()) == Qt::Key_Escape)
        	reject();
        else
        	QDialog::keyReleaseEvent(event);
        }
        

        Parent QDialog:

        void Parent::keyReleaseEvent(QKeyEvent *event) {
        if (event->key() == Qt::Key_Escape)
        	emit close();
        else
        	QDialog::keyReleaseEvent(event);
        }
        

        Child QDialog is called using exec()

        JonBJ 1 Reply Last reply
        0
        • deisikD deisik

          Child QDialog:

          void Child::keyReleaseEvent(QKeyEvent *event) {
          if (Qt::Key(event->key()) == Qt::Key_Escape)
          	reject();
          else
          	QDialog::keyReleaseEvent(event);
          }
          

          Parent QDialog:

          void Parent::keyReleaseEvent(QKeyEvent *event) {
          if (event->key() == Qt::Key_Escape)
          	emit close();
          else
          	QDialog::keyReleaseEvent(event);
          }
          

          Child QDialog is called using exec()

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @deisik
          But why are you doing any of this? Escape closes a dialog anyway, why all the code? Why not have no code??

          As for the reason: I assume acting on a key press in one dialog and a key release in another is a bit "odd". I guess the child gets the press, closes, and then its the parent which sees the key release and so closes?

          I think your code keeps changing...?

          1 Reply Last reply
          0
          • deisikD Offline
            deisikD Offline
            deisik
            wrote on last edited by deisik
            #5

            I was trying different combinations (release vs press). Neither works. Now changed everything to keyReleaseEvent. Didn't change anything

            JonBJ 1 Reply Last reply
            0
            • deisikD deisik

              I was trying different combinations (release vs press). Neither works. Now changed everything to keyReleaseEvent. Didn't change anything

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @deisik
              Before we spend time trying to fathom this: Why do you have any code at all? Without any code, doesn't Escape do just what you want?

              1 Reply Last reply
              0
              • deisikD Offline
                deisikD Offline
                deisik
                wrote on last edited by deisik
                #7

                I double checked everything, no more keyPressEvents. The code is exactly as above. Now I'm trying to remove the handlers completely

                1 Reply Last reply
                0
                • deisikD Offline
                  deisikD Offline
                  deisik
                  wrote on last edited by deisik
                  #8

                  Well, I removed both handlers and it finally started to work as intended. The reason why I used them in the first place was to fix the problem somehow which showed up somewhere else (maybe it was not even related)

                  Anyway, I think I can mark this topic as solved

                  JonBJ 1 Reply Last reply
                  0
                  • deisikD deisik

                    Well, I removed both handlers and it finally started to work as intended. The reason why I used them in the first place was to fix the problem somehow which showed up somewhere else (maybe it was not even related)

                    Anyway, I think I can mark this topic as solved

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @deisik
                    :)

                    BTW: http://doc.qt.io/qt-5/qdialog.html#escape-key

                    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