[Qt-interest] Setting State in QStateMachine

NoRulez norulez at me.com
Tue Jun 21 22:20:08 CEST 2011


Hi,

i've something like the following:

MyClass::MyClass(QWidget *parent) : QWidget(parent) {
    QState *pStateStarted = new QState;
    QState *pStateStartedPending = new QState;
    QState *pStateStoppedPending = new QState;
    QState *pStateStopped = new QState;

    pStateStopped->addTransition(pButton1, SIGNAL(triggered()),
pStateStartedPending);
    pStateStarted->addTransition(pButton2, SIGNAL(triggered()),
pStateStoppedPending);

    pStateStartedPending->addTransition(this, SIGNAL(onStarting()),
pStateStarted);
    pStateStoppedPending->addTransition(this, SIGNAL(onStopping()),
pStateStopped);

    connect(pStateStartedPending, SIGNAL(entered()), this,
SLOT(onStartPending()));
    connect(pStateStoppedPending, SIGNAL(entered()), this,
SLOT(onStopPending()));

    QStateMachine *pStateMachine = new QStateMachine(this);
    pStateMachine->addState(pStateStarted);
    pStateMachine->addState(pStateStartedPending);
    pStateMachine->addState(pStateStoppedPending);
    pStateMachine->addState(pStateStopped);
    pStateMachine->setInitialState(pStateStopped);
    pStateMachine->start();
}

void MyClass::onStartPending() {
    // do Something
    emit onStarted();
}

void MyClass::onStopPending() {
    // do Something
    emit onStopped();
}

void MyClass::somethingElse() {
    // Switch to state "pStateStartedPending"
}


Best Regards

-----Ursprüngliche Nachricht-----
Von: qt-interest-bounces+norulez=me.com at qt.nokia.com
[mailto:qt-interest-bounces+norulez=me.com at qt.nokia.com] Im Auftrag von Sean
Harmer
Gesendet: Dienstag, 21. Juni 2011 21:32
An: qt-interest at qt.nokia.com
Betreff: Re: [Qt-interest] Setting State in QStateMachine

Hi,

On 21/06/2011 19:44, NoRulez wrote:
> I've a simple statemachine with 4 states. After clicking a button the
states will change.
>
> Now the problem I have is if I get a signal I want to change the state to
e.g. 2
>
> I use addTransition for the entered() signal, but the state isn't entered.
>
> Could anybody give me a hint on how to switch states?

Can you post some code that shows the problem please?

How do you add the transition such that it responds to the clicked() 
signal of your button? Is your state machine running? Have you correctly 
set an initial state? Is the state machine in the correct state (the 
source state of the transition) when the signal is emitted?

Cheers,

Sean
_______________________________________________
Qt-interest mailing list
Qt-interest at qt.nokia.com
http://lists.qt.nokia.com/mailman/listinfo/qt-interest




More information about the Qt-interest-old mailing list