Results 1 to 20 of 23

Thread: QGraphicsView mouse events

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsView mouse events

    add
    Qt Code:
    1. setRect(0,0,70,70);
    To copy to clipboard, switch view to plain text mode 
    to your constructor.
    The boundingRect is not enough...

  2. The following user says thank you to caduel for this useful post:

    high_flyer (23rd July 2008)

  3. #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: QGraphicsView mouse events

    thanks!
    You are correct!

    But then I don't understand the docs.
    In the docs it says:
    To write your own graphics item, you first create a subclass of QGraphicsItem, and then start by implementing its two pure virtual public functions: boundingRect(), which returns an estimate of the area painted by the item, and paint(), which implements the actual painting.
    why is boundingRect() pure virtual if setRect() must be called as well?

    Hmm. oh well, no time for that now, I need to work - big thanks!
    ==========================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.

  4. #3
    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: QGraphicsView mouse events

    hmm.... this is more complicated it seems.
    The setRect() was needed because it was a QGraphicsRectItem subclass.
    But QGraphicsItem subclasses, or for example QGraphicsPathItem do not have the setRect() method.
    And with these items I am back to my problem....

    Ideas?

    EDIT: I figured it out - following the analogy of the QGraphicsRectItem, then for QGraphicsPathItem setPath() must be called, or setXxxx() depending on the subclass specialisation.
    This bit should be added to the docs I think...
    Last edited by high_flyer; 23rd July 2008 at 10:18.
    ==========================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.

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

    Default Re: QGraphicsView mouse events

    If you reimplement boundingRect() and shape() you shouldn't need to use setXXXX methods. Maybe you forgot about the shape()? The base implementation should simply call boundingRect() (it's reimplemented in subclasses probably causing your problem).

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

    Sergex (23rd November 2011)

  7. #5
    Join Date
    Sep 2013
    Posts
    17
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView mouse events

    hey I am facing the same problem...
    I have subclassed QGraphicsView, QGraphicsScene, QGraphicsItem.
    I am adding this scene object to view, and item object to scene. I am implementing mouse press events for view subclass and item subclass. Item object's flags are also set.
    When I click on item object only view's press event gets called. I want to check if the item is clicked or not through press event of item but that is not getting called.
    How do I solve this problem?

    Thanks for help in advance.

  8. #6
    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

    The very same problem here. Reimplented shape(), it never gets called.

  9. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView mouse events

    Quote Originally Posted by Cruz View Post
    The very same problem here. Reimplented shape(), it never gets called.
    Did you remember to make it const? If you are using C++11 you can make sure the prototype is correct by using the "override" specifier.
    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.


  10. #8
    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.

  11. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView mouse events

    You are calling the base class implementation so the view forwards events to the scene which forwards them to the item.
    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.


  12. #10
    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 thought the logic was the other way around. The most specific item is called first, which would be the graphics item, and then the event is propagated up until some object accepts it.

    But anyhow, I HAVE to call the base implementation in order to use the functionality provided by the Graphics View Framework. And I also have to remimplement the mouse events in the view and the scene to do custom things. So how can I for example detect the the item is being dragged and not the view, if view and item both react to the mouse move event?

  13. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView mouse events

    Quote Originally Posted by Cruz View Post
    I thought the logic was the other way around. The most specific item is called first, which would be the graphics item, and then the event is propagated up until some object accepts it.
    You click on the view so it is the first one to get the event.

    But anyhow, I HAVE to call the base implementation in order to use the functionality provided by the Graphics View Framework. And I also have to remimplement the mouse events in the view and the scene to do custom things. So how can I for example detect the the item is being dragged and not the view, if view and item both react to the mouse move event?
    Store additional information somewhere that you have already handled the event and check that information on every level (view, scene, item) of event handling.
    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.


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.