Posts Tagged Qt

Hellow world using QStateMachine from Qt state machine framework

Posted in Qt | 1 Comment »

Hi,

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();
}

=

state01.png

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

http://ragner.org/programming-languages/qt/142

Qt hello world and qmake

Posted in Qt | 1 Comment »

Hi all,

I am studying Qt and I want to do some posts about.

So I will start with a simple hello world and talking how to compile its source code.

= main.cpp =

#include <QtGui/QApplication>
#include <QtGui/QLabel>

int main(int argc, char *argv[])
{

    /* All Qt application need implements a QApplication */
    QApplication app(argc, argv); 

    /* Here QLabel is the main window! */
    QLabel label("Hello, world!");

    /* Shows the QLabel */
    label.show();

    /* Enters the main event loop and waits until exit() is called. */
    return app.exec();
}

=

Now you need to create the .pro file with the project’s configurations and dependencies.

To create the .pro file you can just run:

$  qmake -project

The command above will create a .pro file with the same name of the directory where is the source code.

To create the Makefile run:

$ qmake

So to complie the source code run:

$ make

You will see a binary file with the same name of the .pro file.

References:

http://doc.trolltech.com/4.5/classes.html

http://doc.trolltech.com/4.5/widgets-tutorial.html

http://doc.trolltech.com/4.5/qmake-manual.html