QSpinBox doesn't emit valueChanged signal when value is 0 [SOLVED]
Here is my problem : I am creating a QSpinBox which minimum value is 1. I did the setRange accordingly, so when I use the arrows to modify the value my minimum value is respected.
If I enter a 0 value from keyboard, the value is corrected whenever the spinbox looses focus. However, if I click on another button (Ok button, closing the dialog) without explicitly changing focus out of the spinbox, the value displayed to the user is still 0. The property value is the last good value (non-zero), but as the user last saw "0", this behaviour is confusing.
I am modifying this by extending the QSpinBox class and handling the valueChanged signal myself but I found another strange behaviour : the valueChanged() signal is emitted everytime the spinbox is changed from keyboard (with keyboardTracking = true) but not when the value is 0.
Does anyone have an explanation for this? I was going to report this as a bug but I would like to be sure first the problem is not with something I'm missing...
What you have in your spinbox is known as an intermediate state: it is not valid, but it might _become _valid. For instance: say your spinbox contains the value 20. Now, your user edits the spinbox and deletes the 2. Your spinbox now has the value 0 (which is invalid). However, if the user wants to change the value to 30, he'd now type a 3 and the value is valid again. If you'd have blocked the user at the 0, or even 'corrected' the value to 1, he would be very frustrated indeed. So, you can't do that.
What you might do, is subclass QSpinBox. That gives you access to the line edit inside, and you can respond to all text changes there, perhaps using a [[doc:QValidator]]. You can then add a signal to the widget that emits the validate state: Invalid, Intermediate, Acceptable. You might simply disable your OK button if the state is not Acceptable.
Your point is right... there is no way to correct the value before leaving the focus out of the spinbox and without interfering with the user...
I subclassed the SpinBox and handled the keyboardevents myself to achieve this, in a similar way to your suggestion. Thank you for your answer.