QGraphicsScene mousePressEvent event passing problem
-
Problem description, AItem and BItem both get the mousePressed event, and when I click on the element in A Item, the mousePresed event of B Item still gets activated.
The effect I want to achieve is that when I left click on Item A, then it goes to mousePressEvent of Item A and Item B ignores it, if I click on Item B then it goes to mousePressEvent of B and Item A ignores it, how to achieve this?
class AItem : public QObject, public QGraphicsItem { //.... protected: void mousePressEvent(QGraphicsSceneMouseEvent *event) override; void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override; } void AItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { if (event->button() == Qt::LeftButton) { qDebug() << "A Item Pressed!"; } QGraphicsScene::mousePressEvent(event); }
and:
class BItem : public QObject, public QGraphicsItem { //.... protected: void mousePressEvent(QGraphicsSceneMouseEvent *event) override; void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override; } void BItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { if (event->button() == Qt::LeftButton) { qDebug() << "B Item Pressed!"; } QGraphicsScene::mousePressEvent(event); }
and:
class MainScene : public QGraphicsScene { protected: void mousePressEvent(QGraphicsSceneMouseEvent *event) override; void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override; } void MainScene::mousePressEvent(QGraphicsSceneMouseEvent *event) { if (event->button() == Qt::LeftButton) { QTransform transform; if (itemAt(event->scenePos(), transform) == NULL) { qDebug() << "Scene Emtpy Pos Pressed..."; } else { AItem *aItem_ = dynamic_cast<AItem *>(itemAt(event->scenePos(), transform)); BItem *bItem_ = dynamic_cast<BItem *>(itemAt(event->scenePos(), transform)); //Problem description, AItem and BItem both get the mousePressed event, and when I click on the element in A Item, the mousePresed event of B Item still gets activated. //The effect I want to achieve is that when I left click on Item A, then it goes to mousePressEvent of Item A and Item B ignores it, if I click on Item B then it goes to mousePressEvent of B and Item A ignores it, how to achieve this? } } QGraphicsScene::mousePressEvent(event); }
-
@alvazz said in QGraphicsScene mousePressEvent event passing problem:
class AItem : public QObject, public QGraphicsItem
Instead of inheriting these two, you could use
QGraphicsObject
which is basically aQGraphicsItem
that is also aQObject
to be able to use signals and slots.Cant reproduce your described behavior... How are your items positioned in your scene? On top of each other? Overlapping? Or completely independent? And where exactly do you click?
-
tem A and Item B are independent of each other and have no overlapping relationship, they are added to the scene in turn. Located on the same layer.
The click event occurs on Item A or Item B, or on a blank area of the Scene besides.
-
@alvazz said in QGraphicsScene mousePressEvent event passing problem:
void AItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
qDebug() << "A Item Pressed!";
}
QGraphicsScene::mousePressEvent(event);
}Is this your actual code?
Used my own test case... You cant call the protectedQGraphicsScene::mousePressEvent
from aQGraphicsItem
. It should beQGraphicsItem::mousePressEvent
Besides that, I still cant reproduce... If I click item A, I get "A Item Pressed!" and B, when B is clicked, never both.
Show your full code please or if possible make a short GIF/video that illustrates what you are doing