Open-source development tools (ssh, bash, gcc) are available natively on UNIX and their related platforms. This includes Mac OS/X, which is a BSD-based distribution.
 BSD stands for Berkeley Software Distribution, a facility of the Computer Systems Research Group (CSRG) of the University of California at Berkeley. CSRG has been an important center of development for UNIX and for various basic protocols (e.g., TCP/IP) since the 1980s.
When we discuss something that's specific to UNIX-derived platforms (Linux, BSD, Solaris, etc.), we will use the shorthand *nix for "most flavors of UNIX."
Another important acronym is POSIX, which stands for Portable Operating System Interface for UNIX. The development of this family of standards was sponsored by the IEEE (Institute of Electrical and Electronics Engineers), an organization for engineers, scientists, and students that is best known for developing standards for the computer and electronics industry.
 If we wanted to write a POSIX regular expression (see Section 13.2) for *nix, it might look like this: (lin|mac-os|un|solar|ir|ultr|ai|hp)[iu]?[sx].
This section is for readers who are using a computer with some flavor of *nix installed.
The examples in this book were tested with Qt 4.1. We recommend that you use the same version or a later one. The first step in setting up your computer for this book is to make sure that the full installation of Qt 4 is available to you. This includes, in addition to the source and compiled library code, the Qt Assistant documentation system, program examples, and the Qt Designer program.
If your system has KDE 3.x (the K Desktop Environment) installed, then there is already a run-time version of Qt 3.x installed. Qt 4 needs to be installed separately. Our examples do not compile under Qt 3.
To see which (if any) version of Qt has already been installed on your system, start with the following commands:
which qmake qmake -v
The output of the first command tells you where the qmake executable is located. If that output looks like this: bash: qmake: command not found, it is possible that
If you can run it, qmake -v provides version information. If it reports
Using Qt version 4.x.y
then, check whether these other Qt tools are available:
which moc which uic which assistant which designer
If these executables are all found, and from the same place as qmake, Qt 4 is installed and ready to use.
If the tests outlined above indicate that you have an earlier version or no Qt installed, or that you are missing some components of Qt 4, then you will need to install the latest release of Qt 4.
Installing Qt 4 from Packages
On some *nix systems it is possible to install Qt 4 from a package that contains compiled code.
Using your *nix package manager (e.g., apt, urpmi, aptitude, kpackage, synaptic, rpmdrake, etc.), you can easily and quickly install the packages that comprise Qt 4. Here is a list of packages available on Debian systems (the lists will differ on other distros).
[ROOT@lazarus]# apt-cache search qt4 libqt4-core - Qt 4 core non-GUI functionality run-time library libqt4-debug - Qt 4 debugging run-time libraries libqt4-dev - Qt 4 development files libqt4-gui - Qt 4 core GUI functionality run-time library libqt4-qt3support - Qt 3 compatibility library for Qt 4 libqt4-sql - Qt 4 SQL database module qt4-designer - Qt 4 Designer qt4-dev-tools - Qt 4 development tools qt4-doc - Qt 4 API documentation libqt4-designer - Qt 4 Designer libraries [ROOT@lazarus]#
As you can see, in Debian, Qt 4 has been split into many separate packages to give package maintainers more flexibility when they deploy. When developing, you need the full Qt 4 package with developers' tools, full headers, docs, designer, assistant, and examples.
1.3.2. Downloading from Source
You can download, unpack, and compile the latest open-source tarball from Trolltech. Two typical places to unpack the tarball are /usr/local/ (if you are root) or $HOME/qt (if you are not).
A tarball is a file produced by the tar command (tape archive) that can combine many files, as specified in the command line, into one file (which is generally given the extension .tar) for ease of storage or transfer. The combined file is generally compressed using a utility like gzip or bzip2, which appends the extension .gz or .bz to the tar file.
The command line for unpacking a tarball depends on how it was created. You can usually determine this from its extension.
tar -vxf whatever.tar // uses the "verbose" switch tar -zxf whatever.tar.gz // compressed with gzip tar -zxf whatever.tgz // also compressed with gzip tar -jxf whatever.tar.bz2 // compressed with bzip2
A tar file can preserve directory structures and has many options and switches. You can read the online documentation for these utilities by typing:
info tar info gzip info bzip
The Qt source tarball contains the complete source code of the Qt library, plus numerous examples, tutorials, and extensions with full reference documentation. The tarball contains simple installation instructions (in the README and INSTALL files) and a configure --help message. Be sure to read the README file before you attempt to install software from any open-source tarball.
Compiling from source can take 2 to 3 hours (depending on the speed of your system), but it is worth the time. Example 1.1 shows the options we use to configure qt:4.
Example 1.1. ../bin/qtconfigure
#!/bin/sh # This is a script which I use to configure and # make qt4 - it includes extra options I use for working with mysql. # # Before you run this, be sure you have at least the # mysql client header files : # apt-get install libmysqlclient-dev mysql-server mysql-client INSTALLDIR=/usr/local/qt4 ./configure -prefix $INSTALLDIR -fast -qt-gif # for mysql: -I/usr/include/mysql -qt-sql-mysql # for odbc: -plugin-sql-odbc -qt-sql-odbc make
You can run this as a regular user as long as you have write permissions in that directory. Notice that this script runs make right after it is finished with configure, so it will run for quite a while.
In the final step, make install copies the executables and headers into another location from the unzipped tarball source tree. If you are installing in a common location, you need to be root to do this.
After installation, try qmake -v to determine which version of qmake is found by your shell. For systems that have more than one version installed, this is especially important to do.
> which qmake /usr/local/qt-x11-opensource-src-4.1/bin/qmake > qmake -v QMake version: 2.00a Using Qt version 4.1 in /usr/local/qt-x11-opensource- src-4.1/lib >
After installing, check your environment variables and make sure that
 Qt 4 does not require the use of this environment variable, but we use this variable to refer to the "Qt install" directory in our examples.
The bash command env will display the current values of all your environment variables.
The shell script in Example 1.2 shows how we set the values with bash, but the actual values depend on where the files are located on your system.
Example 1.2. src/libs/utils/qt.sh
# Typical user specific environment and startup values for QT # depending on how and where qt was installed on your system export QTDIR=/usr/local/qt4 export PATH=$QTDIR/bin:$PATH export MANPATH=$QTDIR/doc/man:$MANPATH # Location of your own libraries - source and target. export CPPLIBS=~/projects/libs # LD Library path - where to search for shared object libraries # at runtime. export LD_LIBRARY_PATH=$CPPLIBS:$QTDIR/lib:$LD_LIBRARY_PATH
Part I: Introduction to C++ and Qt 4
Introduction to Qt
Inheritance and Polymorphism
Part II: Higher-Level Programming
Introduction to Design Patterns
Generics and Containers
Qt GUI Widgets
Validation and Regular Expressions
Meta Objects, Properties, and Reflective Programming
More Design Patterns
Models and Views
Qt SQL Classes
Part III: C++ Language Reference
Types and Expressions
Scope and Storage Class
Statements and Control Structures
Inheritance in Detail
Part IV: Programming Assignments
MP3 Jukebox Assignments
Part V: Appendices
MP3 Jukebox Assignments
MP3 Jukebox Assignments