Hellow world using QStateMachine from Qt state machine framework
Posted on quarta-feira, abril 15th, 2009 at 19:40Hi,
continuing the posts about Qt, today I will write how to implement a Qt application using QStateMachine from the new Qt animation framework.
The code below demonstrate the core functionality of the State Machine API.
= main.cpp =
/* Includes */
#include <QtGui/QApplication>
#include <QtGui/QPushButton>
#include <QtCore/QStateMachine>
#include <QtCore/QFinalState>
#include <QtCore/QState>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QPushButton button("Exit");
/* State machine that will finish when a button is clicked. */
QStateMachine machine;
/* The initial state. */
QState *s1 = new QState();
/* The final state. */
QFinalState *s2 = new QFinalState();
/* Instructs state s1 to set the property "text" of the button to the given
text "Click me" when the state is entered. */
s1->setPropertyOnEntry(&button, "text", "Click me");
/* Instructs state s1 to execute a transition to state s2 when the button
will be clicked. */
s1->addTransition(&button, SIGNAL(clicked()), s2);
/* Adds the states s1 and s2 to the state machine. */
machine.addState(s1);
machine.addState(s2);
/* Connects the signal finished() from the state machine to the quit() slot
from the QApplication. So the application is going to quit when the state
machine will be finished. */
QObject::connect(&machine, SIGNAL(finished()), QApplication::instance(),
SLOT(quit()));
/* Sets the state machine's initial state; this state is entered when the
state machine is started. */
machine.setInitialState(s1);
/* Starts the sate machine. */
machine.start();
button.show();
return app.exec();
}
=

The code above implements a state machine with two states s1 as initial state and s2 as final state.
When the state machine is started it starts into state s1 that sets the button’s text property to “Click me”.
The transition from s1 to s2 is controlled by the single QPushButton. When the button is clicked the machine executes a transition from state s1 to state s2.
When the state machine enter into the state s2, the final state, it emits the finished() signal that is connected to the quit() slot from QApplication, so it calls the quit() function and the application is finished.
References:
http://www.qtsoftware.com/products/appdev/add-on-products/catalog/4/Utilities/qtanimationframework/
http://doc.trolltech.com/solutions/4/qtanimationframework/statemachine-api.html

Your blog’s content is great, but this template is kind of GAY!
Awesome blog!