Introducing Qt

Qt is a mature, cross-platform GUI toolkit written in C++. Qt is the brainchild of Trolltech, a Norwegian company that develops, markets, and supports Qt and Qt-related software for the commercial market. Trolltech loudly touts the cross-platform capabilities of Qt, which is undeniably impressive; Qt has native support on Linux and Unix derivatives, Windows, Mac OS X, and even embedded platforms, which gives Qt a great competitive advantage over its rivals.

Trolltech sold the commercial versions of Qt at a prohibitively high price for the casual user or hobbyist. Fortunately, Trolltech realized the value in offering a zero-price version to the free software community, and therefore they offered a Qt Free edition for Linux, Windows, and MacOS. In return for a free version of Qt, Trolltech got a large user install base, a large programmer community, and a high profile for its product. In June 2008 Nokia acquired Trolltech ASA to enable the acceleration of their cross-platform software strategy for mobile devices and desktop applications, and to develop its Internet services business. On September 29, 2008 Nokia renamed Trolltech to Qt Development Frameworks.

Qt Free is licensed under the GPL, which means we can program using the Qt libraries and distribute our own GPL software free of charge. As far as we can tell, the two main differences between Qt Free and Qt Professional are the lack of support and the fact that you can't use Qt software in commercial applications. The Qt Web site at http://qt.nokia.com has all the API documentation that we need.

When Qt is properly installed, the QTDIR environment variable should be set to the Qt installation directory. You can check that this is the case as follows:

1> echo $QTDIR
/usr/lib/qt3 

Let's tryout the simplest Qt program and make sure our installation is working properly.

QMainWindow 

Type this program and call it qt 1 . cpp: 

To compile, we'll need to include the Qt include and lib directories: 

$ g++ -0 qtl qtl.cpp -I$QTDIR/include -L$QTDIR/lib -lqt When we run our application, we should get a Qt window (see Figure 17-2). $ ./qtl 
Figure 17-2

How It Works

The first object we encounter is QApplication. This is the main Qt object we must construct, passing the command line arguments before we begin. Each Qt application must have one and only QApplication object that we must create before doing anything else. QApplication deals with under-the-hood Qt operations such as event handling, string localization, and controlling the look and feel.

There are two QApplication methods we use: setMainWidget, which sets the main Widget of our application, and exec, which starts the event loop running. exec doesn't return until either QApplication::quit() is called or the main widget is closed.

QMainWindow is the Qt base window widget that has support for menus, a toolbar, and a status bar. It will be featured a great deal later on as you learn how to extend it and add widgets to create an interface.

Reference

This document was excerpted from

Neil Matthew and Richard Stones, Beginning Linux Programming, 3rd Edition
Wrox (Wiley Publishing), 2004. ISBN 0-7645-4497-7. p. 678-681.


Maintained by John Loomis, last updated 31 January 2010