PDA

View Full Version : QSpinBox / QDoubleSpinBox and valueChanged signal



tpf80
23rd October 2008, 04:23
I have a few QSpinBox and QDoubleSpinBox within a form which I use the signal "valueChanged()" in order to tell if the user changed the value in the spinbox. I noticed however that this signal also fires if I change the value within my program.

Is there a way to differentiate between the user changing the value, and the program changing the value itself?

for instance on a QLineEdit, I can use the "textEdited()" signal, which does not fire when the program sets the text, or on a QComboBox I can use "activated()" which doesn't fire unless the user changes the value.

RThaden
23rd October 2008, 08:48
Hi,

I had the same problem. There is no special signal as you mention for QLineEdit. I used the editingFinished signal in a derived class which is fired when the SpinBox loses focus or return is pressed and fired my own valueChangedOnEdit signal.

Regards,

Rainer

tpf80
23rd October 2008, 15:20
Ah thanks, I didn't see that one in the long list of items with the derived classes. This is a good start.

tpf80
23rd October 2008, 23:42
the editingFinished signal didn't work for me because it does not activate when the user clicks the up and down arrows on the spin box, nor does it activate if the user edits the field, but does not click on another field. In the end I used the valueChanged signal, but I made it work how I wanted to in this way:

1) I created a global flag true/false, set default to false.
2) when I change the field value without user interaction, I set the flag to true during the time the program is populating the spinbox, then when the program is done populating, it sets the flag back to false.
3) on the slot that is called by valueChanged, it checks if this flag is false, and if it is, then we can go and run the code in the slot. If the flag is true, then we know the program is in the process of populating the field, and so do not execute the slot code.
4) it is set up so that the user can not edit the field at the same time as the program, so that there is no possibility that the valueChanged signal would fire from a user edit, while the program edit is in progress. (not likely anyway because it happens so fast, but just in case)

invisible_uli
10th January 2010, 18:16
I have a quite similar problem... hope somebody can help me:

i've got a QSpinBox which sends its value to a server. Naturally I chose the valueChanged-Signal at first. But I didn't like the fact that when typing 155 the signal emits first the 1 then the 15 and at last the 155. So instead I tried using editingFinished. I don't have a problem with using the return-key to emit the signal after typing. BUT I want the Up and Down-Button to work without the return.

Is there any way to achieve this ?

I know it's possible to get to the QLineEdit of the SpinBox , is there also a way to get to the signal of the buttons ?
Or if I send the signal valueChanged to a function I create myself: Can I somehow find out in which way the signal was emitted (if it was only a number typed which i would ignore until the return button or if it was a mouse-click/ button-pressed) ??

I'd really appreciate the help !!

norobro
11th January 2010, 04:47
I think disabling keyboard tracking will produce the results that you want. Check out "setKeyboardTracking(bool)" in the docs.

invisible_uli
11th January 2010, 07:13
I think disabling keyboard tracking will produce the results that you want. Check out "setKeyboardTracking(bool)" in the docs.

Thanks so much norobro. I should have benn able to find that myself :o ...
"If keyboard tracking is disabled, the spinbox doesn't emit the valueChanged() signal while typing. It emits the signal later, when the return key is pressed, when keyboard focus is lost, or when other spinbox functionality is used, e.g. pressing an arrow key." :cool:

Anyway, thanks again for the quick answer !!!!

abdallagafar
26th December 2010, 23:20
Is there a way to differentiate between the user changing the value, and the program changing the value itself?


There is a generic trick for this sort of problems. Say you have xSpinBox that controls the value of variable x. the idea is to set the spinbox AFTER you set the variable:
x = newx; xSpinBox->setValue(x);
the first line in the valueChanged slot should then be like this:
if (value == x) return;
This elliminates the need for flags, and allows changing the variable at any time inside the program.

jillian
28th July 2013, 04:14
I have a few QSpinBox and QDoubleSpinBox within a form which I use the signal "valueChanged()" in order to tell if the user changed the value in the spinbox. ... Is there a way to differentiate between the user changing the value, and the program changing the value itself?

I had the same problem, which I solved in this manner: In my slot, I check to see if my QSpinBox hasFocus(). If it doesn't, then I know that the change in value is programmatic rather than user input. It seems to work...

Oussama
1st September 2018, 00:19
The easiest way is to use QObject::blockSignals (http://doc.qt.io/qt-5/qobject.html#blockSignals):


spinBox->blockSignals(true);
spinBox->setValue(5); // now the valueChanged signal isn't emitted
spinBox->blockSignals(false);