PDA

View Full Version : Reversing the arrows in a QSpinBox



zarkzervo
2nd March 2011, 13:45
How can I "flip" the arrows in a QSpinBox so that when you push down, the number increases and when you push up, the number decreases?

I want to use the spinbox to set depth-value so my users found it more intuitive that down would be an increase of depth.

I have subclassed the QSpinBox (actually a QDoubleSpinBox) and overridden the wheelEvent so that one is taken care of.

I could look for mousepress and see if the mouse is inside the up or down arrow, maybe, but when the value is 0 (minValue), the down-arrow is gray, so just overriding the values here would not be any good. I want the up-arrow to be gray when value is at minValue.

I've tried googling for this and the closest I've come is to completely redefine the QStyle painting routine. I found this also in "C++ GUI programming with Qt 4". This seemed overly complex and I don't want to redefine the style if this means that I would set the spinBox style to something that may be unlike the native look in different OS'es.

Is there any painting routine I could relatively easily override where I just reposition the arrows? Or any other place I should look?

wysota
2nd March 2011, 13:54
Reimplement QAbstractSpinBox::stepBy() :

void MySpinbox::stepBy(int step) {
QSpinBox::stepBy(-step);
}

zarkzervo
2nd March 2011, 14:10
Thanks, but it did not work for the extreme values. When the value is 0.0, the step-function is prevented. I will look more into this. Also the problem with the gray arrow was still there

wysota
2nd March 2011, 14:21
Let's try something else then. Reimplement valueFromText and textFromValue to work on negative numbers and set your maximum as 0 and your minimum as -something. Then other things should work properly.


double MySpinbox::valueFromText(const QString &text) const { return -QDoubleSpinBox::valueFromText(text); }
QString MySpinbox::textFromValue(double value) const { return QDoubleSpinBox::textFromValue(-value); }

zarkzervo
2nd March 2011, 14:46
This seems to fix it for the textFromValue-part, but it looks like the validator prevents the input of positive numbers when I have a range of [-45.0, 0.0]

I will look into that. I'm much closer to this working now than a few hours ago. Thanks!

wysota
2nd March 2011, 15:31
You can reimplement validate() for that.

zarkzervo
4th March 2011, 12:32
I also subclassed QDoubleSpinBox to override the scrolling function.