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. How to handle key press in a QDialog?
Qt 6.11 is out! See what's new in the release blog

How to handle key press in a QDialog?

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 10.7k Views 1 Watching
  • 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.
  • W Offline
    W Offline
    WhatIf
    wrote on last edited by
    #1

    Hello,

    My application consists of a mainwindow and a few qdialogs. I handle key press from the buttons in mainwindow using:

    void MainWindow::keyPressEvent(QKeyEvent * event)
    {
    ......
    }
    

    which works perfectly. I want to handle key presses from the qdialogs. Reading online solutions, I found most answers suggest that mainwindow will handle all the key events from dialogs. I added several breakpoints in keyPressEvent function listed above and pressed several keys while the dialog is active and the function was never called. Should I have a separate keyPressEvent(QKeyEvent * event) in the dialog class to handle its own key presses? Is this the approach to handle dialog key presses?

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Fuel
      wrote on last edited by
      #2

      QDialog has its own Event http://doc.qt.io/qt-4.8/qdialog.html#keyPressEvent

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

        Hi,

        Out of curiosity, what do you mean exactly by " handle key press from the buttons" ?

        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
        • W Offline
          W Offline
          WhatIf
          wrote on last edited by
          #4

          Originally, I thought that QPushButton is the class that receives the key event. In other words, the QPushButton know if it was clicked by the mouse or the enter key was pressed. But it turned out that MainWindow and QDialog intercept the key event.

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

            It depends whether the button has focus or not.

            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
            0
            • W Offline
              W Offline
              WhatIf
              wrote on last edited by
              #6

              Do you mean that the

              keyPressEvent(QKeyEvent * event)
              

              can be in a QPushButton subclass?

              Why do MainWindow and QDialog handle key events too?

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

                Because the widget who has the focus handles the events.

                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
                0
                • W Offline
                  W Offline
                  WhatIf
                  wrote on last edited by
                  #8

                  Alright, Let's say a search dialog has a line edit and a button for simplicity. If the line edit is in focus and the enter key is pressed, the line edit's keyPressEvent(QKeyEvent * event) will be called. And if I tab to select the button and press the enter key, the button's keyPressEvent(QKeyEvent * event) will be called.

                  Is there a standard way in Qt to avoid duplicate code for this situation?

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

                    Can you explain what exactly you are trying to achieve ?

                    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
                    0
                    • W Offline
                      W Offline
                      WhatIf
                      wrote on last edited by
                      #10

                      I'm trying to search for users in a database using a QDialog. The QDialog has 3 QLineEdit (one for first name, middle and last name). 2 QPushButton (Search and cancel).

                      Now when search qdialog is in focus, it should receive the key event. But the behavior I want is, as soon as the user enters at least the first name, he can press the enter key (while the first name line edit is in focus) and all matches of the first name will be displayed. Or the user can enter either the middle or last name in combination with the first name. The user can also tab to the search button and press enter to search or tab to the cancel button and press the enter key to close the search dialog.

                      So basically:

                      1. The qdialog (the search dialog) will be in focus
                      2. The user can press enter from any of 3 QLineEdit while in focus
                      3. The user can press enter while the search button is in focus
                      4. The user can press enter while the cancel button is in focus

                      2 and 3 will cause the info provided by the user to compare against what is in the database for a match. How should I avoid the duplicate code in four widgets?

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

                        What about using the QLineEdit::returnPressed signal ?

                        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
                        0

                        • Login

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