logo
Published on Fedora Tunisia (http://fedora-tn.org)

Your first Hello world application with QT

By kaiser
Created 2006-11-22 13:17

QT is a GUI toolkit, that will simplify creating and maintening
Graphical User Interface for your programs.
QT has been created by Norwegian's Trolltech [1] which proposes different licenses.

QT is proposed with Fedora distro, with the free license. So you can
use it for your own programs, but never for applications to be sold.

In this article, we will create a simple program that displays "Hello
World" on the main Dialog frame of the application.
So open the QT Designer application directly in Application/Programming
menu, or just type
$ designer

in a terminal.
QT Designer application will start. It ask what you want to do : C++ Project, Dialog, C++ Source ...
Note : Do not forget to save your progression, or your changes will be lost.
Step 1
Choose C++ Dialog.

QT GUI [2]

Step 2
Project Settings : Name your project in Project File : hello_world.pro.
Note : it is highly
recommanded that you put your project in its own directory so you will
not be lost when you look for your files. Choose the "three-dots
button" near to Project File and create a new directory named
hello_world, in which we will put all project files.
Step 3
Create a new Dialog : File/New or CTRL+N and choose Dialog, then save
it as hello.ui (ui : user interface). You can resize your dialog form
(Do not let it too much big).
On the right menu, change the "name" proprety into "Hello".
On this simple UI, we will have two widgets : a Push Button and a Text Label. You can find a list of the whole widgets on the left of the screen.
Step 3.1
Choose a TextLabel. Then, choose its dimensions on the Dialog. By
double-clicking on the TextLabel, you may change the text : write down
"Hello World !". You can can center, use bold, italic styles ...
Of course, you may change propreties of the Label, in the menu on the right of the screen. Choose "hello_label".
Step 3.2
Choose a PushButton, and put it in lower-right corner of the dialog. Double-click, and write down : QUIT. Name it "quit_button".
hello form qt
Now, write click on the Push Button and choose "Connections" options.
You will have a menu in which you will edit signals sent to the
application. In our case :

  1. Choose New
  2. Sender : quit_button
  3. Signal : clicked(), which is a predifined event, that means when you click on an object
  4. Receiver : Hello
  5. Slot : close(), which is a predifined function that closes the application.
  6. Validate by Clicking OK Button
Save everything.
Step 4
File/New menu 'or CTRL+N' and choose "C++ Main-File (main.cpp)". Accept
the default options, and click OK. You should have something that look
like this :

#include <qapplication.h>
#include "hello.h"
int main( int argc, char ** argv )
{
    QApplication a( argc, argv );
    Hello w;
    w.show();
    a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
    return a.exec();
}

Save everything.
Step 5
Open a terminal. Then :

$ cd /path/to/project/directory
$ qmake -o Makefile hello_world.pro
... (creating Makefile) ....
$ make
... (compiling) ...
$ ./hello_world

And that's your first program with QT. Nice, isn't it ?
Other tips, you can generate the header and source file, using "uic" command. Let's see that :

  1. in order to generate header, tape in the terminal
    $ uic hello.ui -o hello.h
  2. for sources, type :
    $ uic -impl hello.h hello.ui -o hello.c

You can now see how headers and sources look like. That's all for today.
I will make other tutorials in the future for advanced features in QT. Enjoy programming with QT.


Source URL:
http://fedora-tn.org/?q=node/126