• Aucun résultat trouvé

The Qt Libraries

Dans le document Qt 4 THE BOOK of THE BOOK of (Page 44-49)

Basics, Tools, and First Code

1.4 Base Classes in Qt

1.5.1 The Qt Libraries

Today, the common usage of the termGUI toolkitconveys just a small part of what such a system offers. Qt in particular includes relatively extensive classes for various aspects of application development. Many of these relate to the programming of graphical interfaces, but there are also classes for network programming, OpenGL support, database and XML development, and many more. The focus throughout lies on the platform independence of the classes: With very few exceptions, the same classes are available on all supported operating systems, with the same func-tions and the same behavior.

Qt 4.0 consists of the following program libraries:

QtCore contains base classes that do not generate any GUI output.

QtGui contains the base GUI classes.

QtNetwork contains the network classes.

QtOpenGL contains the OpenGL support.

9 See http://doc.trolltech.com/4.2/hierarchy.html.

QtSql contains the SQL database classes.

QtXml contains the XML and DOM classes (see page 45).

QtAssistantClient allows you to use Qt assistant as a documentation browser in your application.

Qt3Support includes classes that ensure compatibility with Qt 3.

Qt 4.1 added the QtSvg library, which provides support for the SVG vector graphics format, as well as the QtTest library, also called QTestLib, which contains a frame-work for writing unit tests.

Finally in Qt 4.2, Trolltech added the QtDBus module, which provides the Qt bind-ings for the message bus system from Freedesktop.org.10

You may need to link an application’s code to several libraries, often including QtCore and QtGui. For this reason, qmake uses both libraries by default.

The libraries to be linked are specified by the qmake variable QT. By default it con-tains the values core and gui. For example, to write a GUI program with network support, you would add the value network to this variable. This is brought about in the .pro file with the line

QT += network

To write a command-line program with XML support that merely links with QtCore and QtXml, and not with QtGui, you must add xml and remove the value gui. This is done with the following lines:

QT -= gui QT += xml

To use all of the libraries in Qt 4.0, write:

QT += network opengl sql xml support

In addition to specifying project files, there is another topic for which knowledge of the contents of the Qt libraries is of particular interest. Besides the header files for individual class definitions, whose filenames match the names of the classes they describe, Qt also provides header files for its libraries. Each of these files contains the interface descriptions of all of the classes of a library; the name of the header file matches the name of the library. Thus, in the examples so far, which have only used classes from QtGui, instead of the many separate #include statements, we could simply have written

10 See http://www.freedesktop.org/wiki/Software/dbus.

#include <QtGui>

However, these library header files are very long, which considerably increases the length of the compiling process. This won’t be a problem if the compiler supports precompiled header files, but only the more recent compilers do (such as with GCC from version 3.4 on).

The Base Library QtCore

QtCore is a partial library required by every Qt program. Among other things, it makes available the following:

Basic data types, such as QString and QByteArray Basic data structures, such as QList, QVector, and QHash Input/output classes such as QIODevice, QTextStream, and QFile

Classes with which multiple threads can be programmed (including QWaitCon-dition and QThread)

The classes QObject and QCoreApplication (the base class for QApplication) None of these classes depends on GUI components. This separation from the GUI allows Qt applications (such as command-line programs) to be written that do not implement a GUI

In nongraphical programs the QCoreApplication class takes on the role of the QAp-plication class in GUI apQAp-plications: It makes an event loop available. This is useful if you require asynchronous communication, whether between different threads or via network sockets.11

The GUI Library QtGui

The QtGui library contains all classes that are necessary for programming graphical user interfaces, including the following:

The QWidget class and classes derived from it, such as QLabel and QPushButton The layout classes (including QVBoxLayout, QHBoxLayout, and QGridLayout) Classes such as QMainWindow and QMenu, which are needed if you want to add menus to an application

11 QtCore does not contain any network classes, but the QtNetwork library can be used with QtCore if networking is required.

Classes for drawing, such as QPainter, QPen, and QBrush

