PDA

View Full Version : override wheelEvent for QScrollBar



ChasW
24th January 2007, 23:03
I want to override the number of steps per wheel movement to a QScrollBar object that comes from a QWheelEvent.

I have the scrollbar object initialized as a child within a parent widget.

I have managed to handle wheelEvent for the parent widget, but I am confused as to how to do this for the child scrollbar object.

I have read this: http://doc.trolltech.com/4.2/eventsandfilters.html

but I am still not sure how to override it for the scrollbar object.

Any pointers? (pun not intended) :p

jacek
24th January 2007, 23:34
http://doc.trolltech.com/4.2/qapplication.html#wheelScrollLines-prop

ChasW
25th January 2007, 05:57
http://doc.trolltech.com/4.2/qapplication.html#wheelScrollLines-prop

Thank you. I did not think to look in the QApplication for this.

It seems that is an application wide setting. Correct me if this is not a valid assumption.

What is the procedure for overriding a specific scrollbar child object wheelEvent and not all scrollbars in the application?

aamer4yu
25th January 2007, 07:37
Simply override wheelEvent ( QWheelEvent * event ) for the QScrollBar :)

ChasW
25th January 2007, 08:51
Simply override wheelEvent ( QWheelEvent * event ) for the QScrollBar :)

That is precisely what I am doing from the parent widget, but it is only catching wheel events forthe parent, not the scrollbar. I realize I am missing something here...

jpn
25th January 2007, 09:00
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);

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
}


Subclassing is definitely the cleaner way to do it. You can set the subclassed scroll bar to any QAbstractScrollArea descendant class.

aamer4yu
25th January 2007, 09:50
Originally Posted by aamer4yu
Simply override wheelEvent ( QWheelEvent * event ) for the QScrollBar

I meant to say override for the subclass... sorry I was not clear enough ;)