QSpinBox - is there a way to "force" lose focus?
-
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)?
-
I haven't tested that but did you try calling QWidget::clearFocus() on the QLineEdit?
-
I haven't tested that but did you try calling QWidget::clearFocus() on the QLineEdit?
-
@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.
-
@mzimmers
Every spin box has a QLineEdit for input.
You can access it byQAbstractSpinBox::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. -
@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.
@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 *>();
-
@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 *>();
@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.
-
@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.
@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 :-)
-
@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.
@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!