Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QSpinBox - Clicking outside of the QSpinBox to trigger valueChanged?
maximus last edited by A Former User
I'd like my QSpinBox to loose focus and trigger valueChanged when the user click somewhere outside the QSpinBox.
This is usually the normal way to handle focus, so I don't understand why it's not using this way as default with QSpinBox.
Now the focus is only lost when I click on another button, or another QSpinBox, but not when clicking my QWidget.
Is there a way to force this behavior?
Example of the behavior I have now after a click outside:
As you can see, not really natural to keep the SpinBox focused..
QSpinBox *spinBoxDifficulty = new QSpinBox(widgetTopForSpinBox); spinBoxDifficulty->setButtonSymbols(QAbstractSpinBox::PlusMinus); spinBoxDifficulty->setMinimum(1); spinBoxDifficulty->setMaximum(200); spinBoxDifficulty->setSuffix("%"); spinBoxDifficulty->setFixedHeight(30); spinBoxDifficulty->setFixedWidth(55); spinBoxDifficulty->setValue(100); spinBoxDifficulty->setStyleSheet("background-color : rgb(35,35,35); color : white;"); spinBoxDifficulty->setKeyboardTracking(false);
This is usually the normal way (...)
Is it? Could you give an example of that?
Focus can only be moved away to a widget that can receive it. If you click on the "empty" space between input controls what should actually be focused? Do you want to "tab" to empty space too?
If you really want to force your "background" to receive focus you can change its focus policy to
IIRC, you can "activate" that option on your other widgets using setFocusPolicy with Qt::ClickFocus
maximus last edited by
Working by setting ClickFocus on my QDialog.
For me, it's always been natural to loose focus on an editing object (lineEdit, SpinBox) when you click on any other element. If you click an "empty" space, I don't want to focus anything else, just loose focus on the actual element, even more if it's in editing mode.
Good example is your webBrowser URL bar, you could compare that to a QLineEdit.
Thanks for the tip, working fine!
For the case you mention, generally, you click on something that can make use of the focus e.g. keyboard navigation, scrolling with a mouse wheel etc. In your current case, your other widgets don't offer any option like that, that's why clicking on them don't change the focus.
You're welcome !
The behavior you describe might be natural to you, but in general it's not a good thing. You can't "just loose focus". It has to go somewhere, otherwise it breaks the concept of "tabbing". What should be focused after you "just lost" focus on a 99th control in a 100 control window and press tab again? Should next tab go back to the first control? That would make you mad I bet. As the focus goes somewhere it's not really a good thing to make non-user-interactable (if that's even a word) widgets taking focus. Why would I want to have focus on the "background" when I tab through a widget contents?
I can't make you do anything of course but please at least consider these points. You can have a value change for example when you hit "Enter" on the control without it loosing focus.
maximus last edited by
What I was asking is more about loosing the "edit mode" when you click elsewhere, focus could still be on the same element if the new item you clicked is not focusable, but keeping the QWidget in edit mode is confusing. When you press tab you would go to the next focusable element. I guess we can make a difference from "the Widget is focused" and "the widget is currently being edited", I think 2 different mode should have 2 different looks not confuse users. This is the way it's used on web page nowadays.
Thanks for the explanation!