The Qt documentation for State machines shows two principles I'm using: Restoring Properties and Targetless transitions. The first allows to assign properties to a QState, the second to trigger events only in a specific state.
When I combine both functions, restoring properties is not working anymore. When a QSignalTransition
is fired, the properties are set to their initial values (before the state machine was started), even if the current state is not left.
I have a short example to reproduce this:
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QStateMachine>
#include <QSignalTransition>
int main(int argc, char *argv[]) {
QApplication a(argc,argv);
QWidget *w = new QWidget;
QStateMachine *machine = new QStateMachine(w);
QState *s1 = new QState;
QState *s2 = new QState;
QVBoxLayout *layout = new QVBoxLayout(w);
QPushButton *btnState = new QPushButton("Not set");
QPushButton *btnTrigger = new QPushButton("Trigger");
layout->addWidget(btnState);
layout->addWidget(btnTrigger);
machine->setGlobalRestorePolicy(QStateMachine::RestoreProperties);
s1->assignProperty(btnState,"text","S1");
s2->assignProperty(btnState,"text","S2");
s1->addTransition(btnState,SIGNAL(clicked()),s2);
s2->addTransition(btnState,SIGNAL(clicked()),s1);
QSignalTransition *sig = new QSignalTransition(btnTrigger,SIGNAL(clicked()));
s1->addTransition(sig);
machine->addState(s1);
machine->addState(s2);
machine->setInitialState(s1);
machine ->start();
w->show();
return a.exec();
}
It has two buttons: The state button toggles between S1 and S2, the trigger button triggers a QSignalTransition when S1 is active. Clicking trigger when S1 is active, the button text will be set back to "Not set"
. If I understood the concept of setGlobalRestorePolicy()
correctly, the property should stay "S1"
, as it is the value of the state S1, and the state was not left and is still active.
Is there something I missed or misunderstood, or did I something wrong?
I'm using Qt 4.8.4 on windows, if this makes any difference.