PDA

View Full Version : Custom range for a Spinbox?



astodolski
21st August 2013, 14:08
Is it possible to create a custom range for a spinbox? Additionally, that range could be changed at run time. The range would have a initial value (design time) of -20 to 25. Those values are for a spinner that determines zoom ratio for a loaded image. The reason for the custom range is that I cannot have zero in the range of values - either the initial range or any calculated range thereafter. If re-implementing the class, how then would you keep the spinner from selecting zero in the range from minimum to maximum? It would have to, for instance go from -1 to 1 or vice-versa.

If I created a custom spinner class and in designer promoted the spinner to use it, using the validate method doesn't help in "skipping" zero when clicking up/down within the range, unless I am missing something.

Thanks in advance.

wysota
21st August 2013, 14:21
You can either reimplement fixup() or reimplement textFromValue() and valueFromText().

astodolski
21st August 2013, 17:49
You can either reimplement fixup() or reimplement textFromValue() and valueFromText().

Thanks obi wan wysota.

I looked at the icons example for a possible solution to refer to. This shows the use of re-implemented methods (the last two of the three mentioned) except fixup();

I put together a re-implemented fixup as follows:




void ScaleSpinBox::fixup(QString &str) const
{
bool ok;
int value = str.toInt(&ok, 10);

if(ok)
{
if (value == 0)
{
value = -1;
str = QString::number(value);
}
}

else
{
QSpinBox::fixup(str);
}
}




The logic is just a quick stab at getting something started. However, it never gets called.

wysota
21st August 2013, 18:09
fixup is called when the validator states the input is not valid. Thus you probably have to reimplement validate() as well. You can even fix the value there as well. However remember that in your case "0" is a perfectly good intermediate value.

astodolski
21st August 2013, 19:35
fixup is called when the validator states the input is not valid. Thus you probably have to reimplement validate() as well. You can even fix the value there as well. However remember that in your case "0" is a perfectly good intermediate value.

Yes I see that zero being valid always returned Acceptable :( . I guess a test range that never sees a zero and then go outside that range would force a call to QAbstractSpinBox::fixup(). In fixup() I then have to come up with handling the prior value to determine what the stepBy value would be. Perhpaps it be better to just handle the cached value of the last value of the spinbox and then call directly stepBy.

wysota
21st August 2013, 21:28
It won't work if someone just types "0" instead of using arrows.