Tải bản đầy đủ (.pdf) (504 trang)

C++ GUI programming with qt3 2 2005

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (39.16 MB, 504 trang )

[ Team LiB ]



Table of Contents



Index

C++ GUI Programming with Qt 3
ByJasmin Blanchette , Mark Summerfield

Publisher: Prentice Hall PTR
Pub Date: January 15, 2004
ISBN: 0-13-124072-2
Pages: 464

"...not only the best book on Qt I have ever seen, but also the best book presenting any
programming framework. Every sentence appears to be carefully worded, and every chapter
has a sound concept, and so does the work as a whole."
-Matthias Ettrich, Trolltech's lead developer, founder of the KDE project
"The 'Tao of Qt'.... The Qt system is a beautiful example of object oriented design, and the
authors take advantage of this.... The authors have done an excellent job of presenting the
subject in an interesting and engaging way...."
-Ron McCarty, Instructor and Chair of the Department of Computer Science, Penn State Erie,
The Behrend College
The first official Trolltech guide to Qt 3.2 programming!
Straight from Trolltech, this book covers all you need to build industrial-strength applications
with Qt 3.2.x and C++-applications that run natively on Windows, Linux/Unix, Mac OS X, and
embedded Linux with no source code changes! The book teaches solid Qt programming


practices; it is not a rehash of the documentation. You'll find start-to-finish coverage packed
with examples, plus a CD with the Qt 3.2 toolset and Borland C++ compilers-including a noncommercial Qt 3.2 for Windows available nowhere else!
Build powerful C++ GUI applications quickly and easily
Design dialogs and main windows visually and in code
Learn Qt's innovative typesafe signals and slots mechanism
Use layouts to create forms that automatically size and scale
Create custom signals, slots, events, and controls
Program the ''Qt way'' with techniques for Qt 3.2 that'll work with Qt 4
1


Code
with menus, toolbars, dialogs, and drag and drop
[ Team
LiB applications
]
Utilize 2D and 3D graphics, multithreading, and networking
Write database and XML applications
Internationalize to reach foreign markets
Exploit platform-specific-features like ActiveX
Already using Qt or just starting out? Evaluating Qt or managing it? Building open source
applications-or commercial applications? Want to develop for Windows without buying an
expensive compiler? Whatever your goal, this is the only book you need! CD-ROM: Everything
you need to write great GUI programs!



Qt 3.2 Non-Commercial
Table of Contents Edition for Windows




Index

Borland
C++ 5.5
Non-Commercial
C++ GUI
Programming
with
Qt 3

Edition

ByJasmin
Blanchette
Borland
C++,Mark
6.0 Summerfield
Trial Edition

Qt 3.2 Free Editions for Unix/Linux/Mac OS X
Publisher: Prentice Hall PTR
Pub
SQLite
Date: January
database
15, 2004
ISBN: 0-13-124072-2


The
book's code examples
Pages: 464
[ Team LiB ]
"...not only the best book on Qt I have ever seen, but also the best book presenting any
programming framework. Every sentence appears to be carefully worded, and every chapter
has a sound concept, and so does the work as a whole."
-Matthias Ettrich, Trolltech's lead developer, founder of the KDE project
"The 'Tao of Qt'.... The Qt system is a beautiful example of object oriented design, and the
authors take advantage of this.... The authors have done an excellent job of presenting the
subject in an interesting and engaging way...."
-Ron McCarty, Instructor and Chair of the Department of Computer Science, Penn State Erie,
The Behrend College
The first official Trolltech guide to Qt 3.2 programming!
Straight from Trolltech, this book covers all you need to build industrial-strength applications
with Qt 3.2.x and C++-applications that run natively on Windows, Linux/Unix, Mac OS X, and
embedded Linux with no source code changes! The book teaches solid Qt programming
practices; it is not a rehash of the documentation. You'll find start-to-finish coverage packed
with examples, plus a CD with the Qt 3.2 toolset and Borland C++ compilers-including a noncommercial Qt 3.2 for Windows available nowhere else!
Build powerful C++ GUI applications quickly and easily
Design dialogs and main windows visually and in code
Learn Qt's innovative typesafe signals and slots mechanism
Use layouts to create forms that automatically size and scale
Create custom signals, slots, events, and controls
Program the ''Qt way'' with techniques for Qt 3.2 that'll work with Qt 4
1
2


[ Team LiB ]




Table of Contents



Index

C++ GUI Programming with Qt 3
ByJasmin Blanchette , Mark Summerfield

Publisher: Prentice Hall PTR
Pub Date: January 15, 2004
ISBN: 0-13-124072-2
Pages: 464

Copyright

"...not
only
the best
book on
Qt I have ever seen, but also the best book presenting any
Bruce
Perens'
Open Source
Series
programming
framework.

Every
sentence appears to be carefully worded, and every chapter
Foreword
has a sound concept, and so does the work as a whole."
Preface
-Matthias Ettrich, Trolltech's lead developer, founder of the KDE project
Acknowledgments

Brief of
History
of Qt
"The A'Tao
Qt'....
The Qt system is a beautiful example of object oriented design, and the
About
the Authors
authors
take
advantage of this.... The authors have done an excellent job of presenting the
subject
in an interesting and engaging way...."
Production
-Ron About
McCarty,
Instructor and Chair of the Department of Computer Science, Penn State Erie,
the CD-ROM
The Behrend
College
Part I: Basic Qt
Chapter 1. Getting Started


The first official Trolltech guide to Qt 3.2 programming!
Hello Qt
MakingTrolltech,
Connectionsthis book covers all you need to build industrial-strength applications
Straight from
with Qt 3.2.x
C++-applications
Using and
the Reference
Documentationthat run natively on Windows, Linux/Unix, Mac OS X, and
embedded Linux with no source code changes! The book teaches solid Qt programming
practices;
it is 2.
not
a rehash
of the documentation. You'll find start-to-finish coverage packed
Chapter
Creating
Dialogs
with examples,
plus
a
CD
with
the Qt 3.2 toolset and Borland C++ compilers-including a nonSubclassing QDialog
commercial Qt 3.2 for Windows available nowhere else!
Signals and Slots in Depth
Rapid Dialog Design
Shape-Changing

Dialogs
Build
powerful C++
GUI applications quickly and easily
Dynamic Dialogs

Design
dialogs
main Classes
windows visually and in code
Built-in
Widgetand
and Dialog
Learn Qt's innovative typesafe signals and slots mechanism
Chapter 3. Creating Main Windows

Use Subclassing
layouts toQMainWindow
create forms that automatically size and scale
Creating Menus and Toolbars

Create
custom the
signals,
slots, events, and controls
Implementing
File Menu
Setting Up the Status Bar

Program the ''Qt way'' with techniques for Qt 3.2 that'll work with Qt 4

1
3


Using Dialogs
[ Team LiB
]
Storing Settings
Multiple Documents
Splash Screens
Chapter 4. Implementing Application Functionality
The Central Widget
Subclassing QTable
Loading and Saving
Implementing the Edit Menu
Implementing the Other Menus
Subclassing QTableItem
Chapter 5. Creating Custom Widgets



Table ofQt
Contents
Customizing
Widgets



Index QWidget
Subclassing

C++ GUI Programming
with Qt
3
Integrating Custom
Widgets
with Qt Designer
ByJasmin Blanchette
, Mark Summerfield
Double Buffering

Publisher:
Prentice Hall PTR
Part II: Intermediate
Qt
Pub Date:
January
15, 2004
Chapter
6. Layout
Management
ISBN:
0-13-124072-2
Basic
Layouts
Pages:
464
Splitters
Widget Stacks
Scroll Views
Dock Windows


"...not only the best book on Qt I have ever seen, but also the best book presenting any
Multiple Document Interface
programming framework. Every sentence appears to be carefully worded, and every chapter
has a sound concept, and so does the work as a whole."
Chapter 7. Event Processing
-Matthias
Ettrich, Trolltech's lead developer, founder of the KDE project
Reimplementing Event Handlers

"The 'TaoInstalling
of Qt'....
Event
The
Filters
Qt system is a beautiful example of object oriented design, and the
authors take
advantage
this....
The Processing
authors have done an excellent job of presenting the
Staying Responsive of
During
Intensive
subject in an interesting and engaging way...."
-Ron McCarty,
Instructor
Chair of the Department of Computer Science, Penn State Erie,
Chapter 8.
2D and 3D and

Graphics
The Behrend
College
Painting with QPainter
Graphics with QCanvas
The first official
Trolltech guide to Qt 3.2 programming!
Printing

Straight from
Graphics
Trolltech,
with OpenGL
this book covers all you need to build industrial-strength applications
with Qt 3.2.x and C++-applications that run natively on Windows, Linux/Unix, Mac OS X, and
embedded
Linux
withand
noDrop
source code changes! The book teaches solid Qt programming
Chapter
9. Drag
practices;Enabling
it is not
a and
rehash
Drag
Drop of the documentation. You'll find start-to-finish coverage packed
with examples,
plus a CD with the Qt 3.2 toolset and Borland C++ compilers-including a nonSupporting Custom Drag Types

commercial Qt 3.2 for Windows available nowhere else!
Advanced Clipboard Handling

Chapter
10. Input/Output
Build
powerful
C++ GUI applications quickly and easily
Reading and Writing Binary Data

Design
dialogs
and main
Reading
and Writing
Text windows visually and in code
Handling Files and Directories

Learn Qt's innovative typesafe signals and slots mechanism
Inter-Process Communication

Use layouts to create forms that automatically size and scale
Chapter 11. Container Classes

Create
custom signals, slots, events, and controls
Vectors
Lists

Program the ''Qt way'' with techniques for Qt 3.2 that'll work with Qt 4

Maps

1
4


