IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

Two-way Button Example

The Two-way button example shows how to use Qt State Machine Framework to implement a simple state machine that toggles the current state when a button is clicked.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

Two-way Button Example

Image non disponible
 
Sélectionnez
int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    QPushButton button;
    QStateMachine machine;

The application's main() function begins by constructing the application object, a button and a state machine.

 
Sélectionnez
    QState *off = new QState();
    off->assignProperty(&button, "text", "Off");
    off->setObjectName("off");

    QState *on = new QState();
    on->setObjectName("on");
    on->assignProperty(&button, "text", "On");
Image non disponible

The state machine has two states; on and off. When either state is entered, the text of the button will be set accordingly.

 
Sélectionnez
    off->addTransition(&button, &QAbstractButton::clicked, on);
    on->addTransition(&button, &QAbstractButton::clicked, off);

When the state machine is in the off state and the button is clicked, it will transition to the on state; when the state machine is in the on state and the button is clicked, it will transition to the off state.

 
Sélectionnez
    machine.addState(off);
    machine.addState(on);

The states are added to the state machine; they become top-level (sibling) states.

 
Sélectionnez
    machine.setInitialState(off);
    machine.start();

The initial state is off; this is the state the state machine will immediately transition to once the state machine is started.

 
Sélectionnez
    button.resize(100, 50);
    button.show();
    return app.exec();
}

Finally, the button is resized and made visible, and the application event loop is entered.

Example project

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+