PDA

View Full Version : mouse events between qwidget, scene and items



teorias
7th March 2011, 02:48
Hi!

I'm using the code from example "elastic nodes " (http://doc.trolltech.com/4.3/graphicsview-elasticnodes.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

qlands
7th March 2011, 08:01
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:


void myitem::contextMenuEvent(QGraphicsSceneContextMenu Event *event)
{
event->accept(); //Accept the event.
QMenu menu; //Create a meny
menu.addAction(myaction); //Add an action
menu.exec(event->screenPos()); //Shows the menu in the screen position.
}


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

Carlos.

teorias
7th March 2011, 21:26
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

qlands
8th March 2011, 06:48
Try by adding item.setSelected(false); before executing the menu.

teorias
8th March 2011, 14:16
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?