Pointer-Based Containers
[ Team LiB
]
QString and QVariant
Chapter 12. Databases
Connecting and Querying
Presenting Data in Tabular Form
Creating Data-Aware Forms
Chapter 13. Networking
Using QFtp
Using QHttp
TCP Networking with QSocket
UDP Networking with QSocketDevice



Chapter
14. of
XML
Table
Contents




Reading
Index
XML with SAX

C++ GUI Programming
withDOM
Qt 3
Reading XML with
Writing XML
ByJasmin Blanchette
, Mark Summerfield
Chapter 15. Internationalization
Publisher:
Prentice
Hall
PTR
Working
with
Unicode
Pub Date:
January
15, 2004Translation-Aware
Making
Applications
ISBN:
0-13-124072-2
Dynamic
Language Switching
Pages:
464

Translating
Applications
Chapter 16. Providing Online Help
Tooltips, Status Tips, and "What's This?" Help

"...not only
the best book on Qt I have ever seen, but also the best book presenting any
Using QTextBrowser as a Simple Help Engine
programming framework. Every sentence appears to be carefully worded, and every chapter
Using Qt Assistant for Powerful Online Help
has a sound concept, and so does the work as a whole."
-Matthias Ettrich, Trolltech's lead developer, founder of the KDE project
Chapter 17. Multithreading

withThe
Threads
"The 'TaoWorking
of Qt'....
Qt system is a beautiful example of object oriented design, and the
authors take
Communicating
advantage
withof
the
this....
GUI Thread
The authors have done an excellent job of presenting the
subject inUsing
an interesting
and

engaging
Qt's Classes in Non-GUI Threadsway...."
-Ron McCarty, Instructor and Chair of the Department of Computer Science, Penn State Erie,
The Behrend
ChapterCollege
18. Platform-Specific Features
Interfacing with Native APIs

The first official Trolltech guide to Qt 3.2 programming!
Using ActiveX

Session
Management
Straight from
Trolltech,
this book covers all you need to build industrial-strength applications
with Qt 3.2.x and C++-applications that run natively on Windows, Linux/Unix, Mac OS X, and
embedded
Appendix
Linux
A. with
Installing
no source
Qt
code changes! The book teaches solid Qt programming
practices;A it
is not
a rehash of the documentation. You'll find start-to-finish coverage packed
Note
on Licensing

with examples,
a CD with the Qt 3.2 toolset and Borland C++ compilers-including a nonInstallingplus
Qt/Windows
commercial
Qt
3.2
for
Windows available nowhere else!
Installing Qt/Mac
Installing Qt/X11

Build powerful C++ GUI applications quickly and easily
Appendix B. Qt's Class Hierarchy

Design dialogs and main windows visually and in code
Index

Learn Qt's innovative typesafe signals and slots mechanism
[ Team LiB ]
Use layouts to create forms that automatically size and scale
Create custom signals, slots, events, and controls
Program the ''Qt way'' with techniques for Qt 3.2 that'll work with Qt 4
1
5


[ Team LiB ]

Copyright
Library of Congress Cataloging-in-Publication Data

A CIP catalog record for this book can be obtained from the Library of Congress
Editorial/Production Supervision: Kathleen M. Caren
Cover Design Director: Jerry Votta



Art Director: Gail Cocker-Bogusz
Table of Contents

Index
Manufacturing
Buyer: Maura Zaldivar

C++ GUI Programming with Qt 3

Acquisitions
Jill Harry
ByJasmin
Blanchette ,Editor:
Mark Summerfield
Editorial Assistant: Brenda Mulligan
Publisher: Prentice Hall PTR

Marketing Manager: Dan Depasquale

Pub Date: January 15, 2004
ISBN: 0-13-124072-2

Copyright
Pages:

