Results 1 to 20 of 23

Thread: QGraphicsView mouse events

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #20
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView mouse events

    I did make it const, but I doubt this problem has anything to do with shape().

    In my case I discovered that I had an error in the boundingRect() definition. My bounding box had an offset to the item, so that the item would not report mouse events when the mouse was moving over it simply because the bounding box was somewhere else. Now that I fixed it, the mouse events do get delivered to the graphics item correctly. I can even remove the shape implementation (which does nothing else than the standard implementation anyways) and it still works.

    The one thing that remains to solve is that all three, the graphics item, the graphics scene, and the graphics view, all seem to receive the mouse events simultaneously. I was expecting in this situation that the graphics item consumes the event and it doesn't get propagated all the way up to the view. Is my expectation wrong, or am I not doing something right? Here is the code for the relevant places:

    Qt Code:
    1. class GraphicsViewWidget : public QGraphicsView
    2. {
    3. Q_OBJECT
    4.  
    5. GraphicsViewWidget(QWidget *parent = 0);
    6. ~GraphicsViewWidget();
    7.  
    8. protected:
    9. void mousePressEvent(QMouseEvent *event);
    10. void mouseReleaseEvent(QMouseEvent *event);
    11. void mouseMoveEvent(QMouseEvent *event);
    12. };
    13.  
    14. void GraphicsViewWidget::mousePressEvent(QMouseEvent *event)
    15. {
    16. qDebug() << "view press event";
    17. QGraphicsView::mousePressEvent(event);
    18. }
    19.  
    20. void GraphicsViewWidget::mouseMoveEvent(QMouseEvent *event)
    21. {
    22. qDebug() << "view mouse move";
    23. QGraphicsView::mouseMoveEvent(event);
    24. }
    25.  
    26. void GraphicsViewWidget::mouseReleaseEvent(QMouseEvent *event)
    27. {
    28. qDebug() << "view mouse release";
    29. QGraphicsView::mouseReleaseEvent(event);
    30. }
    31.  
    32.  
    33.  
    34. class MyScene : public QGraphicsScene
    35. {
    36. Q_OBJECT
    37.  
    38. ComState comState;
    39.  
    40. public:
    41. MyScene (QWidget *parent = 0);
    42. ~MyScene (){};
    43.  
    44. void mousePressEvent(QGraphicsSceneMouseEvent *event);
    45. void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    46. void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    47. };
    48.  
    49.  
    50.  
    51. MyScene::MyScene (QWidget *parent)
    52. : QGraphicsScene(parent)
    53. {
    54. setSceneRect(-100, -100, 200, 200);
    55.  
    56. addItem(&comState);
    57. comState.setFlags(QGraphicsItem::ItemIsMovable);
    58. }
    59.  
    60. void MyScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
    61. {
    62. qDebug() << "scene mouse press";
    63. QGraphicsScene::mousePressEvent(event);
    64. }
    65.  
    66. void MyScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    67. {
    68. qDebug() << "scene mouse move" << mouseGrabberItem();
    69. QGraphicsScene::mouseMoveEvent(event);
    70. }
    71.  
    72. void MyScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    73. {
    74. qDebug() << "scene mouse release";
    75. QGraphicsScene::mouseReleaseEvent(event);
    76. }
    77.  
    78.  
    79.  
    80. class ComState : public QGraphicsItem
    81. {
    82. public:
    83. double vx;
    84. double vy;
    85.  
    86. public:
    87. ComState(QGraphicsItem *parent = 0);
    88. ~ComState(){};
    89.  
    90. QRectF boundingRect() const;
    91. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    92.  
    93. void mousePressEvent(QGraphicsSceneMouseEvent *event);
    94. void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    95. void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    96. };
    97.  
    98.  
    99.  
    100. ComState::ComState(QGraphicsItem *parent)
    101. : QGraphicsItem(parent)
    102. {
    103. vx = 0;
    104. vy = 0;
    105. }
    106.  
    107. QRectF ComState::boundingRect() const
    108. {
    109. return QRectF(-0.1, -0.1, 0.2, 0.2);
    110. }
    111.  
    112. void ComState::mousePressEvent(QGraphicsSceneMouseEvent *event)
    113. {
    114. qDebug() << "item mouse press";
    115. }
    116.  
    117. void ComState::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    118. {
    119. qDebug() << "item mouse move";
    120. }
    121.  
    122. void ComState::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    123. {
    124. qDebug() << "item mouse release";
    125. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Cruz; 28th May 2014 at 15:42.

Similar Threads

  1. Replies: 9
    Last Post: 22nd June 2008, 22:26
  2. Replies: 2
    Last Post: 2nd April 2008, 14:19
  3. Weird behaviour of mouse events and QMenu pop-ups
    By Ishark in forum Qt Programming
    Replies: 1
    Last Post: 7th August 2007, 07:46
  4. mouse moving don't produce mouse events
    By coralbird in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2006, 06:13
  5. QStackerWidget and mouse events
    By high_flyer in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2006, 19:25

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
  •  
Qt is a trademark of The Qt Company.