Qt GUI Toolkit

This GUI toolkit makes porting graphics to multiple platforms a snap.

Developing applications with graphical user interfaces (GUI) takes time and can be hard work. Making these applications work across different operating systems can be even more complex. Traditionally, applications have been developed for one platform, and then large amounts of the code have been rewritten to port the application to other platforms. Multi-platform GUI toolkits have changed that procedure.

A multi-platform GUI toolkit makes it easier to port applications between platforms. Developing applications with a GUI toolkit is also considerably easier and a lot less work than using a window-system directly (e.g., X11 or Windows). The Qt toolkit is a multi-platform C++ GUI toolkit (class library) that has been developed over a 4 year period. The company Troll Tech AS was founded 2 1/2 years ago to secure future development of Qt.

As one of the Qt developers, I can give you an introduction to and overview of Qt. In the process, I'll throw in my 2 cents worth of general GUI-programming techniques.

The following sections can be found in this article:

  • The Qt Story—background information about Qt

  • Signals and Slots—Qt's object communication mechanism

  • The Qt Paint Engine—drawing graphics with Qt.

  • Qt Event Handling—how to get those user clicks and window system events in Qt

  • Double-buffering—a well known and very useful GUI programming technique

  • Making Your Own Software Components—how to code a new software building block

  • Dialog Boxes—putting it all together and making it run

  • Hints and Tips—my 2 cents worth of GUI-programming experience.

The Qt Story

The first publicly available version of Qt was released in May 1995. Version 0.98 was recently released (July 1996) and included complete source code for X11. Version 1.0 is scheduled for September 1996. Qt for X11 has a non-commercial license which grants any developer the right to use Qt to develop software for the free software community. The non-commercial version of Qt includes the full X11 source code. With this license, Troll Tech hopes to promote the development of high quality free software. Qt is an emulating GUI toolkit which allows programmers a choice between the Motif and the Windows look and feel. It implements its own widgets (user interface elements), and the X11 version of Qt is implemented directly on top of Xlib and uses neither Xt nor Motif. Practically all classes and member functions in Qt are documented. The documentation is available in HTML, postscript, text and as manual pages. The HTML version is fully cross-referenced with links to code examples. In addition, there is a tutorial for the Qt beginner. You can see the documentation on the web at http://www.troll.no/qt/.

Troll Tech has used Linux as its main development platform for over 2 years. All X11 development is done first on Linux, then the source is moved to other platforms for testing and porting. Qt currently runs under several variants of UNIX, Windows 95 and Windows NT.

Signals and Slots

Let's first look at the part of Qt that probably differs most from other GUI toolkits—the mechanism for object communication. One of the most feared and hackish aspects of GUI programming has always been the dreaded callback-function. In most toolkits, widgets have a pointer to a function for each action they trigger. Anyone who has worked with function pointers knows that this can get quite messy. Qt has approached the problem of communication between GUI objects (and other objects for that matter) in a totally new way. Qt introduces the concepts of signals and slots, that eliminate the need for pointers to functions, and provide a type-safe way to send arguments of any type. All Qt objects (classes that inherit from QObject or its descendants, e.g., QWidget) can contain any number of signals and slots. When an object changes its internal state in a way that might be interesting to the outside world, it emits a signal (not to be confused with UNIX interprocess signals), and then goes on happily minding its own business, never knowing or caring if anybody receives the signal. This important feature allows the object to be used as a true software component. Slots are member functions that can be connected to signals. A slot does not know or care if it has a signal connected to it. Again, the object is isolated from the rest of the world, and can be used as a true software component. These two simple concepts make up a powerful component programming system. They may seem awkward when encountered for the first time, but they are a lot more intuitive and easier to both learn and use than the alternatives. Let's look at how signals and slots are specified in a class declaration. The following class is a stripped down version of the class shown in code Listing 3:

class PixmapRotator : public QWidget
{
    Q_OBJECT
public:
    PixmapRotator( QWidget *parent=0,
        const char *name=0 );
public slots:
    void  setAngle( int degrees );
signals:
    void  angleChanged( int );
private:
    int         ang;
};

Signals and slots are specified syntactically using C++ categories in the class declaration. This class defined above has a slot called setAngle. Slots are normal member functions and must have an access specifier. They are, as with other member functions, implemented by the programmer, and can be overloaded or virtual.

The PixmapRotator class has a single signal, angleChanged, which it emits when its angle has changed value. Signals are declared in the class declaration by the programmer but the implementation is generated automatically. To emit a signal, type:

emit signal( arguments )

The implementation of the slot looks like this:

void PixmapRotator::setAngle( int degrees )
{
// keep in range <-360, 360>
    degrees = degrees % 360;
// actual state change?
    if ( ang == degrees )
        return;
    ang   = degrees;            // a new angle
    emit angleChanged( ang );   // tell world
        ...
}

Note that setAngle only emits the signal, if the value actually changed (as the name of the signal implies). A signal should only be emitted when a state change has occurred.

To connect a signal to a slot the QObject static member function connect is used, for example:

connect( scrollBar, SIGNAL(valueChanged(int)),
                 rotator,   SLOT(setAngle(int)) );

Here the QScrollBar scrollBar's signal valueChanged is connected to the PixmapRotator rotator's slot setAngle. This statement assures that whenever the scrollbar changes its value (e.g., if the user clicks on one of its arrows) the angle of the PixmapRotator object will change accordingly. The two objects can interact without knowing about each other as long as a connection is set up by a third party.

As you can see, signals and slots can have arguments. The last argument(s) from a signal can be discarded, but otherwise the arguments must match for a connection to be made.

An arbitrary number of slots can be connected to a single signal and vice versa.

Technically, signals and slots are implemented using the Qt meta object compiler (moc). It parses C++ header files and generates C++ code necessary for Qt to handle signals and slots. The signals, slots and emit keywords are macros, so the compiler preprocessor changes or removes them.

Signals and slots are efficient. Of course they are not as fast as a direct function pointer call, but the difference is small. A signal triggering a slot has been measured to approximately 50 microseconds on a SPARC2.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

GUI deveopment using QT

Anonymous's picture

i wanted to know as to how a GUI can be developed using QT with C++ program give an example .

linux

sowmya's picture

sir,
i am troubling about qt.i am begineer of linux.In my project i am doing in linux.for gui designing we use qt design.i not very well about qt:gui design and where we write code in qt,and how to complie in c++ coding.i am using fedora.pls give me steps for designing and coding... i am waiting..

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix