PDA

View Full Version : Accelerating a QSpinBox value change



dvmorris
11th August 2007, 20:09
Is there any way to implement a feature like photoshop's dialog boxes where if you hold SHIFT and change values with the arrow keys or the scroll wheel, the values increment by multiples of 10?

Does that make sense? I assume it would involve subclassing QSpinBox or QDoubleSpinBox, but I'm not quite sure how best to go about it. Thanks for the help,

dave

marcel
11th August 2007, 20:14
It is possible. You have to subclass QSpinBox and:

1.Override wheelEvent.
Here you will look at QWheelEvent::modifiers to see if SHIFT modifier is pressed.
If it is pressed then you can look at QWheelEvent::delta() and multiply it by 10 and add this value to the current spinbox value.

2.Override keyPressEvent.
Also, you look at QKeyEvent::modifiers() for the SHIFT modifier.
Next you test for the up/down arrows and act accordingly.

Regards

wysota
11th August 2007, 20:42
Or override keyPressEvent and keyReleaseEvent and adjust the singleStep property of the spinbox there.

dvmorris
11th August 2007, 21:01
Ok, both of those make sense. I think I will go with the subclassing option since I am using at least 100 different spinboxes throughout the application. Thanks for the tips,

dave

dvmorris
12th August 2007, 17:14
found this in the source code for qabstractspinbox.cpp


#ifndef QT_NO_WHEELEVENT
void QAbstractSpinBox::wheelEvent(QWheelEvent *event)
{
const int steps = (event->delta() > 0 ? 1 : -1);
if (stepEnabled() & (steps > 0 ? StepUpEnabled : StepDownEnabled))
stepBy(event->modifiers() & Qt::ControlModifier ? steps * 10 : steps);
event->accept();
}
#endif

and this inside the keyPressEvent function:


switch (event->key()) {
case Qt::Key_PageUp:
case Qt::Key_PageDown:
steps *= 10;


looks like I don't really need to waste time reimplementing this as it already kind of does what I want it to do. I wish that were documented somewhere. That's a nice feature to know about...