Archive for the Qt Category

Qt/Kde coding style and SVN tree

Posted in Qt | No Comments »

Hi all,

today I will post  a script based on Qt/Kde coding style using astyle command in a woking svn tree to fix coding style.

This script works like the scrip from the last post but now to work in a SVN tree.

It will check the coding style just in the modified and added files to the svn commit.

#!/bin/bash

astyle --indent=spaces=4 --brackets=linux \
      --indent-labels --pad=oper --unpad=paren \
      --one-line=keep-statements --convert-tabs \
      --indent-preprocessor \
      $(svn status | awk '(/^M/ || /^A/) &&
            (/\.h\>/ || /\.c\>/ || /\.cpp\>/){print $2}')

You can get the source code from here.

References:

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

http://techbase.kde.org/Policies/Kdelibs_Coding_Style

Qt/Kde coding style and Git tree

Posted in Qt | 1 Comment »

Hi all,

today I will post  a script to check and fix Qt/Kde coding style using astyle command in a woking git tree.

#!/bin/bash

astyle --indent=spaces=4 --brackets=linux
      --indent-labels --pad=oper --unpad=paren
      --one-line=keep-statements --convert-tabs
      --indent-preprocessor
      $(git-status | awk -F : '(/modified:/ ||
	/new file:/) && (/.h>/ || /.c>/ ||
	/.cpp>/) {print $2}')

Download the source code here.

So you just need run the script in a working git tree to check and fix Qt coding style.

It will check and fix Qt coding style in all files modified in the git tree.

To force the coding style fix before each commit you can put the script as git’s hook script, putting it in the .git/hooks/pre-commit file of your project.

To enable the pre-commit hook script just run:

$ chmod +x .git/hooks/pre-commit

References:

http://astyle.sourceforge.net/

http://techbase.kde.org/Policies/Kdelibs_Coding_Style

http://git-scm.com/

A Simple Qt State Machine

Posted in Qt | No Comments »

Hi,

today I will post the source code for the following satechart for a simple Qt state machine.

simple-state-machine.jpg

You can get the source code here.

    QPushButton button("Ok");

    /* The state machine. */
    QStateMachine machine;

    /* Three states to the state machine. */
    QState s1;
    QState s2;
    QState s3;

    /* For each state it sets the button's text property. */
    s1.setPropertyOnEntry(&button, "text", "In state S[1]");
    s2.setPropertyOnEntry(&button, "text", "In state S[2]");
    s3.setPropertyOnEntry(&button, "text", "In state S[3]");

    /* Adds the transitions to each state */
    s1.addTransition(&button, SIGNAL(clicked()), &s2);
    s2.addTransition(&button, SIGNAL(clicked()), &s3);
    s3.addTransition(&button, SIGNAL(clicked()), &s1);

    /* Adds the three states s1, s2 and s3 to the state machine. */
    machine.addState(&s1);
    machine.addState(&s2);
    machine.addState(&s3);

    /* Sets the state s1 as initial state. */
    machine.setInitialState(&s1);

    /* Starts the state machine. */
    machine.start();

    button.show();

When any of the states is entered, the button’s text will be changed accordingly.

Reference:

http://doc.trolltech.com/solutions/4/qtanimationframework/statemachine-api.html

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