Results 1 to 2 of 2

Thread: Handling QMouseEvents

  1. #1
    Join Date
    May 2009
    Posts
    21
    Thanks
    4
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Handling QMouseEvents

    Hi,

    I've implemented a sub class of QGraphicsView (MyGraphicsView). I want to install an event filter on this view which handles all "user events". But I've got some trouble with the event handling in Qt.

    I thought the propagation order is always:

    installed filter -> QObject::event()-> event-specific function (like paintEvent)

    I've experienced that this is only true for some events like e.g. QPaintEvent and QKeyEvent in case of QMouseEvent or QWheelEvent the propagation order is:

    event-specific function -> installed filter -> QObject::event().

    So if I would like to filter QMouseEvents or QWheelEvents for my sub class of QGraphicsView I have to reimplement the event-specific functions (e.g. mouseMoveEvent) there I have to ignore() the event so that the installed filter receives the event. If not doing so the event filter doesn't receive these events.

    To me this looks a bit strange. The Qt 4.5 Documentation says : "An event filter gets to process events BEFORE the target object does ..."

    Is this a bug a feature or is there something wrong with my code?

    I've attached a code snippet and the result for QPaintEvent, QMouseEvent, QKeyEvent and QWheelEvent.

    Thank you in advance

    Markus

    PS: I'm using Qt 4.5.1.


    Qt Code:
    1. #include "filterevents.h"
    2.  
    3. filterEvents::filterEvents(QWidget *parent)
    4. : QWidget(parent)
    5. {
    6. // set up from with my graphics view
    7. view = new MyGraphicsView();
    8. layout = new QHBoxLayout();
    9. layout->addWidget(view);
    10. setLayout(layout);
    11.  
    12. // install event filter
    13. view->installEventFilter(this);
    14. }
    15.  
    16. bool filterEvents::eventFilter (QObject * object, QEvent * event)
    17. {
    18. if(event->type() == QEvent::KeyPress ||
    19. event->type() == QEvent::Wheel ||
    20. event->type() == QEvent::MouseButtonPress ||
    21. event->type() == QEvent::Paint)
    22. qDebug() << event << "in filter.";
    23.  
    24. return QObject::eventFilter(object, event);
    25. }
    26.  
    27.  
    28.  
    29. #include "MyGraphicsView.h"
    30. #include <QDebug>
    31.  
    32. MyGraphicsView::MyGraphicsView() {
    33. setMouseTracking(true);
    34. }
    35.  
    36. MyGraphicsView::~MyGraphicsView() {
    37. }
    38.  
    39.  
    40. bool MyGraphicsView::event(QEvent * event) {
    41. if(event->type() == QEvent::KeyPress ||
    42. event->type() == QEvent::Wheel ||
    43. event->type() == QEvent::MouseButtonPress ||
    44. event->type() == QEvent::Paint)
    45. qDebug() << event << "in my graphics view (event).";
    46.  
    47. return QGraphicsView::event(event);
    48. }
    49.  
    50.  
    51. void MyGraphicsView::paintEvent(QPaintEvent * event) {
    52. qDebug() << event << "in my graphics view (paintEvent).";
    53. event->ignore();
    54. }
    55.  
    56.  
    57. void MyGraphicsView::wheelEvent(QWheelEvent * event) {
    58. qDebug() << event << "in my graphics view (wheelEvent).";
    59. event->ignore();
    60. }
    61.  
    62.  
    63. void MyGraphicsView::keyPressEvent(QKeyEvent * event) {
    64. qDebug() << event << "in my graphics view (keyPressEvent).";
    65. event->ignore();
    66. }
    67.  
    68. void MyGraphicsView::mousePressEvent(QMouseEvent * event) {
    69. qDebug() << event << "in my graphics view (mousePressEvent).";
    70. event->ignore();
    71. }
    To copy to clipboard, switch view to plain text mode 


    Result for QPaintEvent, QMouseEvent, QKeyEvent and QWheelEvent:
    -----------------------------------------------------------------------------------

    QPaintEvent(0xbfc7714c) in filter.
    QPaintEvent(0xbfc7714c) in my graphics view (event).
    QPaintEvent(0xbfc76f0c) in my graphics view (paintEvent).

    QMouseEvent(MouseButtonPress, 1, 1, 0) in my graphics view (mousePressEvent).
    QMouseEvent(MouseButtonPress, 1, 1, 0) in filter.
    QMouseEvent(MouseButtonPress, 1, 1, 0) in my graphics view (event).

    QKeyEvent(KeyPress, 1000013, 0, """", false, 1) in filter.
    QKeyEvent(KeyPress, 1000013, 0, """", false, 1) in my graphics view (event).
    QKeyEvent(KeyPress, 1000013, 0, """", false, 1) in my graphics view (keyPressEvent).

    QWheelEvent(120) in my graphics view (wheelEvent).
    QWheelEvent(120) in filter.
    QWheelEvent(120) in my graphics view (event).

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

    Default Re: Handling QMouseEvents

    Quote Originally Posted by Lodorot View Post
    I've experienced that this is only true for some events like e.g. QPaintEvent and QKeyEvent in case of QMouseEvent or QWheelEvent the propagation order is:

    event-specific function -> installed filter -> QObject::event().
    No, it's not. But it is the viewport() that is the destination of the event and the event gets delegated to the view itself thus first the event handler from the view gets called and if it doesn't handle the event then the event gets propagated to the parent and then is caught by the event filter. Install the event filter on the viewport() and it will work as expected.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    Lodorot (4th July 2009)

Similar Threads

  1. Suggested Error Handling
    By Max Yaffe in forum Qt Programming
    Replies: 6
    Last Post: 15th July 2014, 17:29
  2. Replies: 2
    Last Post: 17th April 2009, 05:56
  3. large file handling
    By sakthi in forum Qt-based Software
    Replies: 1
    Last Post: 30th October 2008, 00:34
  4. Exception handling in Qt 4.2.2
    By vishal.chauhan in forum Qt Programming
    Replies: 1
    Last Post: 27th February 2007, 09:47
  5. need help for tree Handling concept
    By jyoti in forum Qt Programming
    Replies: 5
    Last Post: 24th November 2006, 04:52

Tags for this Thread

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.