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?
QtWS25 Last Chance

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 22 Mar 2024, 11:15 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?

    R 1 Reply Last reply 22 Mar 2024, 11:25
    0
    • R Ronel_qtmaster
      22 Mar 2024, 11:53

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

      J Offline
      J Offline
      JonB
      wrote on 22 Mar 2024, 13:09 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 R S 3 Replies Last reply 22 Mar 2024, 13:20
      1
      • J jdent
        22 Mar 2024, 11:15

        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?

        R Offline
        R Offline
        Ronel_qtmaster
        wrote on 22 Mar 2024, 11:25 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 22 Mar 2024, 11:35
        0
        • R Ronel_qtmaster
          22 Mar 2024, 11:25

          @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 22 Mar 2024, 11:35 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?

          R 1 Reply Last reply 22 Mar 2024, 11:38
          0
          • J jdent
            22 Mar 2024, 11:35

            @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?

            R Offline
            R Offline
            Ronel_qtmaster
            wrote on 22 Mar 2024, 11:38 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 22 Mar 2024, 11:45
            0
            • R Ronel_qtmaster
              22 Mar 2024, 11:38

              @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 22 Mar 2024, 11:45 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!

              R 1 Reply Last reply 22 Mar 2024, 11:53
              0
              • J jdent
                22 Mar 2024, 11:45

                @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!

                R Offline
                R Offline
                Ronel_qtmaster
                wrote on 22 Mar 2024, 11:53 last edited by
                #6

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

                J J 2 Replies Last reply 22 Mar 2024, 12:17
                0
                • R Ronel_qtmaster
                  22 Mar 2024, 11:53

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

                  J Offline
                  J Offline
                  jdent
                  wrote on 22 Mar 2024, 12:17 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?

                  J 1 Reply Last reply 22 Mar 2024, 12:31
                  0
                  • J jdent
                    22 Mar 2024, 12:17

                    @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?

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 22 Mar 2024, 12:31 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
                    • R Ronel_qtmaster
                      22 Mar 2024, 11:53

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

                      J Offline
                      J Offline
                      JonB
                      wrote on 22 Mar 2024, 13:09 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 R S 3 Replies Last reply 22 Mar 2024, 13:20
                      1
                      • J JonB
                        22 Mar 2024, 13:09

                        @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 22 Mar 2024, 13:20 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 22 Mar 2024, 13:20
                        • J JonB
                          22 Mar 2024, 13:09

                          @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.

                          R Offline
                          R Offline
                          Ronel_qtmaster
                          wrote on 22 Mar 2024, 14:14 last edited by
                          #11

                          @JonB Thanks for the hint

                          1 Reply Last reply
                          1
                          • J JonB
                            22 Mar 2024, 13:09

                            @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 25 Mar 2024, 06:57 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.

                            J 1 Reply Last reply 25 Mar 2024, 08:20
                            0
                            • S SimonSchroeder
                              25 Mar 2024, 06:57

                              @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.

                              J Offline
                              J Offline
                              JonB
                              wrote on 25 Mar 2024, 08:20 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

                              2/13

                              22 Mar 2024, 11:25

                              topic:navigator.unread, 11
                              • Login

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