Tuesday, August 05, 2008

Qt - Conclusion

Strengths of Qt

The resource editor of Qt is very powerful. The message loop of Qt can be coded manually, unlike several other widget toolkits. It has a powerful layout manager to simplify work.

Qt consists of several powerful features such as Controls, XML, Regular Expressions, Platform Independence, Template classes, memory management, Network API, Database API, OpenGL API. It also has a very good documentation, both online and offline.

Future of Qt

Qt aims at further simplification of UI designing for various platforms. It is now concentrating on extending itself to embedded systems such as mobile phones and PDAs.

Better services are expected for music, video, television, imaging, games and business mobility for mobile devices with the help of Qt. Better cross-platform independence is to be achieved and further extension of Qt into other languages is expected.

Qt Designer

Qt Designer is a GUI based Qt programming tool to make UI programming simpler. It is based on the concept of dialogs which is further built on XML.

Several looks can be previewed using it like Motif, Windows, etc. Simplifies the concept of Signals And Slots using GUI. Supports creation of complex widgets and user-defined slots.

Download the entire presentation here.

Code Less ...
Create More ...
Deploy Everywhere ...

Qt - Gaming In Qt

The previous article covered a simple GUI application, let’s extend this concept to a game, written completely in Qt. Here is the screen-shot of the game. This game is popularly known as "Pocket Tanks" in the gaming market.

I won’t be explaining the entire code here, but let me give you a description about it. Like before, lets first split the entire setup into simpler components.

  • Three buttons - Quit, Shoot, New Game
  • Two LCD Numbers - Hits, Shots Left
  • Two LCD Numbers connected to sliders - Angle, Force
  • A Screen consisting of a cannon, wall and a brick

Download the source here.

First let’s construct the complex widget - LCD Range where a LCD Number is connected to a slider. A LCD Number is created and a slider is also created with specifying a certain range. These two are connected by generating a signal from slider that the value has changed to the LCD Number slot display.

Two of these complex widgets are created - Angle and Force.

Now let’s design the gaming area itself. Here we will be using a very powerful tool known as the QPainter. It provides us with tool for drawing almost anything, lines, arcs, circles, etc.

The wall and the brick are rectangles each with their own dimensions and color. Now comes the cannon, we use a circle here with a rectangle covering a part of it corresponding to the angle. As the angle varies, the position of the rectangle will also vary. Also the brick is placed at a random to make the game more challenging.

Once the UI is done the next step is building the AI (Artificial Intelligence) for the game. Going back to physics, for a body projected at a certain angle, the following formulae exist

Vx = V * cos(angle)
Vy = V * sin(angle)
X = x + Vx*t
Y = y + Vy*t - ½*g*t*t


Vx - starting velocity in x- direction
Vy - starting velocity in y- direction
X - position in x direction of the cannon-ball after a time t from firing
Y - position in y direction of the cannon-ball after a time t from firing
g - acceleration due to gravity

Now these objects are placed properly and set as the main widget to the application. Compile it and run it. That’s it, happy gaming.

Qt - A Simple Example

Enough of the theory; let’s get into the action. Here is a sample window which will take a name and displaying a message. Before we get into the coding part, it is more important to analyze how the window works.

Basically this window can be broken down into 4 components

  1. A Text Label
  2. A Text Field
  3. A Button
  4. A Message Box

Now let’s see how the system works. A user keys in his name, then clicks the button, once the button is clicked a message box appears which reads from the text field and displays a message. Download the source from here.

Primarily an application is created from the QApplication class, and then a complex widget is created and assigned as the main widget of the application and then is displayed after resizing it.

As there are many simple widgets in the application, a complex widget is created from them, and then set to the application. Here the class ComplexBox represents the complex widget. Remember every widget is derived from the QWidget class.

Firstly the components are to be created and then placed on the screen; this is done by using a layout, namely the grid layout. Where the component is placed based on the co-ordinates of the layout. For example the text label is placed at position (1, 1) extending to (1, 2), that is it occupies 2 columns and 1 row starting from (1,1). Similarly all the other components are placed.

