Results 1 to 7 of 7

Thread: override wheelEvent for QScrollBar

  1. #1
    Join Date
    Jan 2007
    Posts
    26
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default override wheelEvent for QScrollBar

    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)

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: override wheelEvent for QScrollBar


  3. The following user says thank you to jacek for this useful post:

    ChasW (25th January 2007)

  4. #3
    Join Date
    Jan 2007
    Posts
    26
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: override wheelEvent for QScrollBar

    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?

  5. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: override wheelEvent for QScrollBar

    Simply override wheelEvent ( QWheelEvent * event ) for the QScrollBar

  6. #5
    Join Date
    Jan 2007
    Posts
    26
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: override wheelEvent for QScrollBar

    Quote Originally Posted by aamer4yu View Post
    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...

  7. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: override wheelEvent for QScrollBar

    You have two ways;
    1. Subclass the scrollbar and override the wheelEvent() for it, instead of the parent
    2. Install an event filter on the scrollbar (see the example below)


    Qt Code:
    1. scrollbar->installEventFilter(this);
    2.  
    3. bool ThisClass::eventFilter(QObject* object, QEvent* event)
    4. {
    5. if (event->type == QEvent::Wheel)
    6. {
    7. // original event
    8. QWheelEvent* w = static_cast<QWheelEvent*>(event);
    9.  
    10. // construct a modified version ("2" instead of w->delta())
    11. QWheelEvent d(w->pos(), w->globalPos(), 2, w->buttons(), w->modifiers(), w->orientation());
    12.  
    13. // the ugly part comes here; this event filter must be temporarily
    14. // removed to be able to deliver the modified event to the receiver
    15. // (otherwise causes an infinite loop)
    16. object->removeEventFilter(this);
    17. QApplication::sendEvent(object, &d);
    18. object->installEventFilter(this);
    19.  
    20. return true; // filter the original event out
    21. }
    22. return false; // pass other events
    23. }
    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.
    Last edited by jpn; 25th January 2007 at 09:31. Reason: added the example
    J-P Nurmi

  8. The following user says thank you to jpn for this useful post:

    ChasW (25th January 2007)

  9. #7
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: override wheelEvent for QScrollBar

    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

Similar Threads

  1. Override QSplitter to draw new handle
    By grogan in forum Qt Programming
    Replies: 4
    Last Post: 27th September 2006, 03:40

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.