Results 1 to 5 of 5

Thread: Accelerating a QSpinBox value change

  1. #1
    Join Date
    Feb 2007
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Accelerating a QSpinBox value change

    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

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Accelerating a QSpinBox value change

    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

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Accelerating a QSpinBox value change

    Or override keyPressEvent and keyReleaseEvent and adjust the singleStep property of the spinbox there.

  4. #4
    Join Date
    Feb 2007
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Accelerating a QSpinBox value change

    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

  5. #5
    Join Date
    Feb 2007
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Accelerating a QSpinBox value change

    found this in the source code for qabstractspinbox.cpp

    Qt Code:
    1. #ifndef QT_NO_WHEELEVENT
    2. void QAbstractSpinBox::wheelEvent(QWheelEvent *event)
    3. {
    4. const int steps = (event->delta() > 0 ? 1 : -1);
    5. if (stepEnabled() & (steps > 0 ? StepUpEnabled : StepDownEnabled))
    6. stepBy(event->modifiers() & Qt::ControlModifier ? steps * 10 : steps);
    7. event->accept();
    8. }
    9. #endif
    To copy to clipboard, switch view to plain text mode 

    and this inside the keyPressEvent function:

    Qt Code:
    1. switch (event->key()) {
    2. case Qt::Key_PageUp:
    3. case Qt::Key_PageDown:
    4. steps *= 10;
    To copy to clipboard, switch view to plain text mode 

    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...

Similar Threads

  1. QSpinBox with checkbox
    By :db:sStrong in forum Qt Programming
    Replies: 4
    Last Post: 17th January 2007, 13:22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.