Results 1 to 5 of 5

Thread: mouse events between qwidget, scene and items

  1. #1
    Join Date
    Mar 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default mouse events between qwidget, scene and items

    Hi!

    I'm using the code from example "elastic nodes " (http://doc.trolltech.com/4.3/graphic...sticnodes.html) and I'm add some things to this example like right buttons mouse events.

    I would like to create this scenario:
    -> right click in a node: open a context menu
    -> right click in other point of scene that it isn't a node: create a node

    In the graphicwidget I created the GraphWidget::mousePressEvent(QMouseEvent *event)
    and i want to know in this function if there are some node in the position of the event.

    The problem is that the position is in view coordinates and i want in scene coordinates... to use in method itemAt(http://doc.qt.nokia.com/4.6/qgraphicsscene.html#itemAt)

    Other problem is that i use the method items() (http://doc.qt.nokia.com/4.6/qgraphicsscene.html#items) but i get <unavailable synchoneous data> why?

    Thanks for any help...
    João

  2. #2
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: mouse events between qwidget, scene and items

    Hi,

    You need to work with void myScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent). mouseEvent will be in scene coordinates and itemAt(mouseEvent->scenePos()) will return an item in that position if any.

    To handle the right click in the node you can use void contextMenuEvent(QGraphicsSceneContextMenuEvent *event). For example:
    Qt Code:
    1. void myitem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
    2. {
    3. event->accept(); //Accept the event.
    4. QMenu menu; //Create a meny
    5. menu.addAction(myaction); //Add an action
    6. menu.exec(event->screenPos()); //Shows the menu in the screen position.
    7. }
    To copy to clipboard, switch view to plain text mode 

    As for the unavailable synchoneous data I have no idea. In which procedure of your code you call scene->items() ?

    Carlos.

  3. #3
    Join Date
    Mar 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: mouse events between qwidget, scene and items

    Hi!

    Thanks for the hint! But the problem was another... i was using scene->pos() instead of only pos() ...

    But know i have another problem.. the contextmenu and mousepress events when ocorred at the same time...

    I have a item that is movable. When i click with right button.. the context menu appears, but when i close the menu the item continues selected.. and make errors like if i click in another point of view the item moves...

    So .... how can I unselect the item after close the menu?

    Thanks,
    João

  4. #4
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: mouse events between qwidget, scene and items

    Try by adding item.setSelected(false); before executing the menu.

  5. #5
    Join Date
    Mar 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: mouse events between qwidget, scene and items

    No... the problem continues...

    When menu closes.. if I click in other place and drag, the item moves unfortunately


    Added after 21 minutes:


    ok... i resolve.. with "set enable(false)" before exec of menu and "set enable (true) " after...

    but this is the correct way?
    Last edited by teorias; 8th March 2011 at 15:16.

Similar Threads

  1. Replies: 7
    Last Post: 26th June 2009, 20:33
  2. add items into scene
    By Noxxik in forum Qt Programming
    Replies: 0
    Last Post: 9th March 2009, 17:32
  3. How to intercept mouse events in a scene
    By markmuetz in forum Qt Programming
    Replies: 2
    Last Post: 22nd May 2008, 15:36
  4. Can't move Items in the scene
    By maverick_pol in forum Qt Programming
    Replies: 2
    Last Post: 16th May 2008, 10:40
  5. mouse moving don't produce mouse events
    By coralbird in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2006, 07:13

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.