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.
-
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?
@Joachim-W
I assume the answer is that you cannot do these things. -
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? -
@Joachim-W
IIRC all such Widgets have avalueChanged
signal that is emited when ever the value changed(either software or user) and aneditingFinished()
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 valueChangedUsera function that is connected to the valueChanged signal
MySpinbox::customChanged(int value){ if(this->hasFocus()) emit valueChangedUser(value); else emit valueChangeSystem(value); }
-
Hi @Joachim-W,
there are indeed some widgets that handle both cases differently - for example
QLineEdit
which hastextEdited()
andtextChanged()
.Unfortunately
QSpinBox
only hasvalueChanged()
so you can't distinguish it here.I think you could subclass
QSpinBox
and change it sosetValue()
returns another signal to distinguish between user and program changes.You would have to do this for all your widgets, however.