PDA

View Full Version : Problem regarding item selection/GraphicsItems/GraphicsScene and event propagation



fres
16th October 2007, 22:34
I have a scene with some items, when I click in the view/scene I want to perform some operations, like draw with the mouse etc., but when I click on an item I want to do stuff with that item. Problem is when I click near the edges of an item. The event is handled by both the item and my custom event handler.

In my subclassed scene code i have(pseudo)


mousePressed(event)
{
// send events to items
QGraphicsScene::mousePressed(event);
if(event->isAccepted())
{
return;
}
// send event to active state
emit(myMousePressed(event));
}


I'm fairly new to qt so maybe I've designed the custom event handler in a bad way.

The way it's done is by a somewhat modified state pattern. I have some state classes that connects to the custom event signals. The active state object is determined by a setState in the graphicsscene subclass. When state A is active, A's code for e.g left mouse press is run. By doing it this way a massive switch/case is avoided.

But again, it's the events which reach both items and my event handler that causes the trouble :(

Any ideas or comments?

marcel
16th October 2007, 22:41
The simplest way that comes to mind is:


mousePressEvent(event)
{
if( itemAt( event->pos()) )
{
//you clicked on an item
}
else
{
//no item there
}
}


Using accepted() is a bit ambiguous. The item can reject the event, for example.