QSpinBox - Clicking outside of the QSpinBox to trigger valueChanged?



  • 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?
    Thanks!

    Example of the behavior I have now after a click outside:
    https://www.dropbox.com/s/lsawg2lrj9kva3t/SpinBoxFocus.png?dl=0
    As you can see, not really natural to keep the SpinBox focused..

    code:

            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);

  • Moderators

    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 Qt::ClickFocus or Qt::StrongFocus using setFocusPolicy.


  • Lifetime Qt Champion

    Hi,

    IIRC, you can "activate" that option on your other widgets using setFocusPolicy with Qt::ClickFocus



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


  • Lifetime Qt Champion

    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 !


  • Moderators

    When you click in a web browser to loose focus on the url bar the focus does not go into void. It is transferred to the web page (if you clicked there) and then through javascript delivered to html elements. Just because you don't see any visible action on their part doesn't mean it's not happening. The html elements can be "focusable" as well and if they are not then the web page view area definitely is.

    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.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.