How to use QMouseEventTransition with QGraphicsScene?
-
Hi,
right now, I have the following problem with the
QStateMachine
framework:
I want to connect two states usingQMouseEventTransition
with theQGraphicsSceneMouseEvent
ofQGraphicsScene
. Clicking with the left mouse button into the scene should trigger the next state. The code for the state machine looks like this:// This function is inside of my Scene class (inherited from `QGraphicsScene`) void buildStateMachine() { auto* idle = new QState auto* processState = new QState; connect(processState, SIGNAL(entered()), this, SLOT(onProcessState())); // MouseClickEvents auto* leftClickTransitionPress = new QMouseEventTransition( this, QEvent::GraphicsSceneMousePress, Qt::LeftButton, idle); leftClickTransitionPress->setTargetState(processState); idle->addTransition(leftClickTransitionPress); // Build state machine auto* machine = new QStateMachine(this); machine->addState(idle); machine->addState(processState); machine->setInitialState(idle); machine->start(); }
The problem now is, that the state
processState
won't be entered when clicking with the left mouse button into the scene.I verified that:
- Overriden
QGraphicsSceneEvents
mouse event functions are called correctly when clicking inside the scene (e.g.QGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent* e)
) - Idle state of the state machine is entered correctly
Am I doing something fundamentaly wrong here?
Qt version used is 4.7.2
Edit:
I want to add, that I use theQStateMachine
framework in my application already. I have no problems with it at all, only theQMouseEventTransition
seems not to work withQGraphicsScene
. As there are no examples in the documentation for the usage of this transition, I'm not sure, how it is used correctly. - Overriden
-
Hi,
QGraphicsSceneMouseEvent is not a QMouseEvent but a QGraphicsSceneEvent thus the behaviour you have currently.
One alternative could be to subclass QGraphicsScene and emit a custom signal from the
QGraphicsScene::mousePressEvent
and use a QSignalTransition. If you don't want to subclass, then you can use an event filter and again emit a custom signal.Hope it helps