You have two ways;
- Subclass the scrollbar and override the wheelEvent() for it, instead of the parent
- Install an event filter on the scrollbar (see the example below)
scrollbar->installEventFilter(this);
{
if (event
->type
== QEvent::Wheel) {
// original event
// construct a modified version ("2" instead of w->delta())
QWheelEvent d
(w
->pos
(), w
->globalPos
(),
2, w
->buttons
(), w
->modifiers
(), w
->orientation
());
// the ugly part comes here; this event filter must be temporarily
// removed to be able to deliver the modified event to the receiver
// (otherwise causes an infinite loop)
object->removeEventFilter(this);
object->installEventFilter(this);
return true; // filter the original event out
}
return false; // pass other events
}
scrollbar->installEventFilter(this);
bool ThisClass::eventFilter(QObject* object, QEvent* event)
{
if (event->type == QEvent::Wheel)
{
// original event
QWheelEvent* w = static_cast<QWheelEvent*>(event);
// construct a modified version ("2" instead of w->delta())
QWheelEvent d(w->pos(), w->globalPos(), 2, w->buttons(), w->modifiers(), w->orientation());
// the ugly part comes here; this event filter must be temporarily
// removed to be able to deliver the modified event to the receiver
// (otherwise causes an infinite loop)
object->removeEventFilter(this);
QApplication::sendEvent(object, &d);
object->installEventFilter(this);
return true; // filter the original event out
}
return false; // pass other events
}
To copy to clipboard, switch view to plain text mode
Subclassing is definitely the cleaner way to do it. You can set the subclassed scroll bar to any QAbstractScrollArea descendant class.
Bookmarks