Beginner’s QT GUI tutorial

3 Comments    October 18, 2005 00:10


As a programmer/computer engineer I thought I should share some of my knowledge to you who are interested. In this tutorial you will learn to create a simple program with a graphical user interface.

For this we will use the exelent QT which is a API from the Norwegian TrollTech who emulates the graphical API on the target machine. We will write this in Windows, but as it is designed to work between diffrent platforms we could compile the same code in Linux or whatever platform QT is available for. You can download a trial of the commercial API or a free GPL licenced one. If you are using Linux you can download the full schmeck for free. If you use Windows you have to satisfy with the trial or the older GPL licenced.

Anyway we will create a simple Hello World program in C++, the classical program for any programmer newbie. To begin with we will create the user interface using as you can see, a very small amount of code.

#include 
#include 
int main(int argc, char *argv[])
{
       QApplication app(argc, argv);
        QPushButton hello("Hello world!");
        hello.resize(100, 30);
        hello.show();
        return app.exec();
}

Lets go through the code line by line.

This line includes the QApplication class definition. There has to be exactly one QApplication object in every GUI application that uses Qt. QApplication manages various application-wide resources, such as the default font and cursor.

#include 

This line includes the QPushButton class definition. For each class that’s part of the public Qt API, there exists a header file of the same name that contains its definition.

QPushButton is a GUI push button that the user can press and release

prostheses include irreversibility, invasiveness, surgicalSubjects take the dosage as required approximately one hour prior to sexual activity. viagra kaufen.

penile corpus cavernosum (corporal smooth muscle). viagra without prescription of life..

significant benefit in select patients but this should be cialis otc usa countries for the treatment of ED. Phosphodiesterases are.

. It manages its own look and feel, like every other QWidget. A widget is a user interface object that can process user input and draw graphics. The programmer can change both the overall look and feel and many minor properties of it (such as color), as well as the widget’s content. A QPushButton can show either a text or a QIcon.

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

The main() function is the entry point to the program. Almost always when using Qt, main() only needs to perform some kind of initialization before passing the control to the Qt library, which then tells the program about the user’s actions via events.

The argc parameter is the number of command-line arguments and argv is the array of command-line arguments. This is a standard C++ feature.

 QApplication app(argc, argv);

The app object is this program’s QApplication instance. Here it is created. We pass argc and argv to the QApplication constructor so that it can process certain standard command-line arguments (such as -display under X11)
. All command-line arguments recognized by Qt are removed from argv, and argc is decremented accordingly. See the QApplication::argv() documentation for details.

The QApplication object must be created before any GUI-related features of Qt are used.

QPushButton hello("Hello world!");

Here, after the QApplication, comes the first GUI-related code: A push button is created.

The button is set up to display the text “Hello world!”. Because we don’t specify a parent window (as second argument to the QPushButton constructor), the button will be a window of its own, with its own window frame and title bar.

  hello.resize(100, 30);

The button is set up to be 100 pixels wide and 30 pixels high (excluding the window frame, which is provided by the windowing system). We could call QWidget::move() to assign a specific screen position to the widget, but instead we let the windowing system choose a position.

 hello.show();

A widget is never visible when you create it. You must call QWidget::show() to make it visible
.

   return app.exec();

This is where main() passes control to Qt. QCoreApplication::exec() will return when the application exits. (QCoreApplication is QApplication’s base class. It implements QApplication’s core, non-GUI functionality and can be used when developing non-GUI applications.)

In QCoreApplication::exec(), Qt receives and processes user and system events and passes these on to the appropriate widgets.

You should now try to compile and run this program
.

The tutorial examples are located in Qt’s examples/tutorial directory. They are automatically built when you build Qt.

If you have typed in the source code manually, you will need to follow these instructions: To compile a C++ application, you need to create a makefile. The easiest way to create a makefile for Qt is to use the qmake build tool supplied with Qt . If you’ve saved main.cpp in its own otherwise empty directory, all you need to do is:

qmake -project
qmake

The first command tells qmake to create a project file (a.pro file). The second command tells it to create a platform-specific makefile based on the project file. You should now be able to type make (or nmake if you’re using Visual Studio) and then run your first Qt application!

From www.trolltech.com.


  3 Comments   Comment

  1. jo

    18 years ago  

    nice stuff cud u include a bit more. only the basic stuff

  2. Neha

    18 years ago  

    Thats really nice one.
    Can we have more article like that on Qt?

  3. sasi

    17 years ago  

    Ya…its really good one…Its very useful for beginners..

Leave a Comment

You must be logged in to post a comment.