PDA

View Full Version : QSpinBox interval



Jordan
24th May 2010, 21:00
Hi,

in a SpinBox you can only set an interval which is going from min to max. Is it possible to set other interval such as 1,10-20. So when you have an value of 1 and click the up arrow you get a 20. And after decreasing the 20 ther is a 1. How can I solve this problem?

Thanks

SixDegrees
24th May 2010, 21:07
Just do whatever mapping between spinbox index and internal indexes you need to within your code. If the variable you want to wind up with a value in is called i, and the spinbox index is idx, then in your code say something like

if (idx == 1)
i = 1;
else
i = 10;

or whatever sort of transformation makes sense.

Jordan
24th May 2010, 21:12
But with this you can enter something wrong by writing s.th. with the keyboard to the box.

Talei
24th May 2010, 21:47
QSpinbox don't allow You to write something wrong, i.e. letters instead of numbers etc.
You can use QDesigner (in QCreator) and set-up spinbox like: min: 0, max: 200, singleStep: 20, but that way, in Your example next value will be 21 not 20, so I think SixDegrees suggestion is more flexible that way.

Jordan
24th May 2010, 21:58
I have tried several things but don't get it to work. What do you mean with mapping.

Talei
25th May 2010, 05:32
What he meant is:
use spinbox signal valueChanged(int);
That way each time value change You will get value in your slot. Then do something like was posted earlier, i.e. check if value is < 20 if true set spinbox value to 1, if false don't do anything or set the value Yourself. That way you can do almost anything with spinbox, i.e. setting different singleStep depending on the actual value range.


if( a == 2) ui->spinBox->setValue( 20 );
if( a == 19) ui->spinBox->setValue( 1 );
These code do the trick, paste it in slot connected to signal valueChanged(int );, a is int slot value, and ui->spinBox is a spinbox placed on a form. Range is 0,1,20-30... (by default spinbox settings)

Jordan
25th May 2010, 09:41
Yes but with this Method you can't enter 22 or so because this is blocked. The only way is to disable KeyboardTracking.

tbscope
25th May 2010, 09:51
Simply subclass QSpinBox and reimplement stepUp() and stepDown().

Jordan
25th May 2010, 09:56
Can you please outline how to reimplement for example stepDown? And I make my forms with QDesigner. Is it possible to place custom widgets in the Designer?

tbscope
25th May 2010, 11:07
Hmmm, forget a simple subclass.
After looking at the source code of QAbstractSpinBox, I see it doesn't use spinUp() and spinDown() internally, instead it uses spinBy