Results 1 to 12 of 12

Thread: mouse click in QGprahicsScene

  1. #1
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default mouse click in QGprahicsScene

    I'm a little puzzled by how the mouse works in the QGraphicsScene.
    If I first click on an item in the view, and then click on a empty space where the item should be placed, how do Iwrite this in code?

    It seems right now, that if no item is selected, nothing happens, the event is ignored. Is this true?
    I'm a little confused with what to write in the

    mousePressEvent(QGraphicsSceneMouseEvent*) function. for the class that is derived from the QGraphicsScene.

  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

    you cuould use QGraphicsScene::items() functions to get the list of items when mouse is pressed...
    then u cud place the first item on next mouse click by using the setPos() function

    hope this helps

  3. #3
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default mouse click in QGprahicsScene (updated question)

    Here is an update of the question about clicking in the view and getting stuff to happen.

    If I have

    class Mapublic QGraphicsScene {...};
    class Town: public QGraphicsPixmapItem {...};
    class Army: public QGraphicsPixmapItem {...};

    and the GraphicsView contains a large Map object (a huge pixmap) that fills the entire view. Then there are Townand Army objects on the map.
    Now I want to be able to click on the Map obj and have code that does one thing if a Army is clicked, or a Town is clicked, and does something else if only the Map object is clicked.

    Perhaps the Town should only react to the the Leftbutton.

    How should I implement this with the mousePressedEvent ? I have added mousePressEvent to the classes, but I'm not sure if it is done correctly. It seems that it reacts differently. Sometimes it reacts as if there were no Army, evern if I click on an army.
    Any good advice from the Qt Gurus that knows how this should be done, would be very nice.

  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

    Well...
    I too have done something similar though my problem was little different. U can refer it to thread http://www.qtcentre.org/forum/f-qt-p...get--4505.html

    I eventually implemented cut-paste from one scene to another. The following may help you.

    Install a filter on the view or the main window. Psuedo code for the mousPressEvent is ..

    Qt Code:
    1. mousPressEvent()
    2. {
    3. QGraphicsItem *item = itemAt(event->pos()); // make sure pos is in proper scene cordinates
    4. cast_pointer of item;
    5. check pointer type
    6. if (pointer == Town)
    7. { // code for town
    8. }
    9. else if(pointer == Army)
    10. { //code for army
    11. }
    12. else if(pointer==map)
    13. {
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 8th December 2006 at 11:31. Reason: missing [code] tags

  5. #5
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mouse click in QGprahicsScene

    Would it be possible to reimplement the mousePressEvent() for the GraphicsScene instead of the GraphicsView?

    The most important thing is though that you only suggest mousePressEvent for one class. Not more than one as I have it right now. I'll try that. Thanks.

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

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

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

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


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

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

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

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