State Machine

April 23, 2009 at 12:37 am Leave a comment

Hi,

I am posting an example that demonstrates the use of QStateMachine.

A state machine with three states, state1, state2 and state3. The state machine is controlled by three QPushButton; when the button is clicked, the machine transitions to another state. Initially, the state machine is in state state1.

main.cpp


  1 #include <QtGui>
  2 #include <QApplication>
  3 #include <QVBoxLayout>
  4 #include <QPushButton>
  5 #include <QStateMachine>
  6
  7
  8
  9
 10 int main ( int argc, char **argv)
 11 {
 12     QApplication app(argc, argv );
 13
 14     /* Set the name application */
 15
 16     app.setApplicationName(“EX_State”);
 17
 18     /* Here created a window */
 19
 20     QWidget *window = new QWidget;
 21
 22     /* Here created the buttons */
 23
 24     QPushButton *button1 = new QPushButton (“Estate 1”);
 25     QPushButton *button2 = new QPushButton (“Estate 2”);
 26     QPushButton *button3 = new QPushButton (“Estate 3”);
 27
 28     /* Here created the Layout */
 29
 30     QVBoxLayout  *vbox = new QVBoxLayout;
 31
 32     /* Here the buttons are added  in the Layout */
 33
 34     vbox->addWidget(button1);
 35     vbox->addWidget(button2);
 36     vbox->addWidget(button3);
 37
 38
 39
 40     window->setLayout(vbox);
 41     window->resize(480,600);
 42
 43
 44
 45     /* The state machine is created */
 46
 47     QStateMachine machine;
 48
 49     /* State are created  */
 50
 51     QState *state1 = new QState();
 52     QState *state2 = new QState();
 53     QState *state3 = new QState();
 54
 55
 56
 57     state1->setPropertyOnEntry(button1,”geometry”,QRectF(0,0,100,100));
 58     state1->setPropertyOnEntry(button2,”geometry”,QRectF(100,100,200,200));
 59     state1->setPropertyOnEntry(button3,”geometry”,QRectF(300,300,150,150));
 60
 61     state2->setPropertyOnEntry(button1,”geometry”, QRectF(380,1,100,210));
 62     state2->setPropertyOnEntry(button2,”geometry”, QRectF(1,99,100,310));
 63     state2->setPropertyOnEntry(button3,”geometry”, QRectF(200,200,100,410));
 64
 65     state3->setPropertyOnEntry(button1,”geometry”, QRectF(10,10,470,20));
 66     state3->setPropertyOnEntry(button2,”geometry”, QRectF(10,300,470,20));
 67     state3->setPropertyOnEntry(button3,”geometry”, QRectF(10,570,470,20));
 68
 69
 70     state1->addTransition(button1, SIGNAL (clicked()), state1);
 71     state1->addTransition(button2, SIGNAL(clicked()),state2);
 72     state1->addTransition(button3, SIGNAL(clicked()),state3);
 73
 74     state2->addTransition(button1, SIGNAL (clicked()), state1);
 75     state2->addTransition(button2, SIGNAL(clicked()),state2);
 76     state2->addTransition(button3, SIGNAL(clicked()),state3);
 77
 78     state3->addTransition(button1, SIGNAL (clicked()), state1);
 79     state3->addTransition(button2, SIGNAL(clicked()),state2);
 80     state3->addTransition(button3, SIGNAL(clicked()),state3);
 81
 82     /* States are added in state machine */
 83
 84     machine.addState(state1);
 85     machine.addState(state2);
 86     machine.addState(state3);
 87
 88     /*  Set the state initial for the state machine */
 89
 90     machine.setInitialState(state1);
 91
 92     /* Start the state machine */
 93     machine.start();
 94
 95     window->show();
 96
 97     return app.exec();
 98
 99
100 }


Blogged with the Flock Browser
Advertisements

Entry filed under: Qt. Tags: .

The Simple Test Hudson – Tool to build/test your project

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

April 2009
M T W T F S S
    Jul »
 12345
6789101112
13141516171819
20212223242526
27282930  

Most Recent Posts


%d bloggers like this: