Important: Please read the Qt Code of Conduct -

How to use QMouseEventTransition with QGraphicsScene?

  • Hi,

    right now, I have the following problem with the QStateMachine framework:
    I want to connect two states using QMouseEventTransition with the QGraphicsSceneMouseEvent of QGraphicsScene. 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);
       // Build state machine
       auto* machine = new QStateMachine(this);

    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:

    1. Overriden QGraphicsSceneEvents mouse event functions are called correctly when clicking inside the scene (e.g. QGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent* e))
    2. Idle state of the state machine is entered correctly

    Am I doing something fundamentaly wrong here?

    Qt version used is 4.7.2

    I want to add, that I use the QStateMachine framework in my application already. I have no problems with it at all, only the QMouseEventTransition seems not to work with QGraphicsScene. As there are no examples in the documentation for the usage of this transition, I'm not sure, how it is used correctly.

  • Lifetime Qt Champion


    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

Log in to reply