Results 1 to 12 of 12

Thread: mouse click in QGprahicsScene

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: mouse click in QGprahicsScene

    The correct way to do this is to first call the scene's event handler, and then check if the mouse event was accepted or ignored by the scene.

    Reimplement mousePressEvent in QGraphicsScene like this:

    Qt Code:
    1. void CustomScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
    2. {
    3. // First check if any items on the scene accept the event
    4. QGraphicsScene::mousePressEvent(event);
    5.  
    6. if (!event->isAccepted()) {
    7. // Someone clicked outside an item, or on an item that doesn't
    8. // accept mouse events.
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    This is better than checking if there are items at the mouse position with QGraphicsScene::items(), because certain items are transparent for mouse events. Like landscape items, in a game scene.
    Last edited by jacek; 8th December 2006 at 11:31. Reason: changed [quote] to [code]
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  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: mouse click in QGprahicsScene

    What about View ? if we handle the mouse press event in view rather than scene , which one is better ??

    Hope i am making sense

  3. #3
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: mouse click in QGprahicsScene

    Sure, you can do the same thing in the view, but it's better to handle events for the scene. The view's responsibility is to visualize and navigate the scene. If you handle events in the scene, you can use standard views and still get the correct mouse behavior.
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  4. #4
    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: mouse click in QGprahicsScene

    Ok fine..
    but how do I check if the Drag event originated from the scene ? I had implemented things in scene itself, but got stuck when I had to check if it originated from the scene.
    Because in Scene event, the source() is widget which u have to set at the time of Drag.


  5. #5
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: mouse click in QGprahicsScene

    Well all Graphics Scene events come with a widget, and the widget is the originator of the event. So if you want to know what widget started the drag, just pull it out from, for example, the QGraphicsSceneMouseEvent that starts the drag.

    Qt Code:
    1. void MyItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    2. {
    3. // event->widget() is the originating widget, and also the viewport
    4. // of the originating QGraphicsView. So event->widget()-parentWidget()
    5. // gives you the view.
    6. }
    To copy to clipboard, switch view to plain text mode 
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  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: mouse click in QGprahicsScene

    Actually I wanted to check if the Drag started from this scene or not..

    in
    Qt Code:
    1. MyScene::mousePressEvent(..)
    2. {
    3. // Start drag
    4. }
    5.  
    6.  
    7. MyScene::dropEvent(QGraphicsSceneDragDropEvent * event )
    8. {
    9. // How to check if this event started from this scene or some other scene..
    10. // how can I compare if(this == event->widget()) ??
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

    Also i guess if i am starting the drag in scene, I have to handle all the move events and setting of position of item in the scene.. right ??

  7. #7
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: mouse click in QGprahicsScene

    You know the view, so you can easily ask for the scene:

    Qt Code:
    1. QWidget *viewport = event->widget();
    2. QGraphicsView *view = viewport ? qobject_cast<QGraphicsView *>(viewport->parentWidget()) : 0;
    3.  
    4. QGraphicsScene *scene = view->scene();
    To copy to clipboard, switch view to plain text mode 
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  8. The following user says thank you to Bitto for this useful post:

    aamer4yu (21st December 2006)

Similar Threads

  1. mouse moving don't produce mouse events
    By coralbird in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2006, 06:13
  2. Replies: 2
    Last Post: 24th July 2006, 18:36
  3. mouse click event
    By vijay anandh in forum Qt Programming
    Replies: 1
    Last Post: 1st May 2006, 09:24
  4. QStackerWidget and mouse events
    By high_flyer in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2006, 19:25
  5. Replies: 5
    Last Post: 12th January 2006, 15:40

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.