Qt World Summit: Submit your Presentation

QStateMachine error state

  • I have state machine with three states s1, s2, s3 and s4. From states s1, s2 and s3 machine can enter error state.

    @QStateMachine machine;
    QState *s1 = new QState();
    QState *s2 = new QState();
    QState *s3 = new QState();
    QFinalState *s4 = new QFinalState();
    s1->addTransition(object, SIGNAL(done()), s2);
    s2->addTransition(object, SIGNAL(done()), s3);
    s3->addTransition(object, SIGNAL(done()), s4);

    What is the best way to do this? I was googling for that, but no luck. is it ok to do something like this?

    @QFinalState *sx = new QFinalState();
    s1->addTransition(object, SIGNAL(error()), sx );
    s2->addTransition(object, SIGNAL(error()), sx );
    s3->addTransition(object, SIGNAL(error()), sx );@

  • Hi zvjerka,

    You can also set the states using void QState::setErrorState(QAbstractState *state);
    instead of using Signal and Slot.
    QFinalState *sx = new QFinalState();
    s1->setErrorState (sx); //If you want a single error state for all states 1,2 and 3
    s2->setErrorState (sx);
    s3->setErrorState (sx);
    For more details you can refer


    and especially this part.

    "If an error is encountered, the machine will look for an error state, and if one is available, it will enter this state. The types of errors possible are described by the Error enum. After the error state is entered, the type of the error can be retrieved with error(). The execution of the state graph will not stop when the error state is entered. If no error state applies to the erroneous state, the machine will stop executing and an error message will be printed to the console.".

    [Edit: Added @ tags around code -- mlong]

  • Hi Techtotie,
    thank you for your answer.
    I found that, but if I understand it well, it is used only for "internal" errors.

    What I'm trying to figure out is: what is the best way to handle "custom errors" in machine execution time.
    we have state machine which is handling elevator door closing and it is doing next steps:

    • s1: check if doors are opened
    • s2: check if it's time for closing (idle timer occurred)
    • s3: check doors health
    • s4: close doors
    • s5: lock doors

    What I want to handle is following:
    If error occur while in state s3 (check doors health), for example door sensor reported failure while checking health status, I want to report that and stop machine execution, because it can happen that someone is standing on doors and it can hurt that person, because doors started closing with failure on door sensor which is checking if there are some objects.

Log in to reply