Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and the publisher was aware of a trademark
claim, the designations have been printed with initial capital letters or in all capitals.
The authors and publisher have taken care in the preparation of this book, but make no expressed or implied
warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for
incidental or consequential damages in connection with or arising out of the use of the information or
programs contained herein.
The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special
sales, which may include electronic versions and/or custom covers and content particular to your business,
training goals, marketing focus, and branding interests. For more information, please contact:
U.S. Corporate and Government Sales
(800) 382-3419
corpsales@pearsontechgroup.com
For sales outside the United States, please contact:
International Sales
international@pearsoned.com
Visit us on the Web: www.prenhallprofessional.com
This Book Is Safari Enabled
The Safari
®
Enabled icon on the cover of your favorite technology book means the book
is available through Safari Bookshelf. When you buy this book, you get free access to the
online edition for 45 days.
Safari Bookshelf is an electronic reference library that lets you easily search thousands of technical books,
find code samples, download chapters, and access technical information whenever and wherever you
need it.
To gain 45-day Safari Enabled access to this book:
• Go to http://www.prenhallprofessional.com/safarienabled
• Complete the brief registration form
• Enter the coupon code S1F3-5NDG-9Z64-LNGX-K8F2
If you have difficulty registering on Safari Bookshelf or accessing the online edition, please e-mail
customer-service@safaribooksonline.com.
Library of Congress Cataloging-in-Publication Data
Blanchette, Jasmin.
C
++
GUI programming with Qt 4 / Jasmin Blanchette, Mark Summerfield.–2nd ed.
p. cm.
Includes index.
ISBN-13: 978-0-13-235416-5 (hardcover : alk. paper)
1. Qt (Electronic resource) 2. Graphical user interfaces (Computer systems) 3. C
++
(Computer program
language) I. Summerfield, Mark. II. Title.
QA76.9.U83B532 2008
005.13’3—dc22
2008000243
Copyright
©
2008 Trolltech ASA
All rights reserved. Printed in the United States of America. This publication may only be distributed
subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version
is available at http://www.opencontent.org/openpub/).
Trolltech
®
,Qt
®
, Qtopia
®
, and the Trolltech and Qtopia logos are registered trademarks of Trolltech ASA.
ISBN-13: 978-0-13-235416-5
ISBN-10: 0-13-235416-0
Text printed in the United States on recycled paper at Courier in Westford, Massachusetts.
First printing, February 2008
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
++
perfor-
mance, the availability of the source code, its documentation, the high-quality
technical support, and all the other items mentioned in Trolltech’s glossy mar-
keting 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 every-
thing that doesn’t. How else can we explain that some of the brightest program-
mers need help to program a video recorder, or that most engineers seem to have
trouble operating the company’sphonesystem? Ifor one am perfectly capable of
memorizing sequences of random numbers and commands, but if these are re-
quired to control my answering machine,I’d prefer not to have one. At Trolltech,
our phone system forces us to press the ‘
∗
’ for two seconds before we are allowed
to enter the other person’s extension number. If you forget to do this and start
to enter the extension immediately, you have to dial the entire number again.
Why ‘
∗
’? Why not ‘#’, or ‘1’, or ‘5’, or any of the other 20 keys on the phone? Why
two seconds and not one, or three, or one and a half? Why anything at all? I
find the phone so irritating that I avoid using it whenever I can. Nobody likes
having to do random things, especially when those random things apparently
depend on some 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. Qt is different. For one thing, Qt makes sense.
And for another, Qt is fun. Qt lets you concentrate on your tasks. When Qt’s
original architects faced a problem, they didn’t just look for a good solution, or
a quick solution, or the simplest solution. They looked for the right solution,
and then they documented it. Granted, they made mistakes,and granted, some
of their design decisions didn’t pass the test of time, but they still got a lot of
things right, and what wasn’t right could and can be corrected. You can see
this by the fact that a system originally designed to bridge Windows 95 and
Unix/Motif now unifies modern desktop systems as diverse as Windows Vista,
Mac OS X, and GNU/Linux, as well as small devices such as mobile phones.
Long before Qt became so popular and so widely used, the dedication of Qt’s
developers to finding the right solutions made Qt special. That dedication is
just as strong today and affects everyone who maintains and develops Qt. For
us, working on Qt is a responsibility and a privilege. We are proud of helping
to make your professional and open source lives easier and more enjoyable.
xiii
One of the things that makes Qt a pleasure to use is its online documentation.
But the documentation’s focus is primarily on individual classes, with little said
about how to build sophisticated real-world applications. This excellent book
fills that gap. It shows you what Qt has to offer,how to program Qt the “Qt way”,
and how to get the best from Qt. The book will teach a C
++
, Java, or C# pro-
grammer how to program Qt, and provides enough advanced material to satisfy
experienced Qt programmers. The book is packed with good examples, advice,
and explanations—and it is the text that we use to induct all new programmers
who join Trolltech.
Nowadays, a vast number of commercial and free Qt applications are available
for purchase or download. Some are specialized for particular vertical markets,
while others are aimed at the mass-market. Seeing so many applications built
with Qt fills us with pride and inspires us to make Qt even better. And with the
help of this book, there will be more and higher-quality Qt applications than
ever before.
Matthias Ettrich
Berlin, Germany
November 2007
xiv
Preface
Qt is a comprehensive C
++
application development framework for creating
cross-platform GUI applications using a “write once, compile anywhere” ap-
proach. Qt lets programmers use a single source tree for applications that
will run on Windows 98 to Vista, Mac OS X, Linux, Solaris, HP-UX, and many
other versions of Unix with X11. The Qt libraries and tools are also part of
Qt/Embedded Linux, a product that provides its own window system on top of
embedded Linux.
The purpose of this book is to teach you how to write GUI programs using
Qt 4. The book starts with “Hello Qt” and quickly progresses to more advanced
topics, such as creating custom widgets and providing drag and drop. The text
is complemented by a set of examples that you can download from the book’s
web site,
http://www.informit.com/title/0132354160
. Appendix A explains how
to download and install the software, including a free C
++
compiler for those
using Windows.
The book is divided into three parts. Part I covers all the fundamental concepts
and practices necessary for programming GUI applications using Qt. Knowl-
edge of this part alone is sufficient to write useful GUI applications. Part II cov-
ers central Qt topics in greater depth,and Part III provides more specialized and
advanced material. You can read the chapters of Parts II and III in any order,
but they assume familiarity with the contents of Part I. The book also includes
several appendixes, with Appendix B showing how to build Qt applications and
Appendix C introducing Qt Jambi, the Java version of Qt.
The first Qt 4 edition of the book built on the Qt 3 edition, although it was
completely revised to reflect good idiomatic Qt 4 programming techniques and
included new chapters on Qt 4’s model/view architecture, the new plugin frame-
work, embedded programming with Qt/Embedded Linux, and a new appendix.
This extended and revised second edition has been thoroughly updated to take
advantage of features introduced in Qt versions 4.2 and 4.3, and includes new
chapters on look and feel customization and application scripting as well as two
new appendixes. The original graphics chapter has been split into separate 2D
and 3D chapters, which between them now cover the new graphics view class-
es and
QPainter
’s OpenGL back-end. In addition, much new material has been
added to the database, XML, and embedded programming chapters.
This edition, like its predecessors, emphasizes explaining Qt programming and
providing realistic examples, rather than simply rehashing or summarizing
Qt’s extensive online documentation. Because the book teaches solid Qt 4
programming principles and practices, readers will easily be able to learn the
xv