QSlider Value Changed Signal
Asked Answered
B

1

35

I'm using a QSlider (v4.6) for input as well as to provide feedback to the user. For the feedback I will be calling the setValue method. I'm trying to find a signal that will fire only if the user modified the value. The valueChanged signal fires when the user changed the value as well as when I call setValue. sliderMoved only fires when the user drags the slider (not when using the keyboard). I checked the API docs and can't seem to find anything. Am I missing something? This seems something that would be common. If there is no other signal, how would you recommend that I simulate this functionality? Should I set a flag before calling setValue, disconnect and reconnect the signal every time I call setValue...?

Bleachers answered 10/11, 2010 at 15:36 Comment(2)
Seems still valid for Qt 5's valueChanged signal.Carline
Note that setMinimum() and setMaximum() may adjust the slider value, triggering the valueChanged signal..Carline
A
52

Good question, I checked the API and also couldn't find a signal that would be triggered only if value was modified by user. The workaround you proposed may be the only option, just keep in mind that you don't have to disconnect / connect all signals, just use QObject::blockSignals method:

slider->blockSignals(true);
slider->setValue(x);
slider->blockSignals(false);

Hope that helps.

Aracelis answered 10/11, 2010 at 16:0 Comment(3)
Thanks Pawel! I didn't know about blockSignals. I guess that will have to do.Bleachers
You probably want to store the value returned by blockSignals(true) and use it in the second call instead of false. That way if some other code has already enabled signal blocking you won't mess up the state. (I hope this problem is unlikely, but defensive coding is a good idea anyway.)Dismay
This method can be applied in PySide (the python bindings), and I did apply. Result is as expected, except for the signals that are defined in Qt Designer. If you connect a slider to a progress bar in Qt Designer and say "when slider's value changed, set progress bar's value" and then block the slider's signal before updating its value, then slider's value is updated but progress bar's is not. That's why, disconnecting and connecting the signals is more appropriate.Este

© 2022 - 2024 — McMap. All rights reserved.