Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: QGraphicsView mouse events

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

    Hi,

    I have a custom QGraphicsView, that has a QGraphicsScene (not custom), which has custom QGraphicsItems.
    But the Items get no mouse evens.
    Here is what I did:
    - reimplemented QGraphicsView::mousePressEvent() (and the other events I need) - this gets called. (QGraphicsView::mousePressEvent() gets called in it as well)
    - QGraphicsItems have setFlags(QGraphicsItem::ItemIsMovable|QGraphicsIte m::ItemIsSelectable | flags())
    - reimplemented QGraphicsItems ::mousePressEvent() - not being called.

    So basically events are not propagated from view to items on the scene.
    I must be missing something but I can't see what - any suggestions?
    Throw anything that comes up your mind, it is probably something silly I just forgot...

    Thanks in advance.
    ==========================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.

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

    Did you try setting QGraphicsItem::setAcceptsHoverEvents to true on the items ??

  3. #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

    no, since I don't need hover events, juts plain click and move mouse events.
    But thanks for the suggestion.
    ==========================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. #4
    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

    in order to debug this I sub classed QGrapicsScene.
    The mouse event (in this case click event) gets delivered to the scene.
    In the mouse event handler in the scene however, if I ask mouseGrabberItem() - I get -> 0, which means no item is grabbing the mouse - which means no item is getting mouse events.
    Any idea idea what could be causing this?
    I can't recall nor do I see this in the docs that I need to explicitly enable mouse grabbing on items since its a default behaviour (beyond setting the selectable flag).
    (I am using Qt4.3 in this case, and grabMouse() is not yet available. But this should not be needed any how...)

    EDIT:
    all the items return true on acceptedMouseButtons ().testFlag( Qt::LeftButton)


    help?
    Last edited by high_flyer; 16th July 2008 at 17:29.
    ==========================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. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView mouse events

    Do you call the base class implementation of the view's mouse events? You need to do that in order for events to be delivered to items.

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

    Can u post some code which mimicks this behaviour.
    I tried catching mousePressEvent in of my code, and I do get the events. And I have these flags set - setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); .

    Probably you are missing some thing.
    QUERY : Are you calling base class QGraphicsScene::mousePressEvent() from you scene class ?? I guess you are not. I dont see any other reason unless we see ur code

    EDIT:
    Wysota posted before me, and ya it must be base class of view,,, not scene
    Last edited by aamer4yu; 16th July 2008 at 19:44.

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

    Hi,
    @wysota:
    yes I do.
    Here is the class chain mouse press evets:
    The QGraphicsView subclass (this gets called):
    Qt Code:
    1. void QCTimeLineWidget::mousePressEvent(QMouseEvent *event)
    2. {
    3. QGraphicsView::mousePressEvent(event);
    4.  
    5. if(event->button() == Qt::RightButton)
    6. {
    7. //do some stuff
    8. event->accept();
    9. }
    10. else event->ignore();
    11. }
    To copy to clipboard, switch view to plain text mode 

    The Scene subclass (this gets called), and "no mouse grabber" is also being printed:
    Qt Code:
    1. void SliderScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent )
    2. {
    3. std::cout<<"scene event"<<std::endl;
    4. if(!mouseGrabberItem()) std::cout<<"no mouse grabber"<<std::endl;
    5. QGraphicsScene::mousePressEvent(mouseEvent);
    6. }
    To copy to clipboard, switch view to plain text mode 

    the QGrphicsRectItem subclass - not getting called (but that is no surprise since there is no mouse grabber item in the scene - but I don't know why)
    Qt Code:
    1. CPLContainer::CPLContainer(QGraphicsItem *parent)
    2. {
    3. setBoundingRect(QRectF(0,0,20,20));
    4. setFlag(QGraphicsItem::ItemIsMovable);
    5. setFlag(QGraphicsItem::ItemIsSelectable);
    6. }
    7.  
    8. void CPLContainer::mousePressEvent ( QGraphicsSceneMouseEvent * event )
    9. {
    10. std::cout<<"CPLContainer mouse clicked"<<std::endl;
    11. QGraphicsRectItem::mousePressEvent(event);
    12. }
    To copy to clipboard, switch view to plain text mode 

    Thanks again for the help.
    ==========================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.

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

    Default Re: QGraphicsView mouse events

    I don't like the event->ignore() line of yours. Don't you think you should ignore an event if and only if both your handler and the base handler wants to ignore the event? It might be that the default handler accepts the event, but it gets ignored because you ignore it as it was not caused by right click and because of that Something Bad Happens. I don't know if that is the source of your problem (I doubt it), but you should double check that anyway. Despite that I think your item's handler should be getting called... Try extracting the smallest possible chunk of code that reproduces the problem.

  9. #9
    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

    Why do you call setBoundingRect and not setRect in your CPLContainer constructor? Perhaps your rect is empty and thus gets no mouse events?

  10. #10
    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 for you answers guys.
    @wysota: the ignore is not a problem since it is only present in the QGraphicsView subclass, and the scene does get the event.
    In addition, the original code did not have the ignore in it, I just added it as I started trying all kinds of things.
    In order to make a long story short, attached is a project (kdevelop) which illustrates the problem. (only three basically empty classes and a main)
    Maybe you can then help me find what it is that I am doing wrong.
    I know I am doing something wrong which is basic, but I really just don't see what!

    @caduel: setBaoundingRect() ( in my code) sets the rect that is returned by boundingRect(), which is the important thing.
    But in the code attached even this is stripped off (bounding rect is hard coded), and still the same behaviour.

    I really appreciate the help!
    Thanks.
    Attached Files Attached Files
    ==========================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.

  11. #11
    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

    i) note besides: the Makefile in your zip does not work for others
    ii) using Qt4.4 (Linux) your code seems to work

    running your app and clicking the red rectangle, I get:
    Hello from Qt 4!
    view pressed
    QCTimelineScene: mousePressEvent
    scene clicked
    view pressed
    QCTimelineScene: mousePressEvent
    scene clicked
    view pressed
    QCTimelineScene: mousePressEvent

    Should it do more?

  12. #12
    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

    i) note besides: the Makefile in your zip does not work for others
    of course not - this is why you have the pro file, run qmake on in it first! :-)

    yes it should do more - notice that the red rectangle item does not get the mouse click - only the view and the scene.
    ==========================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.

  13. #13
    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...

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

    high_flyer (23rd July 2008)

  15. #14
    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.

  16. #15
    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 11: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.

  17. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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).

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

    Sergex (23rd November 2011)

  19. #17
    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.

  20. #18
    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.

  21. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


  22. #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 16:42.

Similar Threads

  1. Replies: 9
    Last Post: 22nd June 2008, 23:26
  2. Replies: 2
    Last Post: 2nd April 2008, 15:19
  3. Weird behaviour of mouse events and QMenu pop-ups
    By Ishark in forum Qt Programming
    Replies: 1
    Last Post: 7th August 2007, 08:46
  4. mouse moving don't produce mouse events
    By coralbird in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2006, 07:13
  5. QStackerWidget and mouse events
    By high_flyer in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2006, 20: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.