Classes providing ready-to-use dialogs (including QFileDialog and QPrintDialog) The QApplication class

QtGui requires the QtCore library.

The Network Library QtNetwork

The partial library QtNetwork provides classes for writing network applications.

In addition to supporting simple socket communication via the QTcpSocket and QUdpSocket classes, this library also enables client-side HTTP and FTP with QHttp and QFtp.

Unlike QtGui, QtNetwork requires the QtCore library, but it can, of course, be used together with QtGui and the other libraries.

The OpenGL Library QtOpenGL

The QtOpenGL library enables OpenGL to be used in a Qt program. It provides the QGLWidget class—a Qt widget in which you can draw using OpenGL commands.

QtOpenGL uses the QtCore and QtGui libraries.

The Database Library QtSql

The QtSql library classes provide access to SQL databases in Qt programs. This library includes classes that are used to establish a connection with an SQL database and to query and modify data. Qt supports a range of SQL databases, including the open source databases PostgreSQL, MySQL, and SQLite.

QtSql requires the QtCore library, and it is discussed at length in Chapter 9 (page 257).

The XML Library QtXml

A simple, nonvalidating XML parser is provided by the partial library QtXML. It can be addressed directly through a SAX2 interface (Simple API for XML).

QtXml also contains an implementation of the DOM standard (Document Object Model). The corresponding classes allow you to parse an XML document, manipu-late its tree structure, publish the modified document again as an XML document, or to create a new XML document with DOM.

This library requires only the QtCore library, and it is discussed in more depth in Chapter 13 (page 353).

The Compatibility Library Qt3Support

Compared with its predecessor, Qt 3, Qt 4 has undergone considerable develop-ment: Some classes contain changes that are incompatible with the Qt 3 versions, and others have been replaced in Qt 4 with completely new classes with different names. In order to simplify the porting of Qt 3 programs to Qt 4, Trolltech includes the corresponding Qt 3 classes in the Qt3Support library. However, you should not use this library for new programs, since development of their classes has stopped.

Since this book explains programming with Qt 4, we will not use these classes and will not discuss them further.

The Vector Graphics Library QtSvg

The SVG vector graphics format, published by the W3 consortium and based on XML, has great potential. From Qt 4.1 onward the QtSvg library supports the SVG profiles SVG Basic and SVG Tiny,12which can be used to display SVG files and animations, although it cannot as yet create them or, as in XML, manipulate them through a DOM tree.

The QtAssistantClient Library

The assistant client library allows you to remotely control the Qt assistant applica-tion. This allows you to use the assistant as a platform-independent help browser for your application. The heart of the module is the QAssistantClient class.

Customized help pages for use with Qt assistant are provided in basic HTML markup, along with an XML file that describes the structure of the documentation.

The Test Case Library QTestLib

Originally released outside the Qt core distribution for paying customers, QTestLib entered the regular Qt distribution starting with the Qt 4.1.0 release. The library contains facilities to write proper unit tests for newly written classes, and covers a scope similar to JUnit in Java.

12 See http://www.w3.org/TR/SVGMobile/.

The QtDBus Library

DBus is a messaging protocol that has emerged as a de facto standard on Linux and other Unix derivates. For instance, the Linux Hardware Abstraction Layer (HAL) and the upcoming KDE 4 are using DBus for interprocess communication. Even though ports for Windows and Mac OS X exist, Qt 4.2 will offer to build this library only on Unix. This may, however, change in future versions.

ActiveQt and Migration Classes

The platform-specific extension ActiveQt for Windows makes it possible to imple-ment Active X components with Qt and to use them in Qt programs. It is available only in the commercial Qt desktop edition, however.

Trolltech also provides migration solutions for MFC-, Motif-, and Xt-based appli-cations. Like ActiveQt, however, they are available only as separate commercial add-ons for Qt 4, theQt Solutions,13and will not be discussed in this book.

Dans le document Qt 4 THE BOOK of THE BOOK of (Page 44-49)