Results 1 to 2 of 2

Thread: MouseMoveEvent not called when cursor over child widget

  1. #1
    Join Date
    Jul 2016
    Posts
    19
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default MouseMoveEvent not called when cursor over child widget

    Hello,
    In a QDialog, I would like to display the mouse coordinates when the mouse is above a widget which is included in the QDialog.
    Using "setMouseTracking", it works when the widget is a QLabel but it doesn't when it is a QGraphicsView : if the mouse moved above the qgraphicsview, the function mouseMoveEvent of the QDialog isn't called. Why ?

    Example:
    Qt Code:
    1. #include <QDialog>
    2. #include <QGridLayout>
    3. #include <QDebug>
    4. #include <QApplication>
    5. #include <QGraphicsView>
    6. #include <QGraphicsScene>
    7. #include <QLabel>
    8.  
    9. class MyDialog : public QDialog
    10. {
    11. public:
    12. MyDialog() : QDialog()
    13. {setMouseTracking(true);}
    14. protected:
    15. void mouseMoveEvent(QMouseEvent *event)
    16. {
    17. qDebug()<<QCursor::pos();
    18. }
    19. };
    20.  
    21. int main(int argc, char **argv){
    22. QApplication app(argc, argv);
    23. MyDialog dlg;
    24. QGridLayout *l = new QGridLayout(&dlg);
    25. QLabel* label1 = new QLabel("Above Label");
    26. label1->setMouseTracking(true);
    27. QGraphicsScene* scene = new QGraphicsScene(0, 0, 100, 100);
    28. QGraphicsView* view = new QGraphicsView(scene);
    29. view->setMouseTracking(true);
    30. l->addWidget(label1,0, 0);
    31. l->addWidget(view, 0, 1, 2, 1);
    32. return dlg.exec();
    33. }
    To copy to clipboard, switch view to plain text mode 

    Thank you.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: MouseMoveEvent not called when cursor over child widget

    This part of the QGraphicsView docs seems interesting:
    You can interact with the items on the scene by using the mouse and keyboard. QGraphicsView translates the mouse and key events into scene events, (events that inherit QGraphicsSceneEvent,), and forward them to the visualized scene. In the end, it's the individual item that handles the events and reacts to them. For example, if you click on a selectable item, the item will typically let the scene know that it has been selected, and it will also redraw itself to display a selection rectangle. Similiary, if you click and drag the mouse to move a movable item, it's the item that handles the mouse moves and moves itself. Item interaction is enabled by default, and you can toggle it by calling setInteractive().
    As a test you can try to call setInteractive(false) and see if this changes things.
    Depending on your need to interact with your view you might need to reimplent the mouse event handlers to forward the events to your dialog.

    There is another approach as well:
    you can install an event filter on your view:
    See http://doc.qt.io/qt-5/qobject.html#installEventFilter for more details on that.

    A third option is to try to use grabMouse():
    http://doc.qt.io/qt-5/qwidget.html#grabMouse
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 1
    Last Post: 11th May 2016, 08:54
  2. Replies: 0
    Last Post: 20th January 2012, 06:50
  3. Replies: 4
    Last Post: 23rd November 2011, 00:42
  4. mouseMoveEvent: KDE moving widget
    By NullPointer in forum Newbie
    Replies: 1
    Last Post: 17th August 2011, 15:57
  5. Replies: 1
    Last Post: 11th March 2011, 20:34

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.