Reputation: 456
I have a class that inherits from QGraphicItems
and another one that inherits from QGraphicsScene
. I created a custom item and place them in my custom scene.
Here are my event handler functions:
void Panel::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if (mouseEvent->button() != Qt::LeftButton)
return;
//my code
QGraphicsScene::mousePressEvent(mouseEvent);
}
void Panel::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
//my code
QGraphicsScene::mouseMoveEvent(mouseEvent);
}
void Panel::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
//my code
QGraphicsScene::mouseReleaseEvent(mouseEvent);
}
However, those events do not seem to get called. I based my code on the DiagramScene
example in Qt and the major difference is that my item inherits from QGraphicsItem
rather than QGraphicsPolygonItem
.
How can I get to handle the events properly?
Upvotes: 0
Views: 644
Reputation: 4954
In your implementation of Panel::mousePressEvent()
, you end up calling QGraphicsScene::mousePressEvent()
after your own code has handled the event. The default implementation of QGraphicsScene::mousePressEvent()
will probably undo things you have done in your own code. As an example, the default implementation of QGraphicsScene::mousePressEvent()
will forward the event to the topmost item that accepts mouse events, and that item will become the mouse grabber. If your code expect some other things to happen such that another object should be the grabber (can't tell from the limited code you posted), then the call to the base class will undo what you did.
You shouldn't be calling the base class' event handlers if you wish to handle events yourself in a custom way.
Upvotes: 1