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 can I make a default QPushButton respond to ENTER?
Forum Update on Monday, May 27th 2025

How can I make a default QPushButton respond to ENTER?

Scheduled Pinned Locked Moved Solved General and Desktop
dialog boxdefault action
13 Posts 5 Posters 1.1k 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.
  • J Offline
    J Offline
    jdent
    wrote on last edited by
    #1

    I have a QPushButton marked as default but when I press ENTER it is not activated - when my cursor is in a QLineEdit control for example. This is the code:

        connect(searchButton, &QPushButton::clicked, this, &PasswordManager::search);
        searchButton->setDefault(true);
    

    What am I missing?

    Ronel_qtmasterR 1 Reply Last reply
    0
    • Ronel_qtmasterR Ronel_qtmaster

      @jdent did you try pushButton->setShortcut(QKeySequence(Qt::Key_Enter));

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

      @Ronel_qtmaster said in How can I make a default QPushButton respond to ENTER?:

      @jdent did you try pushButton->setShortcut(QKeySequence(Qt::Key_Enter));

      See my discussion with @Chris-Kawa on this in https://forum.qt.io/topic/155461/qlineedit-returnpressed-when-something-else-has-a-key_return-shortcut. It might be worth trying Qt::Key_Return here, I suspect what you call the Enter key is what Qt calls the Return key. But it may be that those two are interchangeable, not sure.

      It can certainly be done with a shortcut if you want, but I (think I) prefer @jsulm's suggestion of connecting returnPressed.

      J Ronel_qtmasterR S 3 Replies Last reply
      1
      • J jdent

        I have a QPushButton marked as default but when I press ENTER it is not activated - when my cursor is in a QLineEdit control for example. This is the code:

            connect(searchButton, &QPushButton::clicked, this, &PasswordManager::search);
            searchButton->setDefault(true);
        

        What am I missing?

        Ronel_qtmasterR Offline
        Ronel_qtmasterR Offline
        Ronel_qtmaster
        wrote on last edited by Ronel_qtmaster
        #2

        @jdent The default property of QPushButtons, instead will "press" the button whenever the top level widget receives the Return or Enter keys are pressed, no matter of the current focused widget, and as long as the focused widget does not handle those keys.So it does not apply to qlineedit

        J 1 Reply Last reply
        0
        • Ronel_qtmasterR Ronel_qtmaster

          @jdent The default property of QPushButtons, instead will "press" the button whenever the top level widget receives the Return or Enter keys are pressed, no matter of the current focused widget, and as long as the focused widget does not handle those keys.So it does not apply to qlineedit

          J Offline
          J Offline
          jdent
          wrote on last edited by jdent
          #3

          @Ronel_qtmaster I am sorry I don't understand what you are saying. Is there no way to make the button respond to ENTER? what if I set a shortcut?

          Ronel_qtmasterR 1 Reply Last reply
          0
          • J jdent

            @Ronel_qtmaster I am sorry I don't understand what you are saying. Is there no way to make the button respond to ENTER? what if I set a shortcut?

            Ronel_qtmasterR Offline
            Ronel_qtmasterR Offline
            Ronel_qtmaster
            wrote on last edited by
            #4

            @jdent i mean setDefault will not work when there is a focus in the LineEdit.Appart from that it will work.If you add a shortcut it will work as well

            J 1 Reply Last reply
            0
            • Ronel_qtmasterR Ronel_qtmaster

              @jdent i mean setDefault will not work when there is a focus in the LineEdit.Appart from that it will work.If you add a shortcut it will work as well

              J Offline
              J Offline
              jdent
              wrote on last edited by jdent
              #5

              @Ronel_qtmaster ok. How can I set a shortcut for the ENTER key?
              I tried

              searchButton->setShortcut(QKeySequence::StandardKey::)
              

              but there is no key for ENTER or RETURN!

              Ronel_qtmasterR 1 Reply Last reply
              0
              • J jdent

                @Ronel_qtmaster ok. How can I set a shortcut for the ENTER key?
                I tried

                searchButton->setShortcut(QKeySequence::StandardKey::)
                

                but there is no key for ENTER or RETURN!

                Ronel_qtmasterR Offline
                Ronel_qtmasterR Offline
                Ronel_qtmaster
                wrote on last edited by
                #6

                @jdent did you try pushButton->setShortcut(QKeySequence(Qt::Key_Enter));

                J JonBJ 2 Replies Last reply
                0
                • Ronel_qtmasterR Ronel_qtmaster

                  @jdent did you try pushButton->setShortcut(QKeySequence(Qt::Key_Enter));

                  J Offline
                  J Offline
                  jdent
                  wrote on last edited by
                  #7

                  @Ronel_qtmaster I tried it - no results.

                  4ff17106-748f-4df2-b3a5-0653c521edf9-image.png

                  In MFC (Microsoft Foundation Classes) you can do this, I can't see why we can't do it in Qt...
                  There must be a way!!

                  When I press ENTER when inside the line edit, can't I take that keystroke and invoke the Search button?

                  jsulmJ 1 Reply Last reply
                  0
                  • J jdent

                    @Ronel_qtmaster I tried it - no results.

                    4ff17106-748f-4df2-b3a5-0653c521edf9-image.png

                    In MFC (Microsoft Foundation Classes) you can do this, I can't see why we can't do it in Qt...
                    There must be a way!!

                    When I press ENTER when inside the line edit, can't I take that keystroke and invoke the Search button?

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @jdent Connect https://doc.qt.io/qt-6/qlineedit.html#returnPressed to https://doc.qt.io/qt-6/qabstractbutton.html#click

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1
                    • Ronel_qtmasterR Ronel_qtmaster

                      @jdent did you try pushButton->setShortcut(QKeySequence(Qt::Key_Enter));

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

                      @Ronel_qtmaster said in How can I make a default QPushButton respond to ENTER?:

                      @jdent did you try pushButton->setShortcut(QKeySequence(Qt::Key_Enter));

                      See my discussion with @Chris-Kawa on this in https://forum.qt.io/topic/155461/qlineedit-returnpressed-when-something-else-has-a-key_return-shortcut. It might be worth trying Qt::Key_Return here, I suspect what you call the Enter key is what Qt calls the Return key. But it may be that those two are interchangeable, not sure.

                      It can certainly be done with a shortcut if you want, but I (think I) prefer @jsulm's suggestion of connecting returnPressed.

                      J Ronel_qtmasterR S 3 Replies Last reply
                      1
                      • JonBJ JonB

                        @Ronel_qtmaster said in How can I make a default QPushButton respond to ENTER?:

                        @jdent did you try pushButton->setShortcut(QKeySequence(Qt::Key_Enter));

                        See my discussion with @Chris-Kawa on this in https://forum.qt.io/topic/155461/qlineedit-returnpressed-when-something-else-has-a-key_return-shortcut. It might be worth trying Qt::Key_Return here, I suspect what you call the Enter key is what Qt calls the Return key. But it may be that those two are interchangeable, not sure.

                        It can certainly be done with a shortcut if you want, but I (think I) prefer @jsulm's suggestion of connecting returnPressed.

                        J Offline
                        J Offline
                        jdent
                        wrote on last edited by
                        #10

                        @JonB This did it!!

                        searchButton->setShortcut(QKeySequence(Qt::Key_Return));
                        

                        Thanks all of you!!

                        1 Reply Last reply
                        1
                        • J jdent has marked this topic as solved on
                        • JonBJ JonB

                          @Ronel_qtmaster said in How can I make a default QPushButton respond to ENTER?:

                          @jdent did you try pushButton->setShortcut(QKeySequence(Qt::Key_Enter));

                          See my discussion with @Chris-Kawa on this in https://forum.qt.io/topic/155461/qlineedit-returnpressed-when-something-else-has-a-key_return-shortcut. It might be worth trying Qt::Key_Return here, I suspect what you call the Enter key is what Qt calls the Return key. But it may be that those two are interchangeable, not sure.

                          It can certainly be done with a shortcut if you want, but I (think I) prefer @jsulm's suggestion of connecting returnPressed.

                          Ronel_qtmasterR Offline
                          Ronel_qtmasterR Offline
                          Ronel_qtmaster
                          wrote on last edited by
                          #11

                          @JonB Thanks for the hint

                          1 Reply Last reply
                          1
                          • JonBJ JonB

                            @Ronel_qtmaster said in How can I make a default QPushButton respond to ENTER?:

                            @jdent did you try pushButton->setShortcut(QKeySequence(Qt::Key_Enter));

                            See my discussion with @Chris-Kawa on this in https://forum.qt.io/topic/155461/qlineedit-returnpressed-when-something-else-has-a-key_return-shortcut. It might be worth trying Qt::Key_Return here, I suspect what you call the Enter key is what Qt calls the Return key. But it may be that those two are interchangeable, not sure.

                            It can certainly be done with a shortcut if you want, but I (think I) prefer @jsulm's suggestion of connecting returnPressed.

                            S Offline
                            S Offline
                            SimonSchroeder
                            wrote on last edited by
                            #12

                            @JonB said in How can I make a default QPushButton respond to ENTER?:

                            I suspect what you call the Enter key is what Qt calls the Return key

                            You might want to connect both Enter and Return. If I'm not mistaken, Enter is the key located in the NumPad area.

                            JonBJ 1 Reply Last reply
                            0
                            • S SimonSchroeder

                              @JonB said in How can I make a default QPushButton respond to ENTER?:

                              I suspect what you call the Enter key is what Qt calls the Return key

                              You might want to connect both Enter and Return. If I'm not mistaken, Enter is the key located in the NumPad area.

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

                              @SimonSchroeder See the discussion around https://forum.qt.io/topic/155461/qlineedit-returnpressed-when-something-else-has-a-key_return-shortcut/16 for the pointlessness/potential evilness of the NumPad Enter 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