Results 1 to 20 of 23

Thread: QGraphicsView mouse events

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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 16: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 18: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.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.