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. QSpinBox - is there a way to "force" lose focus?

QSpinBox - is there a way to "force" lose focus?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 6 Posters 3.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.
  • M Offline
    M Offline
    mzimmers
    wrote on 15 Jul 2020, 22:58 last edited by
    #1

    Hi all -

    I'm using a QSpinBox in a QWidget. From the docs:

    This signal is emitted editing is finished. This happens when the spinbox loses focus and when enter is pressed.
    

    But it appears that the spinbox doesn't lose focus until the user clicks on another widget in the window. I don't suppose there's a way to cause it to lose focus when the mouse cursor is removed from the spinbox area (in other words, focus on nothing)?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on 16 Jul 2020, 01:54 last edited by
      #2

      I haven't tested that but did you try calling QWidget::clearFocus() on the QLineEdit?

      M 1 Reply Last reply 16 Jul 2020, 01:57
      2
      • B Bonnie
        16 Jul 2020, 01:54

        I haven't tested that but did you try calling QWidget::clearFocus() on the QLineEdit?

        M Offline
        M Offline
        mzimmers
        wrote on 16 Jul 2020, 01:57 last edited by
        #3

        @Bonnie what QLineEdit?

        B 1 Reply Last reply 16 Jul 2020, 02:54
        0
        • M mzimmers
          16 Jul 2020, 01:57

          @Bonnie what QLineEdit?

          B Offline
          B Offline
          Bonnie
          wrote on 16 Jul 2020, 02:54 last edited by
          #4

          @mzimmers
          Every spin box has a QLineEdit for input.
          You can access it by QAbstractSpinBox::lineEdit(), but it is a protected function.
          I'm not sure whether the focus need to be cleared by the spin box or the line edit.

          M 1 Reply Last reply 16 Jul 2020, 19:01
          1
          • K Offline
            K Offline
            Kent-Dorfman
            wrote on 16 Jul 2020, 03:42 last edited by
            #5

            @mzimmers said in QSpinBox - is there a way to "force" lose focus?:

            But it appears that the spinbox doesn't lose focus until the user clicks on another widget in the window. I don't suppose there's a way to cause it to lose focus when the mouse cursor is removed from the spinbox area (in other words, focus on nothing)?

            I think "something" always has to have focus. I've dealt with similar situations by intercepting signals or events as necessary and manually setting focus to some other placeholder widget.

            1 Reply Last reply
            1
            • B Bonnie
              16 Jul 2020, 02:54

              @mzimmers
              Every spin box has a QLineEdit for input.
              You can access it by QAbstractSpinBox::lineEdit(), but it is a protected function.
              I'm not sure whether the focus need to be cleared by the spin box or the line edit.

              M Offline
              M Offline
              mzimmers
              wrote on 16 Jul 2020, 19:01 last edited by
              #6

              @Bonnie ah, I see. I'd rather not go to the effort of sub-classing the spin box just to get access to the line edit. Thanks for the suggestion, though.

              J 1 Reply Last reply 16 Jul 2020, 19:13
              0
              • M mzimmers
                16 Jul 2020, 19:01

                @Bonnie ah, I see. I'd rather not go to the effort of sub-classing the spin box just to get access to the line edit. Thanks for the suggestion, though.

                J Offline
                J Offline
                JonB
                wrote on 16 Jul 2020, 19:13 last edited by JonB
                #7

                @mzimmers said in QSpinBox - is there a way to "force" lose focus?:

                @Bonnie ah, I see. I'd rather not go to the effort of sub-classing the spin box just to get access to the line edit. Thanks for the suggestion, though.

                So that you know, you don't have to subclass. Use

                QLineEdit *lineEdit = spinBox->findChild<QLineEdit *>();
                
                M 1 Reply Last reply 16 Jul 2020, 19:32
                2
                • J JonB
                  16 Jul 2020, 19:13

                  @mzimmers said in QSpinBox - is there a way to "force" lose focus?:

                  @Bonnie ah, I see. I'd rather not go to the effort of sub-classing the spin box just to get access to the line edit. Thanks for the suggestion, though.

                  So that you know, you don't have to subclass. Use

                  QLineEdit *lineEdit = spinBox->findChild<QLineEdit *>();
                  
                  M Offline
                  M Offline
                  mzimmers
                  wrote on 16 Jul 2020, 19:32 last edited by
                  #8

                  @JonB said in QSpinBox - is there a way to "force" lose focus?:

                  findChild

                  Now that is very cool...hadn't seen that before. I'm guessing that's a relatively expensive function, though, so you might not want to use it in a slot that's servicing something like a spin box?

                  Then again, today's computers are so damn fast...who knows.

                  J J 2 Replies Last reply 17 Jul 2020, 04:55
                  0
                  • M mzimmers
                    16 Jul 2020, 19:32

                    @JonB said in QSpinBox - is there a way to "force" lose focus?:

                    findChild

                    Now that is very cool...hadn't seen that before. I'm guessing that's a relatively expensive function, though, so you might not want to use it in a slot that's servicing something like a spin box?

                    Then again, today's computers are so damn fast...who knows.

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 17 Jul 2020, 04:55 last edited by
                    #9

                    @mzimmers said in QSpinBox - is there a way to "force" lose focus?:

                    I'm guessing that's a relatively expensive function

                    I don't think it will have any effect in a slot. It's not like spin box has thousands of line edits :-)

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

                    1 Reply Last reply
                    2
                    • M mzimmers
                      16 Jul 2020, 19:32

                      @JonB said in QSpinBox - is there a way to "force" lose focus?:

                      findChild

                      Now that is very cool...hadn't seen that before. I'm guessing that's a relatively expensive function, though, so you might not want to use it in a slot that's servicing something like a spin box?

                      Then again, today's computers are so damn fast...who knows.

                      J Offline
                      J Offline
                      J.Hilk
                      Moderators
                      wrote on 17 Jul 2020, 05:34 last edited by
                      #10

                      @mzimmers said in QSpinBox - is there a way to "force" lose focus?:

                      @JonB said in QSpinBox - is there a way to "force" lose focus?:

                      findChild

                      Now that is very cool...hadn't seen that before. I'm guessing that's a relatively expensive function, though, so you might not want to use it in a slot that's servicing something like a spin box?

                      Then again, today's computers are so damn fast...who knows.

                      Its fine if you call it on a QWidget that does not have many children, like for example a QSpinBox,

                      Just don't call it repeatedly on your MainWindow as it is recursive will will take considerable amount of time, even on modern computers!


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      1 Reply Last reply
                      2

                      9/10

                      17 Jul 2020, 04:55

                      • Login

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