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?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 6 Posters 4.2k Views 2 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on 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 last edited by
      #2

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

      mzimmersM 1 Reply Last reply
      2
      • B Bonnie

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

        mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #3

        @Bonnie what QLineEdit?

        B 1 Reply Last reply
        0
        • mzimmersM mzimmers

          @Bonnie what QLineEdit?

          B Offline
          B Offline
          Bonnie
          wrote on 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.

          mzimmersM 1 Reply Last reply
          1
          • Kent-DorfmanK Offline
            Kent-DorfmanK Offline
            Kent-Dorfman
            wrote on 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.

            If you meet the AI on the road, kill it.

            1 Reply Last reply
            1
            • B Bonnie

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

              mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on 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.

              JonBJ 1 Reply Last reply
              0
              • mzimmersM mzimmers

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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on 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 *>();
                
                mzimmersM 1 Reply Last reply
                2
                • JonBJ JonB

                  @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 *>();
                  
                  mzimmersM Offline
                  mzimmersM Offline
                  mzimmers
                  wrote on 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.

                  jsulmJ J.HilkJ 2 Replies Last reply
                  0
                  • mzimmersM mzimmers

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

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 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
                    • mzimmersM mzimmers

                      @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.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on 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

                      • Login

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