Determine whether widget value was changed by user or by software?



  • Control widgets like QSpinBox, QCheckBox etc emit signals like valueChanged, stateChanged etc. These signals are caused either by direct user action upon the widget, or by external calls of setValue, setChecked etc.

    I'd like to treat these two cases differently. Is there a straightforward way to find out where the signal comes from?

    Or is there a way to implement alternative forms of setValue, setChecked etc that do not emit valueChanged, stateChanged etc?



  • That's interesting,

    The first approach that comes to my mind is to determine if the mouse is in the widget you're using:

    http://doc.qt.io/qt-5/qmouseevent.html#pos

    I'm neither sure if QTest functions such as QTest::mouseClick move the mouse to the widget nor your specific implementation.



  • @Joachim-W
    I assume the answer is that you cannot do these things.


  • Qt Champions 2017

    Hi
    Can I ask why you need this ?
    We sometimes block signals to avoid GUI triggering user function during
    loading data etc.
    Is that what you need it fo r?



  • why I need this? - for logging user actions, and not the system's response to them.



  • @Joachim-W
    IIRC all such Widgets have a valueChanged signal that is emited when ever the value changed(either software or user) and an editingFinished() that only emits when an user interacted with the class- pressing enter or switching focus.

    Besides that the other option is subclassing for example QSpinBox,
    add 2 new signals e.g valueChangeSystem and valueChangedUser

    a function that is connected to the valueChanged signal

    MySpinbox::customChanged(int value){
         if(this->hasFocus())
            emit valueChangedUser(value);
         else
            emit valueChangeSystem(value);
    }
    

  • Qt Champions 2017

    Hi @Joachim-W,

    there are indeed some widgets that handle both cases differently - for example QLineEdit which has textEdited() and textChanged().

    Unfortunately QSpinBox only has valueChanged() so you can't distinguish it here.

    I think you could subclass QSpinBox and change it so setValue() returns another signal to distinguish between user and program changes.

    You would have to do this for all your widgets, however.



  • Thank you for your excellent suggestions. For the time being, 'hasFocus' solves my problem.


Log in to reply
 

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