PDA

View Full Version : QGraphicsItem right click event problem



liqxpil
13th December 2010, 11:36
hi all

i am working on a subclass of the QGraphicsItem class, i am trying to reimplemented the MouseReleaseEvent to do actions based on the pressed button but i cant get the right button to work, what i did was this:


void MyClass::mouseReleaseEvent(QGraphicsSceneMouseEven t *event)
{
if(event->button() == Qt::LeftButton)
{
//do stuff
}

else if(event->button() == Qt::RightButton)
{
//do other stuff
}
}

is this the right way to do it?

high_flyer
13th December 2010, 11:53
did you implement mousePressEvent () for this item?

liqxpil
13th December 2010, 12:10
Added after 8 minutes:

it works after i changed the event to mousePressEvent instead
i get it the release event doesnt accept the right button? anyway works for me.. thanks high_flyer

high_flyer
13th December 2010, 13:54
no.
Its just that mousePressEvent() decied which object receives mouse events:
from the docs:


This event handler, for event event, can be reimplemented to receive mouse press events for this item. Mouse press events are only delivered to items that accept the mouse button that is pressed. By default, an item accepts all mouse buttons, but you can change this by calling setAcceptedMouseButtons().

The mouse press event decides which item should become the mouse grabber (see QGraphicsScene::mouseGrabberItem()). If you do not reimplement this function, the press event will propagate to any topmost item beneath this item, and no other mouse events will be delivered to this item.

If you do reimplement this function, event will by default be accepted (see QEvent::accept()), and this item is then the mouse grabber. This allows the item to receive future move, release and doubleclick events. If you call QEvent::ignore() on event, this item will lose the mouse grab, and event will propagate to any topmost item beneath. No further mouse events will be delivered to this item unless a new mouse press event is received.