©464
2004 Trolltech AS
Published by Pearson Education, Inc.
Publishing as Prentice Hall Professional Technical Reference
Upper Saddle River,
New Jersey
07458
"...not
only the
best book on Qt I have ever seen, but also the best book presenting any
programming framework. Every sentence appears to be carefully worded, and every chapter
This
may onlyand
be so
distributed
terms and conditions set forth in the Open
has amaterial
sound concept,
does the subject
work astoa the
whole."
Publication
License,
v1.0
or
later
(the
latest
version
is

-Matthias Ettrich, Trolltech's lead developer, founder ofavailable
the KDEat
project
/>"The 'Tao of Qt'.... The Qt system is a beautiful example of object oriented design, and the
Prentice
Halladvantage
PTR offers
excellent
ondone
this an
book
when job
ordered
in quantity
authors take
of this....
The discounts
authors have
excellent
of presenting
the
for
bulk
purchases
or
special
sales.
For
more
information,

please
contact:
subject in an interesting and engaging way...."
U.S.Corporate
and Government
1-800-382-3419,
-Ron McCarty, Instructor
and Chair Sales,
of the Department
of Computer Science, Penn State Erie,

For
sales outside of the U.S., please contact:
The Behrend College
International Sales, 1-317-581-3793,
The first official Trolltech guide to Qt 3.2 programming!
Trolltech®, Qt®, and the Trolltech logo are registered trademarks of Trolltech. OpenGL™is a
trademark
of Silicon
Graphics,
Inc.covers
in theall
United
States
countries. All other
company
Straight from
Trolltech,
this book
you need

to and
buildother
industrial-strength
applications
and
product
names
mentioned
herein
are
the
trademarks
or
registered
trademarks
of
with Qt 3.2.x and C++-applications that run natively on Windows, Linux/Unix, Mac OStheir
X, and
respective
embedded owners.
Linux with no source code changes! The book teaches solid Qt programming
practices; it is not a rehash of the documentation. You'll find start-to-finish coverage packed
The
copyright
holder,
andQt
publisher
haveand
taken
care C++

in thecompilers-including
preparation of this abook,
with authors,
examples,
plus a CD
with the
3.2 toolset
Borland
nonbut
make
no
expressed
or
implied
warranty
of
any
kind
and
assume
no responsibility for errors
commercial Qt 3.2 for Windows available nowhere else!
or omissions. The information in this book is furnished for informational use only, is subject to
change without notice, and does not represent a commitment on the part of the copyright
holderBuild
or the
publisher.
liability
is assumed
for and

incidental
powerful
C++No
GUI
applications
quickly
easily or consequential damages in
connection with or arising out of the use of the information or programs contained herein.
Design dialogs and main windows visually and in code
The software described in this book is furnished under a license agreement or non-disclosure
agreement.
Learn Qt's
The innovative
software may
typesafe
be used
signals
or copied
and slots
only mechanism
in accordance with the terms of the
agreement.
Use layouts to create forms that automatically size and scale
Printed in the United States of America
Create custom signals, slots, events, and controls
First Printing
Program the ''Qt way'' with techniques for Qt 3.2 that'll work with Qt 4
1
6



Pearson
Education
Ltd.
[ Team LiB
]
Pearson Education Australia Pty., Limited
Pearson Education Singapore, Pte. Ltd.
Pearson Education North Asia Ltd.
Pearson Education Canada, Ltd.
Pearson Educación de Mexico, S.A. de C.V.
Pearson Education-Japan
Pearson Education Malaysia, Pte. Ltd.
[ Team LiB ]



Table of Contents



Index

C++ GUI Programming with Qt 3
ByJasmin Blanchette , Mark Summerfield

Publisher: Prentice Hall PTR
Pub Date: January 15, 2004
ISBN: 0-13-124072-2
Pages: 464


"...not only the best book on Qt I have ever seen, but also the best book presenting any
programming framework. Every sentence appears to be carefully worded, and every chapter
has a sound concept, and so does the work as a whole."
-Matthias Ettrich, Trolltech's lead developer, founder of the KDE project
"The 'Tao of Qt'.... The Qt system is a beautiful example of object oriented design, and the
authors take advantage of this.... The authors have done an excellent job of presenting the
subject in an interesting and engaging way...."
-Ron McCarty, Instructor and Chair of the Department of Computer Science, Penn State Erie,
The Behrend College
The first official Trolltech guide to Qt 3.2 programming!
Straight from Trolltech, this book covers all you need to build industrial-strength applications
with Qt 3.2.x and C++-applications that run natively on Windows, Linux/Unix, Mac OS X, and
embedded Linux with no source code changes! The book teaches solid Qt programming
practices; it is not a rehash of the documentation. You'll find start-to-finish coverage packed
with examples, plus a CD with the Qt 3.2 toolset and Borland C++ compilers-including a noncommercial Qt 3.2 for Windows available nowhere else!
Build powerful C++ GUI applications quickly and easily
Design dialogs and main windows visually and in code
Learn Qt's innovative typesafe signals and slots mechanism
Use layouts to create forms that automatically size and scale
Create custom signals, slots, events, and controls
Program the ''Qt way'' with techniques for Qt 3.2 that'll work with Qt 4
1
7


[ Team LiB ]

Bruce Perens' Open Source Series
C++ GUI Programming with Qt 3

Jasmin Blanchette, Mark Summerfield
Managing Linux Systems with Webmin: System Administration and Module Development
Jamie Cameron




Understanding the Linux Virtual Memory Manager
Mel Gorman
Table of Contents

Index
Implementing
CIFS: The Common Internet File System

C++ GUI
Programming
Qt 3
Christopher
R. with
Hertel
ByJasmin Blanchette , Mark Summerfield

Embedded Software Development with eCos
Anthony J. Massa
Publisher: Prentice Hall PTR

Rapid Application Development with Mozilla
Nigel McFarlane


Pub Date: January 15, 2004
ISBN: 0-13-124072-2

Pages:
464
The
Linux

Development Platform: Configuring, Using, and Maintaining a Complete
Programming Environment
Rafeeq Ur Rehman, Christopher Paul
with Snort:
Advanced
IDSthe
Techniques
Snort, Apache,
"...notIntrusion
only theDetection
best bookSystems
on Qt I have
ever seen,
but also
best bookwith
presenting
any
MySQL, PHP,
and ACID
programming
framework.
Every sentence appears to be carefully worded, and every chapter

Ur Rehman
has aRafeeq
sound concept,
and so does the work as a whole."
-Matthias Ettrich, Trolltech's lead developer, founder of the KDE project
The Official Samba-3 HOWTO and Reference Guide
John of
H.Qt'....
Terpstra,
R. Vernooij,
Editors
"The 'Tao
The Jelmer
Qt system
is a beautiful
example of object oriented design, and the
authors take advantage of this.... The authors have done an excellent job of presenting the
subject in an interesting and engaging way...."
[-Ron
Team
LiB ] Instructor and Chair of the Department of Computer Science, Penn State Erie,
McCarty,
The Behrend College
The first official Trolltech guide to Qt 3.2 programming!
Straight from Trolltech, this book covers all you need to build industrial-strength applications
with Qt 3.2.x and C++-applications that run natively on Windows, Linux/Unix, Mac OS X, and
embedded Linux with no source code changes! The book teaches solid Qt programming
practices; it is not a rehash of the documentation. You'll find start-to-finish coverage packed
with examples, plus a CD with the Qt 3.2 toolset and Borland C++ compilers-including a noncommercial Qt 3.2 for Windows available nowhere else!
Build powerful C++ GUI applications quickly and easily

Design dialogs and main windows visually and in code
Learn Qt's innovative typesafe signals and slots mechanism
Use layouts to create forms that automatically size and scale
Create custom signals, slots, events, and controls
Program the ''Qt way'' with techniques for Qt 3.2 that'll work with Qt 4
1
8


[ Team LiB ]

Foreword
Why Qt? Why do programmers like us choose Qt? Sure, there are the obvious answers: Qt's
single-source compatibility, its feature richness, its C++ performance, the availability of the
source code, its documentation, the high-quality technical support, and all the other items
mentioned in Trolltech's glossy marketing materials. This is all very well, but it misses the most
important point: Qt is successful because programmers like it.
How come programmers like one technology, but dislike another? Personally, I believe software
engineers enjoy technology that feels right, but dislike everything that doesn't. How else can
we
explain that
some
of the brightest programmers need help to program a VCR, or that most

Table
of Contents
engineers
seem
to
have

trouble operating the company's phone system? I for one am perfectly

Index
capable
of
memorizing
sequences
of random numbers and commands, but if these are required
C++ GUI Programming with Qt 3
to control my answering machine, I'd prefer not to have one. At Trolltech, our phone system
By
Jasmin
, Mark
forces
usBlanchette
to hold the
'*'Summerfield
key pressed down for two seconds before we are allowed to type in the
other person's extension number. If you forget to do this but start typing the extension
immediately, you have to dial the entire number again. Why '*'? Why not '#', or '1', or '5', or
Prentice Hall PTR
anyPublisher:
of the other
twenty keys on the phone? Why two seconds and not one, or three, or one
Date: Why
January
15, 2004 at all? I find the phone so irritating that I avoid using it whenever I
andPub
a half?
anything

ISBN: 0-13-124072-2
can. Nobody
likes having to do random things, especially when those random things apparently
depend
Pages:
on some
464
equally random context you wish you didn't have to know about in the first
place.
Programming can be a lot like using our phone system, only worse. And this is where Qt comes
to
the rescue.
is different.
ForI one
makes
sense.
forbook
another,
Qt is fun.
"...not
only theQt
best
book on Qt
havething,
ever Qt
seen,
but also
theAnd
best
presenting

any Qt lets
you
concentrate
on
your
tasks.
When
Qt's
original
architects
faced
a
problem,
they
didn't
just
programming framework. Every sentence appears to be carefully worded, and every chapter
look
for
a
good
solution,
or
a
quick
solution,
or
the
simplest
solution.

They
looked
for
the
right
has a sound concept, and so does the work as a whole."
solution,
thenTrolltech's
they documented
it. Granted
they of
made
mistakes,
and granted some of
-Matthiasand
Ettrich,
lead developer,
founder
the KDE
project
their design decisions didn't pass the test of time, but they still got a lot of things right, and
what 'Tao
wasn't
and
can beiscorrected.
can see
by the
fact that
a system
"The

of right
Qt'....could
The Qt
system
a beautifulYou
example
ofthis
object
oriented
design,
and the
originallytake
designed
to bridge
Windows
and Unix/Motif
unifies modern
desktop systems
authors
advantage
of this....
The 95
authors
have donenow
an excellent
job of presenting
the
as
diverse
as interesting

Windows XP,
Mac
OS X, and
GNU/Linux with KDE.
subject
in an
and
engaging
way...."
-Ron McCarty, Instructor and Chair of the Department of Computer Science, Penn State Erie,
Long
before Qt
became so popular and so widely used, the dedication of Qt's developers to
The Behrend
College
finding the right solutions made Qt special. That dedication is just as strong today and affects
everyone
who maintains
develops
Qt.programming!
For us, working on Qt is a responsibility and a
The
first official
Trolltech and
guide
to Qt 3.2
privilege. We are proud of helping to make your professional and open source lives easier and
more
enjoyable.
Straight

from Trolltech, this book covers all you need to build industrial-strength applications
with Qt 3.2.x and C++-applications that run natively on Windows, Linux/Unix, Mac OS X, and
One
of the Linux
thingswith
thatno
makes
Qt code
a pleasure
to use
its online
documentation.
But the
embedded
source
changes!
Theisbook
teaches
solid Qt programming
documentation's
is primarily
on individual classes,
little said about
how to build
practices; it is notfocus
a rehash
of the documentation.
You'll with
find start-to-finish
coverage

packed
sophisticated
real-world
excellent
book
fills that
It shows you what
Qt
with examples,
plus a CDapplications.
with the Qt This
3.2 toolset
and
Borland
C++gap.
compilers-including
a nonhas to offer,Qt
how
program
Qtavailable
the "Qt way",
and else!
how to get the best from Qt. The book will
commercial
3.2tofor
Windows
nowhere
teach a C++ programmer how to program Qt, and provides enough advanced material to
satisfy experienced Qt programmers. The book is packed with good examples, advice, and
explanations,

Build powerful
and will
C++
be the
GUItext
applications
that we use
quickly
to induct
and easily
all new programmers who join
Trolltech.
Design dialogs and main windows visually and in code
Nowadays, there are a vast number of commercial and free Qt applications available for
Learn
innovative
typesafe
signals and
mechanism
purchase
orQt's
download.
Some
are specialized
for slots
particular
vertical markets, while others are
aimed at the mass-market. Seeing so many applications built with Qt fills us with pride and
Useuslayouts
to Qt

create
that
automatically
size
and book,
scale there will be more and
inspires
to make
evenforms
better.
And
with the help
of this
higher quality Qt applications than ever before.
Create custom signals, slots, events, and controls
Matthias Ettrich
Oslo, Program
Norway the ''Qt way'' with techniques for Qt 3.2 that'll work with Qt 4
1
9


November
[ Team LiB2003
]
[ Team LiB ]



Table of Contents




Index

C++ GUI Programming with Qt 3
ByJasmin Blanchette , Mark Summerfield

Publisher: Prentice Hall PTR
Pub Date: January 15, 2004
ISBN: 0-13-124072-2
Pages: 464

"...not only the best book on Qt I have ever seen, but also the best book presenting any
programming framework. Every sentence appears to be carefully worded, and every chapter
has a sound concept, and so does the work as a whole."
-Matthias Ettrich, Trolltech's lead developer, founder of the KDE project
"The 'Tao of Qt'.... The Qt system is a beautiful example of object oriented design, and the
authors take advantage of this.... The authors have done an excellent job of presenting the
subject in an interesting and engaging way...."
-Ron McCarty, Instructor and Chair of the Department of Computer Science, Penn State Erie,
The Behrend College
The first official Trolltech guide to Qt 3.2 programming!
Straight from Trolltech, this book covers all you need to build industrial-strength applications
with Qt 3.2.x and C++-applications that run natively on Windows, Linux/Unix, Mac OS X, and
embedded Linux with no source code changes! The book teaches solid Qt programming
practices; it is not a rehash of the documentation. You'll find start-to-finish coverage packed
with examples, plus a CD with the Qt 3.2 toolset and Borland C++ compilers-including a noncommercial Qt 3.2 for Windows available nowhere else!
Build powerful C++ GUI applications quickly and easily
Design dialogs and main windows visually and in code

Learn Qt's innovative typesafe signals and slots mechanism
Use layouts to create forms that automatically size and scale
Create custom signals, slots, events, and controls
Program the ''Qt way'' with techniques for Qt 3.2 that'll work with Qt 4
10
1


[ Team LiB ]

Preface
The Qt toolkit is a C++ class library and a set of tools for building multiplatform GUI programs
using a "write once, compile anywhere" approach. Qt lets programmers use a single source
tree for applications that will run on Windows 95 to XP, Mac OS X, Linux, Solaris, HP-UX, and
many other versions of Unix with X11. A version of Qt is also available for Embedded Linux,
with the same API.
The purpose of this book is to teach you how to write GUI programs using Qt 3. The book
starts with "Hello Qt" and quickly moves on to more advanced topics, such as creating custom
widgets
and Table
providing
drag and drop. The text is complemented by a CD that contains the

of Contents
source
code
of
the
example
programs. The CD also provides Qt and Borland C++ for Windows,


Index
Qt
for
Unix,
and
Qt
for
Mac
OS
X. Appendix A explains how to install the software.
C++ GUI Programming with Qt 3
By
Jasmin
Mark
Summerfield
The
bookBlanchette
focuses ,on
explaining
good idiomatic Qt 3 programming techniques rather than
simply rehashing or summarizing Qt's extensive online documentation. And because we are
involved in the development of Qt 4, we have tried to ensure that most of what we teach here
Prentice
Hallsensible
PTR
will Publisher:
still be valid
and
for Qt 4.

Pub Date: January 15, 2004

It is assumed
that you have a basic knowledge of C++. The code examples use a subset of
ISBN: 0-13-124072-2
C++, avoiding
Pages: 464 many C++ features that are rarely needed when programming Qt. In the few
places where a more advanced C++ construct is unavoidable, it is explained as it is used.
Qt made its reputation as a multiplatform toolkit, but because of its intuitive and powerful API,
many organizations use Qt for single-platform development. Adobe Photoshop Album is just
"...not
only the
book on Qt Windows
I have ever
seen, butwritten
also the
bestMany
booksophisticated
presenting any
one example
of best
a mass-market
application
in Qt.
software
programming
framework.
Every
sentence
appears

to
be
carefully
worded,
and every
chapter
systems in vertical markets, such as 3D animation tools, digital film processing,
electronic
has
a sound
concept,
sodesign),
does theoilwork
whole."
design
automation
(forand
chip
and as
gasa exploration,
financial services, and medical
-Matthias
Ettrich,
Trolltech's
lead
developer,
founder
of the
projectWindows product written
imaging, are built with Qt. If you are making a living with

a KDE
successful
in Qt, you can easily create new markets in the Mac OS X and Linux worlds simply by
"The 'Tao of Qt'.... The Qt system is a beautiful example of object oriented design, and the
recompiling.
authors take advantage of this.... The authors have done an excellent job of presenting the
subject
in an interesting
and licenses.
engagingIfway...."
Qt is available
under various
you want to build commercial applications, you must
-Ron
McCarty,
Instructor
and
Chair
of to
thebuild
Department
of Computer
Science,
buy a commercial license; if you
want
open source
programs,
you canPenn
use State
a non-Erie,

The
Behrend
College
commercial Qt edition. (The editions of Qt on the CD are non-commercial.) Qt is the foundation
on which the K Desktop Environment (KDE) and the many open source applications that go
The first official Trolltech guide to Qt 3.2 programming!
with it are built.
Straight from Trolltech, this book covers all you need to build industrial-strength applications
In addition to Qt's hundreds of classes, there are add-ons that extend Qt's scope and power.
with Qt 3.2.x and C++-applications that run natively on Windows, Linux/Unix, Mac OS X, and
Some of these products, like the Qt/Motif integration module and Qt Script for Applications
embedded Linux with no source code changes! The book teaches solid Qt programming
(QSA), are supplied by Trolltech, while others are provided by companies and by the open
practices; it is not a rehash of the documentation. You'll find start-to-finish coverage packed
source community. See for information on Qt
with examples, plus a CD with the Qt 3.2 toolset and Borland C++ compilers-including a nonadd-ons. Qt also has a well-established and thriving user community that uses the qtcommercial Qt 3.2 for Windows available nowhere else!
interest mailing list; see for details.
The book
divided into
Part I covers
theeasily
concepts and practices necessary for
Buildispowerful
C++two
GUIparts.
applications
quicklyalland
programming GUI applications using Qt. Knowledge of this part alone is sufficient to write
usefulDesign
GUI applications.

covers central
topics
in more depth and provides more
dialogs and Part
mainIIwindows
visuallyQtand
in code
specialized and advanced material. The chapters of Part II can be read in any order, but they
assume
familiarity
with the typesafe
contents signals
of Part and
I. slots mechanism
Learn
Qt's innovative
If youUse
spot
errors to
in create
the book,
have
suggestions
for the
or want to give us
layouts
forms
that
automatically
sizenext

and edition,
scale
feedback, we would be delighted to hear from you. You can reach us at

Create custom signals, slots,and
events,

and controls
The errata will be
placed on />Program the ''Qt way'' with techniques for Qt 3.2 that'll work with Qt 4
11
1


[ Team LiB ]



Table of Contents



Index

C++ GUI Programming with Qt 3
ByJasmin Blanchette , Mark Summerfield

Publisher: Prentice Hall PTR
Pub Date: January 15, 2004
ISBN: 0-13-124072-2

Pages: 464

"...not only the best book on Qt I have ever seen, but also the best book presenting any
programming framework. Every sentence appears to be carefully worded, and every chapter
has a sound concept, and so does the work as a whole."
-Matthias Ettrich, Trolltech's lead developer, founder of the KDE project
"The 'Tao of Qt'.... The Qt system is a beautiful example of object oriented design, and the
authors take advantage of this.... The authors have done an excellent job of presenting the
subject in an interesting and engaging way...."
-Ron McCarty, Instructor and Chair of the Department of Computer Science, Penn State Erie,
The Behrend College
The first official Trolltech guide to Qt 3.2 programming!
Straight from Trolltech, this book covers all you need to build industrial-strength applications
with Qt 3.2.x and C++-applications that run natively on Windows, Linux/Unix, Mac OS X, and
embedded Linux with no source code changes! The book teaches solid Qt programming
practices; it is not a rehash of the documentation. You'll find start-to-finish coverage packed
with examples, plus a CD with the Qt 3.2 toolset and Borland C++ compilers-including a noncommercial Qt 3.2 for Windows available nowhere else!
Build powerful C++ GUI applications quickly and easily
Design dialogs and main windows visually and in code
Learn Qt's innovative typesafe signals and slots mechanism
Use layouts to create forms that automatically size and scale
Create custom signals, slots, events, and controls
Program the ''Qt way'' with techniques for Qt 3.2 that'll work with Qt 4
12
1


[ Team LiB ]

Acknowledgments

Our first acknowledgment goes to Eirik Chambe-Eng, Trolltech's president. Eirik not only
enthusiastically encouraged us to write the book, he also allowed us to spend a considerable
amount of our work time writing it. Eirik and Trolltech CEO Haavard Nord both read the
manuscript and provided valuable feedback. Their generosity and foresight was aided and
abetted by Matthias Ettrich, Trolltech's lead developer and our boss. Matthias cheerfully
accepted our neglect of duty as we obsessed over the writing of this book and gave us a lot of
advice on good Qt programming style.
We
asked two
Qt of
customers,

Table
Contents Paul Curtis and Klaus Schmidinger, to be our external reviewers.
Both
are
Qt
experts
with an amazing attention to technical detail, which they proved by

Index
spotting
some
very
subtle
errors in our manuscript and suggesting numerous improvements.
C++ GUI Programming with Qt 3
By
Jasmin
Blanchettealongside

, Mark Summerfield
Within
Trolltech,
Matthias, our most stalwart reviewer was Reginald Stadlbauer.[*]
His technical insight was invaluable, and he taught us how to do some things in Qt that we
didn't even know were possible.
Publisher: Prentice Hall PTR
[*] Reginald has now moved to Germany, where he co-founded froglogic, a software consultancy.
Pub
Date: January 15, 2004

ISBN: 0-13-124072-2

Our other key reviewers within Trolltech were Trenton Schulz, Andy Shaw, and Andreas Aardal
Pages: 464
Hanssen. Trenton and Andy gave feedback on all aspects of the book and were especially
helpful regarding Qt/Mac and Qt/Windows. Andreas gave us invaluable help refining Part I.
In addition to the reviewers mentioned above, we received expert help from Warwick Allison
"...not
only theEirik
best Chambe-Eng
book on Qt I (Qt's
have history),
ever seen,
but alsoEttrich
the best
book
presenting
anycustom
(2D graphics),

Matthias
(event
processing
and
programming
framework.
sentenceVolker
appears
to be carefully
worded,
andHughes
every chapter
widgets),
Harald
FernengelEvery
(databases),
Hilsheimer
(ActiveX),
Bradley
has a sound concept,
and
so does the
as a and
whole."
(multithreading),
Trond
Kjernåsen
(3Dwork
graphics
databases), Lars Knoll (2D graphics), Sam

-Matthias Ettrich,
Magnuson
(qmake),
Trolltech's
Dimitri Papadopoulos
lead developer,
(Qt/X11),
founderPaul
of the
Olav
KDE
Tvete
project
(custom widgets and
Qt/Embedded), Rainer Schmid (networking and XML), and Gunnar Sletta (event processing).
"The 'Tao of Qt'.... The Qt system is a beautiful example of object oriented design, and the
authors
take advantage
of this.... The
authors
have
an to
excellent
job
of presenting
the
Extra
thanks
are due to Trolltech's
support

team
for done
helping
keep our
support
load under
subject while
in an the
interesting
and engaging
way...."
control
book consumed
so much
of our time, and to Trolltech's system administrators
-Ron
McCarty,
Chairand
of the
of Computer Science,
Penn
Erie,
for
keeping
ourInstructor
machines and
running
our Department
networks communicating
throughout

theState
project.
The Behrend College
We are also grateful to Troy Kitch from Borland for giving us permission to include Borland
The
official on
Trolltech
guide to Qt 3.2
C++first
compilers
the accompanying
CD,programming!
and to the SQLite developers for putting their
database into the public domain.
Straight from Trolltech, this book covers all you need to build industrial-strength applications
withthe
Qt production
3.2.x and C++-applications
thatled
run
natively
on Windows,
Linux/Unix,
Mac OS
and
On
side, Rainer Schmid
the
team that
created the

accompanying
CD,X,ably
embedded by
Linux
withFernengel
no sourceand
code
changes!
book teaches
solid
Qt handled
programming
supported
Harald
Andy
Shaw.The
Trolltech's
Cathrine
Bore
the contracts
practices;
it ison
not
a rehash
the
documentation.
You'll
find start-to-finish
coverage
and legalities

our
behalf. of
Jeff
Kingston,
author of
the Lout
typesetting tool,
gave uspacked
advice
with
examples,
plus
a
CD
with
the
Qt
3.2
toolset
and
Borland
C++
compilers-including
nonand enhanced the tool in the light of our feedback. Jill Harry of Prentice Hall had faith ina the
commercial
3.2
for and
Windows
available
else! matters were smoothly handled,

project
fromQt
the
start
ensured
that allnowhere
the practical
leaving us free to concentrate on the writing. And Lisa Iarkowski turned our camera-ready
manuscript into the beautiful volume you now hold in your hands.
Build powerful C++ GUI applications quickly and easily
[ Team LiB ]
Design dialogs and main windows visually and in code
Learn Qt's innovative typesafe signals and slots mechanism
Use layouts to create forms that automatically size and scale
Create custom signals, slots, events, and controls
Program the ''Qt way'' with techniques for Qt 3.2 that'll work with Qt 4
13
1


[ Team LiB ]

A Brief History of Qt
The Qt toolkit first became publicly available in May 1995. It was initially developed by Haavard
Nord (Trolltech's CEO) and Eirik Chambe-Eng (Trolltech's president). Haavard and Eirik met
each other at the Norwegian Institute of Technology in Trondheim, Norway, where they both
graduated with master's degrees in computer science.
Haavard's interest in C++ GUI development began in 1988 when he was commissioned by a
Swedish company to design and implement a C++ GUI toolkit. A couple of years later, in the
summer of 1990, Haavard and Eirik were working together on a C++ database application for

ultrasound
images.
system needed to be able to run with a GUI on Unix, Macintosh, and

Table of The
Contents
Windows.
One
day
that
summer,
Haavard and Eirik went outside to enjoy the sunshine, and as

Index
they
sat
on
a
park
bench,
Haavard
said, "We need an object-oriented display system." The
C++ GUI Programming with Qt 3
resulting discussion laid the intellectual foundation for the object-oriented multiplatform GUI
By
Jasmin
Blanchette
toolkit
they
would,Mark

soonSummerfield
go on to build.
In 1991, Haavard started writing the classes that eventually became Qt, collaborating with Eirik
Publisher:
Prentice
PTR
on the
design.
TheHall
following
year, Eirik came up the ideafor "signals and slots", a simple but
Pub Date:
January
15, 2004
powerful
GUI
programming
paradigm. Haavard took the idea and produced a hand-coded
implementation.
ISBN: 0-13-124072-2
By 1993, Haavard and Eirik had developed Qt's first graphics kernel and were
able toPages:
implement
their own widgets. At the end of the year, Haavard suggested that they go
464
into business together to build "the world's best C++ GUI toolkit".
The year 1994 began inauspiciously with the two young programmers wanting to enter a well
established market, with no customers, an unfinished product, and no money. Fortunately,
"...not only the best book on Qt I have ever seen, but also the best book presenting any
both their wives had work and were willing to support their husbands for the two years Eirik

programming framework. Every sentence appears to be carefully worded, and every chapter
and Haavard expected to need to develop the product and start earning an income.
has a sound concept, and so does the work as a whole."
-Matthias
Ettrich,
lead developer,
founder
of the KDE
project
They chose
'Q' as Trolltech's
the class prefix
because the
letter looked
beautiful
in Haavard's Emacs font.
The 't' was added to stand for "toolkit", inspired by "Xt", the X Toolkit. The company was
"The 'Tao of Qt'.... The Qt system is a beautiful example of object oriented design, and the
incorporated on 4 March 1994, originally as "Quasar Technologies", then as "Troll Tech", and
authors take advantage of this.... The authors have done an excellent job of presenting the
today as "Trolltech".
subject in an interesting and engaging way...."
-Ron
McCarty,
Instructor
Chairmade
of the
Department
ComputerUniversity
Science, Penn

State Erie,
In April
1995, thanks
to aand
contact
through
one ofofHaavard's
professors,
the
The
Behrend
College
Norwegian company Metis gave them a contract to develop software based on Qt. Around this
time, Trolltech hired Arnt Gulbrandsen,[*] who devised and implemented an ingenious
The first official Trolltech guide to Qt 3.2 programming!
documentation system as well ascontributing to Qt's code.
Straight
from
Trolltech, this book covers all you need to build industrial-strength applications
[*] Arnt
left the company a few years ago to pursue his career in Germany.
with Qt 3.2.x and C++-applications that run natively on Windows, Linux/Unix, Mac OS X, and
embedded
no source
code changes!
The book teaches
programming
On
20 May Linux
1995,with

Qt 0.90
was uploaded
to sunsite.unc.edu.
Six solid
daysQt
later,
the release was
practices;
is comp.os.linux.announce.
not a rehash of the documentation.
find public
start-to-finish
packed
announcediton
This wasYou'll
Qt's first
release. coverage
Qt could be
used
with
examples,
plus
a
CD
with
the
Qt
3.2
toolset
and

Borland
C++
compilers-including
a nonfor both Windows and Unix development, offering the same API on both platforms. Qt was
commercial
Qt 3.2
Windows
available
else! license was required for commercial
available under
twofor
licenses
fromday
one:nowhere
A commercial
development and a free software edition was available for open source development. The Metis
contract kept Trolltech afloat, while for ten long months no one bought a commercial Qt
Build powerful C++ GUI applications quickly and easily
license.
Design dialogs and main windows visually and in code
In March 1996, the European Space Agency became the second Qt customer, with a purchase
of ten commercial licenses. With unwavering faith, Eirik and Haavard hired another developer.
Learn Qt's innovative typesafe signals and slots mechanism
Qt 0.97 was released at the end of May, and on 24 September 1996, Qt 1.0 came out. By the
end ofUse
thelayouts
year, Qt
reached
1.1; eight customers,
each in a different country, had

tohad
create
formsversion
that automatically
size and scale
bought 18 licenses between them. This year also saw the founding of the KDE project, led by
Matthias
Ettrich.
Create
custom signals, slots, events, and controls
Qt 1.2Program
was released
in way''
April 1997.
Matthias Ettrich's
decision
usewith
Qt to
KDE helped Qt
the ''Qt
with techniques
for Qt 3.2
that'll to
work
Qtbuild
4
14
1



become
the]de-facto standard for C++ GUI development on Linux. Qt 1.3 was released in
[ Team LiB
September 1997.
Matthias joined Trolltech in 1998, and the last major Qt 1 release, 1.40, was made in
September of that year. Qt 2.0 was released in June 1999. Qt 2 hadmany major architectural
changes and was a much stronger and more mature product than its predecessor. It also
featured forty new classes and Unicode support. Qt 2 had a new open source license, the Q
Public License (QPL), which complied to the Open Source Definition. In August 1999, Qt won
the LinuxWorld award for best library/tool. Around this time, Trolltech Pty Ltd (Australia) was
established.
Trolltech released Qt/Embedded in 2000. It was designed to run on Embedded Linux devices
and provided is own window system as a lightweight replacement for X11. Both Qt/Embedded
and Qt/X11 were now offered under the widely used GNU General Public License (GPL) as well
as under commercial licenses. By the end of 2000, Trolltech had established Trolltech Inc.
(USA) and had released the first version of Qtopia, an environment for handheld devices.

Table of Contents
Qt/Embedded
won the LinuxWorld "Best Embedded Linux Solution"award in both 2001 and

Index
2002.
C++ GUI Programming with Qt 3

Qt
3.0 was
released
in Summerfield
2001. Qt was now available on Windows, Unix, Linux, Embedded Linux,

ByJasmin
Blanchette
, Mark
and Mac OS X. Qt 3.0 provided 42 new classes and the code surpassed 500,000 lines. Qt 3.0
won the Software Development Times "Jolt Productivity Award" in 2002.
Publisher: Prentice Hall PTR

Trolltech's
have
doubled year on year since the company's birth. This success is a
Pub Date:sales
January
15, 2004
reflection both of the quality of Qt and of how enjoyable it is to use. For most of the company's
ISBN: 0-13-124072-2
existence, sales and marketing were handled by just a couple of people. Yet, in less than a
Pages: 464
decade, Qt has gone from being a "secret" product, known only to a select group of
professionals, to having thousands of customers and tens of thousands of open source
developers all around the world.
"...not
[ Teamonly
LiB ]the best book on Qt I have ever seen, but also the best book presenting any
programming framework. Every sentence appears to be carefully worded, and every chapter
has a sound concept, and so does the work as a whole."
-Matthias Ettrich, Trolltech's lead developer, founder of the KDE project
"The 'Tao of Qt'.... The Qt system is a beautiful example of object oriented design, and the
authors take advantage of this.... The authors have done an excellent job of presenting the
subject in an interesting and engaging way...."
-Ron McCarty, Instructor and Chair of the Department of Computer Science, Penn State Erie,

The Behrend College
The first official Trolltech guide to Qt 3.2 programming!
Straight from Trolltech, this book covers all you need to build industrial-strength applications
with Qt 3.2.x and C++-applications that run natively on Windows, Linux/Unix, Mac OS X, and
embedded Linux with no source code changes! The book teaches solid Qt programming
practices; it is not a rehash of the documentation. You'll find start-to-finish coverage packed
with examples, plus a CD with the Qt 3.2 toolset and Borland C++ compilers-including a noncommercial Qt 3.2 for Windows available nowhere else!
Build powerful C++ GUI applications quickly and easily
Design dialogs and main windows visually and in code
Learn Qt's innovative typesafe signals and slots mechanism
Use layouts to create forms that automatically size and scale
Create custom signals, slots, events, and controls
Program the ''Qt way'' with techniques for Qt 3.2 that'll work with Qt 4
15
1


[ Team LiB ]

About the Authors
Jasmin Blanchette
Jasmin graduated in computer science in 2001 from the University of Sherbrooke, Quebec, and
was awarded the Fernand Seguin medal of excellence. He did a work term at Trolltech in the
summer of 2000 as a software engineer and has been working there continuously since early
2001. Now a senior software engineer, he is the driving force behind the Qt Linguist translation
tool and provides Qt Quarterly, Trolltech's technical newsletter, with much of its content. In his
spare time, he is writing a novel in Norwegian and Swedish. He lives in Oslo with his girlfriend
Anne-Lene.

Table of Contents



Index

Mark Summerfield

C++ GUI Programming with Qt 3
By
Jasmin
Blanchette
Summerfield
Mark
graduated
in,Mark
computer
science in 1993 from the University of Wales Swansea. He
followed this with a year's postgraduate research before going into industry. He spent many
years working as a software engineer for a variety of firms before joining Trolltech. For the
Prentice
pastPublisher:
few years,
he Hall
hasPTR
been Trolltech's documentation manager, responsible for maintaining
overPub
1500
Date:pages
Januaryof
15,
online

2004 Qt documentation and for editing Qt Quarterly. In his free time, he
writes open
source software. He lives in the Swansea Valley in South Wales, UK, with his wife
ISBN: 0-13-124072-2
Andrea.
Pages: 464

[ Team LiB ]
"...not only the best book on Qt I have ever seen, but also the best book presenting any
programming framework. Every sentence appears to be carefully worded, and every chapter
has a sound concept, and so does the work as a whole."
-Matthias Ettrich, Trolltech's lead developer, founder of the KDE project
"The 'Tao of Qt'.... The Qt system is a beautiful example of object oriented design, and the
authors take advantage of this.... The authors have done an excellent job of presenting the
subject in an interesting and engaging way...."
-Ron McCarty, Instructor and Chair of the Department of Computer Science, Penn State Erie,
The Behrend College
The first official Trolltech guide to Qt 3.2 programming!
Straight from Trolltech, this book covers all you need to build industrial-strength applications
with Qt 3.2.x and C++-applications that run natively on Windows, Linux/Unix, Mac OS X, and
embedded Linux with no source code changes! The book teaches solid Qt programming
practices; it is not a rehash of the documentation. You'll find start-to-finish coverage packed
with examples, plus a CD with the Qt 3.2 toolset and Borland C++ compilers-including a noncommercial Qt 3.2 for Windows available nowhere else!
Build powerful C++ GUI applications quickly and easily
Design dialogs and main windows visually and in code
Learn Qt's innovative typesafe signals and slots mechanism
Use layouts to create forms that automatically size and scale
Create custom signals, slots, events, and controls
Program the ''Qt way'' with techniques for Qt 3.2 that'll work with Qt 4
16

1


[ Team LiB ]

Production
The authors wrote the text using NEdit and Vim. They typeset and indexed the text
themselves, marking it up with a modified Lout syntax that they converted to pure Lout using a
custom preprocessor written in Python. They produced all the diagrams in Lout and used
ImageMagick to convert screenshots to PostScript. The monospaced font used for code is
derived from Courier and was created using PfaEdit. The cover was provided by the publisher;
the photograph is of the fall of the Berlin Wall, November 1989. The marked-up text was
converted to PostScript by Lout, then to PDF by Ghostscript. The authors did all the editing and
processing on Debian GNU/Linux systems under KDE. The example programs were tested on
Windows, Linux,
Mac OS X.

Tableand
of Contents


Index

[C++
Team
LiB ]
GUI Programming with Qt 3
ByJasmin Blanchette , Mark Summerfield

Publisher: Prentice Hall PTR

Pub Date: January 15, 2004
ISBN: 0-13-124072-2
Pages: 464

"...not only the best book on Qt I have ever seen, but also the best book presenting any
programming framework. Every sentence appears to be carefully worded, and every chapter
has a sound concept, and so does the work as a whole."
-Matthias Ettrich, Trolltech's lead developer, founder of the KDE project
"The 'Tao of Qt'.... The Qt system is a beautiful example of object oriented design, and the
authors take advantage of this.... The authors have done an excellent job of presenting the
subject in an interesting and engaging way...."
-Ron McCarty, Instructor and Chair of the Department of Computer Science, Penn State Erie,
The Behrend College
The first official Trolltech guide to Qt 3.2 programming!
Straight from Trolltech, this book covers all you need to build industrial-strength applications
with Qt 3.2.x and C++-applications that run natively on Windows, Linux/Unix, Mac OS X, and
embedded Linux with no source code changes! The book teaches solid Qt programming
practices; it is not a rehash of the documentation. You'll find start-to-finish coverage packed
with examples, plus a CD with the Qt 3.2 toolset and Borland C++ compilers-including a noncommercial Qt 3.2 for Windows available nowhere else!
Build powerful C++ GUI applications quickly and easily
Design dialogs and main windows visually and in code
Learn Qt's innovative typesafe signals and slots mechanism
Use layouts to create forms that automatically size and scale
Create custom signals, slots, events, and controls
Program the ''Qt way'' with techniques for Qt 3.2 that'll work with Qt 4
17
1


[ Team LiB ]


About the CD-ROM
The CD-ROM included with C++ GUI Programming with Qt 3 contains all the software and
source code needed to create and run applications on Windows, Mac OS X, and Unix/Linux with
X11. The CD-ROM includes the following:
Qt 3.2.1 Non-Commercial Edition for Windows
Qt 3.2.1 Free Edition for Mac OS X



Table
of Edition
Contentsfor Unix/Linux with X11
Qt 3.2.1
Free
Index

C++ GUI
Programming
with Qt 5
3
Borland
C++ Builder

Non-Commercial Edition

ByJasmin Blanchette , Mark Summerfield

Borland C++ Builder 6 Trial Edition
Source code for the book's examples


Publisher: Prentice Hall PTR

Pub Date: January
15, 2004
All versions
of Qt come
with the Qt library and a set of tools including the qmake build tool, Qt
ISBN:
0-13-124072-2
Designer for visual dialog design, Qt Linguist for internationalization support, and Qt Assistant
Pages: 464documentation.
for presenting

The CD-ROM can be used on Microsoft Windows 95, 98, NT 4, ME, 2000, XP, Mac OS X, Linux,
and most versions of Unix.
"...not only the best book on Qt I have ever seen, but also the best book presenting any
License
Agreement
programming
framework. Every sentence appears to be carefully worded, and every chapter
has a sound concept, and so does the work as a whole."
Each of the software packages on the CD-ROM has its own license agreement. The full legal
-Matthias Ettrich, Trolltech's lead developer, founder of the KDE project
texts of the licenses are included with the packages on the CD-ROM.
"The 'Tao of Qt'.... The Qt system is a beautiful example of object oriented design, and the
Technical Support
authors take advantage of this.... The authors have done an excellent job of presenting the
subject
an interesting

engaging
way...."
Neither in
Prentice
Hall nor and
Trolltech
offers
any technical support for any of the software on the
-Ron
McCarty,
Instructor
and
Chair
of
the
Department
ofavailable
Computer
Science,
Pennfully
State Erie,
CD-ROM. (Fully supported commercial editions
of Qt are
from
Trolltech;
The
Behrend
College
supported commercial editions of Borland C++ Builder are available from Inprise.) If the CDROM is damaged, you can obtain a replacement copy by sending an email that describes the
The first official Trolltech guide to Qt 3.2 programming!

problem to
Straight from Trolltech, this book covers all you need to build industrial-strength applications
with Qt 3.2.x and C++-applications that run natively on Windows, Linux/Unix, Mac OS X, and
[ Team LiB ]
embedded Linux with no source code changes! The book teaches solid Qt programming
practices; it is not a rehash of the documentation. You'll find start-to-finish coverage packed
with examples, plus a CD with the Qt 3.2 toolset and Borland C++ compilers-including a noncommercial Qt 3.2 for Windows available nowhere else!
Build powerful C++ GUI applications quickly and easily
Design dialogs and main windows visually and in code
Learn Qt's innovative typesafe signals and slots mechanism
Use layouts to create forms that automatically size and scale
Create custom signals, slots, events, and controls
Program the ''Qt way'' with techniques for Qt 3.2 that'll work with Qt 4
18
1


[ Team LiB ]

Part I: Basic Qt
Chapter 1. Getting Started
Chapter 2. Creating Dialogs
Chapter 3. Creating Main Windows
Chapter 4. Implementing Application Functionality


of Contents
ChapterTable
5. Creating
Custom Widgets



[ Team LiB ]Index
C++ GUI Programming with Qt 3
ByJasmin Blanchette , Mark Summerfield

Publisher: Prentice Hall PTR
Pub Date: January 15, 2004
ISBN: 0-13-124072-2
Pages: 464

"...not only the best book on Qt I have ever seen, but also the best book presenting any
programming framework. Every sentence appears to be carefully worded, and every chapter
has a sound concept, and so does the work as a whole."
-Matthias Ettrich, Trolltech's lead developer, founder of the KDE project
"The 'Tao of Qt'.... The Qt system is a beautiful example of object oriented design, and the
authors take advantage of this.... The authors have done an excellent job of presenting the
subject in an interesting and engaging way...."
-Ron McCarty, Instructor and Chair of the Department of Computer Science, Penn State Erie,
The Behrend College
The first official Trolltech guide to Qt 3.2 programming!
Straight from Trolltech, this book covers all you need to build industrial-strength applications
with Qt 3.2.x and C++-applications that run natively on Windows, Linux/Unix, Mac OS X, and
embedded Linux with no source code changes! The book teaches solid Qt programming
practices; it is not a rehash of the documentation. You'll find start-to-finish coverage packed
with examples, plus a CD with the Qt 3.2 toolset and Borland C++ compilers-including a noncommercial Qt 3.2 for Windows available nowhere else!
Build powerful C++ GUI applications quickly and easily
Design dialogs and main windows visually and in code
Learn Qt's innovative typesafe signals and slots mechanism
Use layouts to create forms that automatically size and scale

Create custom signals, slots, events, and controls
Program the ''Qt way'' with techniques for Qt 3.2 that'll work with Qt 4
19
1


[ Team LiB ]

Chapter 1. Getting Started
Hello Qt
Making Connections
Using the Reference Documentation
This chapter shows how to combine basic C++ with the functionality provided by Qt to create a
few small graphical user interface (GUI) applications. This chapter also introduces two key Qt

Table
of slots"
Contents
ideas: "signals
and
and layouts. In Chapter 2, we will go into more depth, and in Chapter

Index
3, we will start building a realistic application.
C++ GUI Programming with Qt 3

[ByTeam
]
JasminLiB
Blanchette

, Mark Summerfield

Publisher: Prentice Hall PTR
Pub Date: January 15, 2004
ISBN: 0-13-124072-2
Pages: 464

"...not only the best book on Qt I have ever seen, but also the best book presenting any
programming framework. Every sentence appears to be carefully worded, and every chapter
has a sound concept, and so does the work as a whole."
-Matthias Ettrich, Trolltech's lead developer, founder of the KDE project
"The 'Tao of Qt'.... The Qt system is a beautiful example of object oriented design, and the
authors take advantage of this.... The authors have done an excellent job of presenting the
subject in an interesting and engaging way...."
-Ron McCarty, Instructor and Chair of the Department of Computer Science, Penn State Erie,
The Behrend College
The first official Trolltech guide to Qt 3.2 programming!
Straight from Trolltech, this book covers all you need to build industrial-strength applications
with Qt 3.2.x and C++-applications that run natively on Windows, Linux/Unix, Mac OS X, and
embedded Linux with no source code changes! The book teaches solid Qt programming
practices; it is not a rehash of the documentation. You'll find start-to-finish coverage packed
with examples, plus a CD with the Qt 3.2 toolset and Borland C++ compilers-including a noncommercial Qt 3.2 for Windows available nowhere else!
Build powerful C++ GUI applications quickly and easily
Design dialogs and main windows visually and in code
Learn Qt's innovative typesafe signals and slots mechanism
Use layouts to create forms that automatically size and scale
Create custom signals, slots, events, and controls
Program the ''Qt way'' with techniques for Qt 3.2 that'll work with Qt 4
20
1



[ Team LiB ]

Hello Qt
Here's a very simple Qt program:

1 #include <qapplication.h>
2 #include <qlabel.h>
3 int main(int argc, char *argv[])
4 {
5
QApplication app (argc, argv);
•6
Table*label
of Contents
QLabel
= new QLabel("Hello Qt!", 0);
•7
Index
app.setMainWidget(label);
C++
with Qt 3
8 GUI Programming
label->show();
9
return
app.exec();
ByJasmin Blanchette , Mark Summerfield
10 }

Publisher: Prentice Hall PTR

We Pub
will Date:
first January
study 15,
it line
by line, then we will see how to compile and run it.
2004
Lines 1ISBN:
and 0-13-124072-2
2 include the definitions of the QApplication and QLabel classes.
Pages: 464

Line 5 creates a QApplication object to manage application-wide resources. The
QApplication constructor requires argc and argv because Qt supports a few command-line
arguments of its own.
"...not only the best book on Qt I have ever seen, but also the best book presenting any
Line
6 creates framework.
a QLabel widget
displays
"Helloto
Qt!".
In Qt terminology,
a widget
is a visual
programming
Everythat
sentence

appears
be carefully
worded, and
every chapter
element
in
a
user
interface.
Buttons,
menus,
scroll
bars,
and
frames
are
all
examples
of
has a sound concept, and so does the work as a whole."
widgets.
Widgets
can
contain
other
widgets;
for
example,
an
application

window
is
usually
a
-Matthias Ettrich, Trolltech's lead developer, founder of the KDE project
widget that contains a QMenuBar, a QToolBar, a QStatusBar, and some other widgets. The 0
argument
to Qt'....
the QLabel
null pointer)
means
that the
widget design,
is a window
in its
"The 'Tao of
The Qtconstructor
system is a(abeautiful
example
of object
oriented
and the
own
right,
notadvantage
a widget inside
another
window.have done an excellent job of presenting the
authors
take

of this....
The authors
subject in an interesting and engaging way...."
Line
makes the
label theand
application's
main
widget. When
the userScience,
closes the
main
widget
-Ron7McCarty,
Instructor
Chair of the
Department
of Computer
Penn
State
Erie,(by
clicking
X
in
the
window's
title
bar,
for
example),

the
program
terminates.
Without
a
main
The Behrend College
widget, the program would keep running in the background even after the user has closed the
window.
The first official Trolltech guide to Qt 3.2 programming!
Line
8 makes
label visible.
Widgets
are
created
hidden,
so that we canapplications
customize
Straight
from the
Trolltech,
this book
covers
allalways
you need
to build
industrial-strength
them
before

showing
them,
thereby
avoiding
flicker.
with Qt 3.2.x and C++-applications that run natively on Windows, Linux/Unix, Mac OS X, and
embedded Linux with no source code changes! The book teaches solid Qt programming
Line
9 passes
of the of
application
on to Qt. AtYou'll
this point,
the program coverage
enters a kind
of
practices;
it is control
not a rehash
the documentation.
find start-to-finish
packed
stand-by
mode,plus
where
it waits
for user
actions
such
mouseC++

clickscompilers-including
and key presses. a nonwith
examples,
a CD
with the
Qt 3.2
toolset
andas
Borland
commercial Qt 3.2 for Windows available nowhere else!
User actions generate events (also called "messages") to which the program can respond,
usually by executing one or more functions. In this respect, GUI applications differ drastically
from conventional
which typically
process
Build powerfulbatch
C++ programs,
GUI applications
quickly and
easilyinput, produce results, and
terminate without human intervention.
Design dialogs and main windows visually and in code

Figure 1.1. Hello on Windows XP

Learn Qt's innovative typesafe signals and slots mechanism

Use layouts to create forms that automatically size and scale
Create custom signals, slots, events, and controls
It is now

Program
time to
thetest
''Qtthe
way''
program
with techniques
on your machine.
for Qt 3.2
First,
that'll
youwork
will with
need Qt
to 4
install Qt 3.2 (or a
21
1


later
Qt LiB
3 release),
a process that is explained in Appendix A. From now on, we will assume
[ Team
]
that you have a correctly installed copy of Qt 3.2 and that Qt's bin directory is in your PATH
environment variable. (On Windows, this is done automatically by the Qt installation program,
so you don't need to worry about it.)
You will also need the Hello program's source code in a file called hello.cpp in a directory

calledhello. You can type in hello.cpp yourself, or copy it from the CD provided with this
book, where it is available as \examples\chap01\hello\hello.cpp.
From a command prompt, change directory to hello, then type

qmake -project

to create a platform-independent project file (hello.pro), then type


Table of Contents



Index

qmake
C++ GUIhello.pro
Programming with Qt 3
ByJasmin Blanchette , Mark Summerfield

to create a platform-specific makefile from the project file. Run make to build the program, and
run the program by typing hello on Windows, ./hello on Unix, and open hello.app on Mac
Publisher: Prentice Hall PTR
OS X. If you are using Microsoft Visual C++, you will need to run nmake instead of make.
Pub Date: January 15, 2004
Alternatively,
you can create a Visual Studio project file from hello.pro by typing
ISBN: 0-13-124072-2
Pages: 464


qmake -tp vc hello.pro

and
then
build
program
Visual
Studio.
"...not
only
thethe
best
book oninQt
I have
ever seen, but also the best book presenting any
programming framework. Every sentence appears to be carefully worded, and every chapter
has a sound concept,
Figure
and1.2.
so does
A label
the work
with
as a basic
whole." HTML formatting
-Matthias Ettrich, Trolltech's lead developer, founder of the KDE project
"The 'Tao of Qt'.... The Qt system is a beautiful example of object oriented design, and the
authors take advantage of this.... The authors have done an excellent job of presenting the
subject in an interesting and engaging way...."
-Ron McCarty, Instructor and Chair of the Department of Computer Science, Penn State Erie,

The Behrend College
Now let's have some fun: We will brighten up the label by using some simple HTML-style
The
first official
guide
Qt 3.2 programming!
formatting.
ThisTrolltech
can be done
bytoreplacing
the line
Straight from Trolltech, this book covers all you need to build industrial-strength applications
with Qt 3.2.x
and=C++-applications
that run
natively
QLabel
*label
new QLabel("Hello
Qt!",
0); on Windows, Linux/Unix, Mac OS X, and
embedded Linux with no source code changes! The book teaches solid Qt programming
practices; it is not a rehash of the documentation. You'll find start-to-finish coverage packed
with examples, plus a CD with the Qt 3.2 toolset and Borland C++ compilers-including a noncommercial Qt 3.2 for Windows available nowhere else!
QLabel *label = new QLabel("

<i>Hello</i> "
Build powerful C++ GUI applications
quickly and easily
"<font color=red>Qt!</font>

",
0);
Design dialogs and main windows visually and in code

and rebuilding the application.
Learn Qt's innovative typesafe signals and slots mechanism
[ Team LiB ]
Use layouts to create forms that automatically size and scale
Create custom signals, slots, events, and controls
Program the ''Qt way'' with techniques for Qt 3.2 that'll work with Qt 4
22
1


[ Team LiB ]

Making Connections
The next example illustrates how to respond to user actions. The application consists of a
button that the user can click to quit. The source code is very similar to Hello, except that we
are using a QPushButton instead of a QLabel as our main widget, and we are connecting a
user action (clicking a button) to a piece of code.
This application's source code is on the CD in the file \examples\chap01\quit\quit.cpp.

Figure 1.3. The Quit application


Table of Contents



Index

C++ GUI Programming with Qt 3
ByJasmin Blanchette , Mark Summerfield


1 Publisher:
#include
<qapplication.h>
Prentice
Hall PTR
2 #include
<qpushbutton.h>
Pub Date: January 15, 2004
ISBN: 0-13-124072-2

3 int main(int argc, char *argv[])
4 { Pages: 464
5
QApplication app(argc, argv);
6
QPushButton *button = new QPushButton("Quit", 0);
7
QObject::connect(button, SIGNAL(clicked()),
"...not
only the best book on &app,
Qt I have
ever seen, but also the best book presenting any
8
SLOT(quit()));
programming
framework.
Every
sentence
9

app.setMainWidget(button); appears to be carefully worded, and every chapter
has a sound
concept, and so does the work as a whole."
10
button->show();
-Matthiasreturn
Ettrich,app.exec();
Trolltech's lead developer, founder of the KDE project
11
12 }
"The 'Tao of Qt'.... The Qt system is a beautiful example of object oriented design, and the
authors take advantage of this.... The authors have done an excellent job of presenting the
subject
in an emit
interesting
engaging
way...."
Qt's widgets
signalsand
to indicate
that
a user action or a change of state has occurred. [*] For
-Ron McCarty,
Instructor
andaChair
of the signal
Department
of Computer
Science,
PennAState

instance,
QPushButton
emits
clicked()
when the
user clicks
the button.
signalErie,
can
The
Behrend
College
be connected to a function (called a slot in that context), so that when the signal is emitted,
the slot is automatically executed. In our example, we connect the button's clicked() signal
The
first
official Trolltech
guidequit()
to Qt 3.2
programming!
to the
QApplication
object's
slot.
The SIGNAL() and SLOT() macros are part of the
syntax; they are explained in more detail in the next chapter.
Straight from Trolltech, this book covers all you need to build industrial-strength applications
with Qt
[*] Qt
3.2.x

andare
C++-applications
that run
Linux/Unix,
Mac
OS X, and
signals
unrelated to Unix signals.
In natively
this book,on
we Windows,
are only concerned
with Qt
signals.
embedded Linux with no source code changes! The book teaches solid Qt programming
We
will now
build
application.
assume that You'll
you have
a directory
calledpacked
quit
practices;
it is
notthe
a rehash
of the We
documentation.

find created
start-to-finish
coverage
containing
quit.cpp.
qmake
in Qt
the3.2
quit
directory
to generate
project file, then arun
it
with examples,
plus aRun
CD with
the
toolset
and Borland
C++the
compilers-including
noncommercial
again
to generate
Qt 3.2afor
makefile:
Windows available nowhere else!
powerful C++ GUI applications quickly and easily
qmakeBuild
-project

qmake quit.pro
Design dialogs and main windows visually and in code
Learnthe
Qt's
innovativeand
typesafe
Now build
application,
run it.signals
If you and
clickslots
Quit,mechanism
or press Space (which presses the
button), the application will terminate.
Use layouts to create forms that automatically size and scale
The next example demonstrates how to use signals and slots to synchronize two widgets. The
Createasks
custom
signals,
slots,
anduser
controls
application
for the
user's
age,events,
which the
can enter by manipulating either a spin box
or a slider.
Program the ''Qt way'' with techniques for Qt 3.2 that'll work with Qt 4

23
1


Figure 1.4. The Age application

[ Team LiB ]

The application consists of three widgets: a QSpinBox, a QSlider, and a QHBox (horizontal
layout box). The QHBox is the application's main widget. The QSpinBox and the QSlider are
rendered inside the QHBox; they are children of the QHBox.

Figure 1.5. The Age application's widgets



Table of Contents



Index

C++ GUI Programming with Qt 3
ByJasmin Blanchette , Mark Summerfield

1
2
3
4


#include <qapplication.h>
#include
<qhbox.h>
Publisher:
Prentice
Hall PTR
#include
<qslider.h>
Pub Date: January
15, 2004
#include
<qspinbox.h>
ISBN: 0-13-124072-2
Pages: 464

5 int main(int argc, char *argv[])
6 {
7
QApplication app(argc, argv);
"...not
only
the *hbox
best book
on Qt
I have ever seen, but also the best book presenting any
8
QHBox
= new
QHBox(0);
programming

framework.
Every
sentence
9
hbox->setCaption("Enter
Yourappears
Age"); to be carefully worded, and every chapter
has
a
sound
concept,
and
so
does
the
work
as a whole."
10
hbox->setMargin(6);
-Matthias
Ettrich,
Trolltech's
lead
developer,
founder of the KDE project
11
hbox->setSpacing(6);
"The
of Qt'.... *spinBox
The Qt system

is aQSpinBox(hbox);
beautiful example of object oriented design, and the
12 'Tao
QSpinBox
= new
authors
take
advantage
of
this....
The
authors have done an excellent
13
QSlider *slider = new QSlider(Qt::Horizontal,
hbox);job of presenting the
subject
in
an
interesting
and
engaging
way...."
14
spinBox->setRange(0, 130);
-Ron
Instructor and Chair
of the Department of Computer Science, Penn State Erie,
15 McCarty,
slider->setRange(0,
130);

The Behrend College
16
QObject::connect(spinBox, SIGNAL(valueChanged(int)),
The first official Trolltech guide to Qt 3.2 programming!
17
slider, SLOT(setValue(int)));
18
QObject::connect(slider,
SIGNAL(valueChanged(int)),
Straight from Trolltech, this book covers
all you need to build industrial-strength applications
19
spinBox,
with Qt 3.2.x and C++-applications thatSLOT(setValue(int)));
run natively on Windows, Linux/Unix, Mac OS X, and
20
spinBox->setValue(35);
embedded
Linux with no source code changes! The book teaches solid Qt programming
practices; it is not a rehash of the documentation. You'll find start-to-finish coverage packed
21
app.setMainWidget(hbox);
with examples,
plus a CD with the Qt 3.2 toolset and Borland C++ compilers-including a non22
hbox->show();
commercial
Qt 3.2 for Windows available nowhere else!
23
return app.exec();
24 } Build powerful C++ GUI applications quickly and easily

Design dialogs and main windows visually and in code
Lines 8 to 11 set up the QHBox.[*] We call setCaption() to set the text displayed in the
Learn
Qt's
innovative
slots mechanism
window's
title
bar.
Then wetypesafe
put somesignals
space and
(6 pixels)
around and in between the child widgets.
Use
If layouts
you get ato
compiler
createerror
forms
on the
thatQHBox
automatically
constructor,
size
it means
and scale
that you are using an older version of
[*]


Qt. Make sure that you are using Qt 3.2.0 or a later Qt 3 release.

Create custom signals, slots, events, and controls
Lines 12 and 13 create a QSpinBox and a QSlider with the QHBox as the parent.
Program the ''Qt way'' with techniques for Qt 3.2 that'll work with Qt 4
24
1


Even
though
[ Team
LiB ] we didn't set the position or size of any widget explicitly, the QSpinBox and
QSlider appear nicely laid out side by side inside the QHBox. This is because QHBox
automatically assigns reasonable positions and sizes to its children based on their needs. Qt
provides many classes like QHBox to free us from the chore of hard-coding screen positions in
our applications.
Lines 14 and 15 set the valid range for the spin box and the slider. (We can safely assume that
the user is at most 130 years old.) The two connect() calls shown in lines 16 to 19 ensure that
the spin box and the slider are synchronized so that they always show the same value.
Whenever the value of one widget changes, its valueChanged(int) signal is emitted, and the
setValue(int) slot of the other widget is called with the new value.
Line 20 sets the spin box value to 35. When this happens, the QSpinBox emits the
valueChanged(int) signal with an int argument of 35. This argument is passed to the
QSlider'ssetValue(int) slot, which sets the slider value to 35. The slider then emits the
valueChanged(int) signal, because its own value changed, triggering the spin box's

Tableslot.
of Contents
setValue(int)

But at this point, setValue(int) doesn't emit any signal, since the spin

Index
box value is already 35. This prevents infinite recursion. Figure 1.6 summarizes the situation.
C++ GUI Programming with Qt 3
ByJasmin BlanchetteFigure
, Mark Summerfield
1.6. Changing

one value changes both

Publisher: Prentice Hall PTR
Pub Date: January 15, 2004
ISBN: 0-13-124072-2
Pages: 464

"...not only the best book on Qt I have ever seen, but also the best book presenting any
programming framework. Every sentence appears to be carefully worded, and every chapter
has a sound concept, and so does the work as a whole."
-Matthias Ettrich, Trolltech's lead developer, founder of the KDE project
"The 'Tao of Qt'.... The Qt system is a beautiful example of object oriented design, and the
authors take advantage of this.... The authors have done an excellent job of presenting the
subject in an interesting and engaging way...."
-Ron McCarty, Instructor and Chair of the Department of Computer Science, Penn State Erie,
The Behrend College
The first official Trolltech guide to Qt 3.2 programming!
Line 22 shows the QHBox and its two child widgets.
Straight from Trolltech, this book covers all you need to build industrial-strength applications
Qt's
to building

user interfaces
is simple
to understand
very flexible.
most
with approach
Qt 3.2.x and
C++-applications
that run
natively
on Windows,and
Linux/Unix,
Mac The
OS X,
and
common
pattern
that
Qt
programmers
use
is
to
instantiate
the
required
widgets
and
then
set

embedded Linux with no source code changes! The book teaches solid Qt programming
their
properties
as anecessary.
add the
widgets
to layouts, which
automatically
practices;
it is not
rehash of Programmers
the documentation.
You'll
find start-to-finish
coverage
packed
take
care of sizing
positioning.
User
behavior
is managed
by connecting widgets
with examples,
plusand
a CD
with the Qt
3.2interface
toolset and
Borland

C++ compilers-including
a nontogether
using
signals
and slots
mechanism.
commercial
Qt Qt's
3.2 for
Windows
available
nowhere else!
[ Team LiB ]
Build powerful C++ GUI applications quickly and easily
Design dialogs and main windows visually and in code
Learn Qt's innovative typesafe signals and slots mechanism
Use layouts to create forms that automatically size and scale
Create custom signals, slots, events, and controls
Program the ''Qt way'' with techniques for Qt 3.2 that'll work with Qt 4
25
1


×