Next we have the inter-actions between various components. As we can see, clicking the button leads to display of the message. This is the perfect situation of an action listener; this is implemented in Qt by a signal-slot relationship. The signal and the slot are primarily functions. Here the signal is generated by a pre-defined signal called the clicked() of the button, as a result an action is performed by the call() slot of the ComplexBox object itself. As seen the slot is declared in the header.

That’s it; our first Qt program is over. Run these commands to compile it

qmake –project

Qt - Programming In Qt

The primary prerequisite of learning Qt is C++. As already stated that Qt builds on the libraries of C++, it is vital to know the concepts of C++ like classes; private, protected and public members; inheritance, etc.

Like any traditional C++ program, the execution of a Qt program also starts from the function main(). A few enhancements do exist for Qt such as Signals And Slots. These are primarily responsible to establish connectivity between various widgets of the application.

Every Qt program consists of an application and several widgets built inside it.

Main Classes of Qt

Qt is based on widgets and intercommunication. A widget is any component with which a user can interact. It can be a button, a text box, anything. Complex widgets can be created from simpler ones.

Like any GUI toolkit, even Qt has several predefined widgets, namely QPushButton, QRadioButton, QCheckBox, QLabel, QLineEdit, QLCDNumber, QSlider, etc.

An entire list of Qt classes can be found in the help documentation of Qt, under the section Qt’s classes.

Writing A Program

The primary steps to be followed while writing a program are

  • Define the problem properly
  • Make sure what the widgets used will be, and how they are interconnected

Every Qt program is an application and is created from QApplication class. Further widgets are created and are attached to this application. And later the application is displayed. Generally simpler widgets are combined to form a complex widget, which is then used in the program.

Qt - Introduction To KDE

KDE stands for K Desktop Environment. Qt can be significantly experienced on a KDE desktop. The Qt toolkit was used for the KDE project. Every window available on the KDE desktop is a part of the Qt design.

Actually KDE is primarily built on several technologies such as

  1. KHTML - an HTML engine
  2. Plasma - desktop and panel widget engine
  3. Phonon - multimedia framework
  4. Solid - device integration framework

On top of this the entire UI is built on the technology of KDELibs, which is in turn built on Qt.

Qt - Introduction And Working

Qt is a cross-platform application development framework, widely used for the development of GUI and non-GUI programs. Originally Qt was developed by a Norwegian company, Trolltech. It is presently being acquired by Nokia.

Examples of GUI applications using Qt are the VLC Media Player, Opera, Adobe Photoshop Album, Google Earth, Mathematica, etc. Non GUI applications include console tools and servers.

Qt is primarily based on C++ with several non-standard extensions. That is Qt was built by modifying the traditional C++ so as to achieve easier GUI programming. It uses an additional preprocessor that converts this code into standard C++ code before compilation.

Notably, Qt isn’t restricted to UNIX, it is also extended to various platforms such as Windows (98, NT, ME, 2000), Macintosh (Mac OS X). Further, Qt can be used in several programming languages such as Ada (QtAda), C# (Qyoto), Java (Qt Jambi), Ruby (RubyQt) and several others. Qt is also available for embedded systems such as cell-phones, PDA’s and others under the name of Qtopia.

Coming to the working of Qt, Qt uses the native APIs of the underlying platform so as to draw the Qt controls, achieving platform independence. It also offers SQL database access, XML parsing and threads management.

Further a new concept was introduced to C++ on a whole, which was that of intercommunication, under the name of Signals and Slots. This was possible with a tool known as the Meta Object Compiler. This tool handles the mechanism of signals and slots, run-time type information and dynamic property system. However this concept has been greatly criticized for the absence of type safety.

Qt - UI (User-Interface) Designing

Technically UI design refers to the design of computers, applications and websites with the focus on the users experience and interaction. There are several tools for UI design.

  • Windows API specific to only Microsoft Windows. This is achieved by primarily including “windows.h” and using its functions to create windows
  • Java and Swing. The Swing package of Java can be used to create any UI required. However the entire Swing package is based on Java, and to run any of these windows, a JVM is required

Coming to UNIX, several toolkits have been designed for the X Window System. The most popular of them being -

  • GTK+, standing for “The GIMP Toolkit” - Examples of applications using this is the GIMP, Terminal and others
  • Qt, pronounced as cute

The KDE desktop environment is built on Qt. And the GNOME (GNU Object Model Environment) desktop environment is built on GTK+.