PDA

View Full Version : QStateMachine doesn't starts :(



nudels
17th January 2012, 21:17
Hello Guys :)

i have a problem with the QStateMachine. I want to use it for seting static positions for some GraphicsObjects, but it doesn't start :(

here is my code for init the machine and states:




void BedienelementController::initMachine()
{
_machine = new QStateMachine(this);
initState();
}


void BedienelementController::initState()
{
_stateList.append(new QState());
_stateList.append(new QState());
_stateList.append(new QState());


_stateList.at(0)->addTransition(this, SIGNAL(jsRechts()), _stateList.at(1));
_stateList.at(1)->addTransition(this, SIGNAL(jsRechts()), _stateList.at(2));
_stateList.at(2)->addTransition(this, SIGNAL(jsRechts()), _stateList.at(0));

_machine->addState(_stateList.at(0));
_machine->addState(_stateList.at(1));
_machine->addState(_stateList.at(2));

_machine->setInitialState(_stateList.at(0));

_machine->start();


_stateList.at(0)->assignProperty(_textFelderH.at(0), "pos", *_punkteListe.at(0));
_stateList.at(0)->assignProperty(_textFelderH.at(1), "pos", *_punkteListe.at(1));
_stateList.at(0)->assignProperty(_textFelderH.at(2), "pos", *_punkteListe.at(2));

_stateList.at(1)->assignProperty(_textFelderH.at(0), "pos", *_punkteListe.at(1));
_stateList.at(1)->assignProperty(_textFelderH.at(1), "pos", *_punkteListe.at(2));
_stateList.at(1)->assignProperty(_textFelderH.at(2), "pos", *_punkteListe.at(0));

_stateList.at(2)->assignProperty(_textFelderH.at(0), "pos", *_punkteListe.at(2));
_stateList.at(2)->assignProperty(_textFelderH.at(1), "pos", *_punkteListe.at(0));
_stateList.at(2)->assignProperty(_textFelderH.at(2), "pos", *_punkteListe.at(1));
qDebug() << _machine->isRunning();

}



The Debug output allways says "false".

_stateList is a QList<QState*>, _punkteListe a QList<QPointF*> and _textFelderH ar my class inherited from QGraphicsObject
The "BedienelementController"-Class is inherited from QWidget.

i hope smbdy can give me a hint ;)


EDIT:
okay... i have to read the online documentation... the statemachine can only be started when a event-loop is running...
sry :(

Thanks!

with best regards

nudels

d_stranz
24th January 2012, 01:16
An easy trick to fix this problem: Set up your state machine in the initState() method as you have done. At the end of that method, insert a single-shot QTimer, like this:



void BedienelementController::initState()
{
// ...
QTimer::singleShot( 0, _machine, SLOT( start() ) );
}


As soon as the event loop is started, the timer signal will be emitted and the state machine will start.