Qt, the Qt logo, Qtopia, the Qtopia logo, Trolltech, and the Trolltech logo are
registered trademarks of. Trolltech ASA and/or its subsidiaries in the U.S. and
other ...
Foundations of Qt Development
Johan Thelin
Foundations of Qt Development Copyright © 2007 by Johan Thelin All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-59059-831-3 ISBN-10 (pbk): 1-59059-831-8 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Qt, the Qt logo, Qtopia, the Qtopia logo, Trolltech, and the Trolltech logo are registered trademarks of Trolltech ASA and/or its subsidiaries in the U.S. and other countries. All rights reserved. Lead Editor: Jason Gilmore Technical Reviewer: Witold Wysota Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jason Gilmore, Jonathan Hassell, Chris Mills, Matthew Moodie, Jeffrey Pepper, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Senior Project Manager: Tracy Brown Collins Copy Edit Manager: Nicole Flores Copy Editor: Nancy Sixsmith Assistant Production Director: Kari Brooks-Copony Production Editor: Kelly Winquist Compositor: Dina Quan Proofreader: Paulette McGee Indexer: Brenda Miller Artist: April Milne Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail
[email protected], or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail
[email protected], or visit http://www.apress.com. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com in the Source Code/ Download section.
Till Åsa.
Contents at a Glance Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
PART 1 ■CHAPTER ■CHAPTER ■CHAPTER ■CHAPTER
PART 2 ■CHAPTER ■CHAPTER ■CHAPTER ■CHAPTER ■CHAPTER ■CHAPTER ■CHAPTER ■CHAPTER ■CHAPTER ■CHAPTER ■CHAPTER ■CHAPTER
PART 3
■■■ 1 2 3 4
The Qt Way of C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Rapid Application Development Using Qt . . . . . . . . . . . . . . . . . . . . . . . 33 Widgets and Layouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 The Main Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
■■■ 5 6 7 8 9 10 11 12 13 14 15 16
The Qt Building Blocks
The Model-View Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Creating Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Drawing and Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Files, Streams, and XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Providing Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 Internationalization and Localization . . . . . . . . . . . . . . . . . . . . . . . . . . 279 Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 Doing Things in Parallel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403 Building Qt Projects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 Unit Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
■■■
■APPENDIX A ■APPENDIX B
Getting to Know Qt
Appendixes
Third-Party Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501 Containers, Types, and Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513 v
Contents Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
PART 1
■■■
■CHAPTER 1
Getting to Know Qt
The Qt Way of C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Installing a Qt Development Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Installing on Unix Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Installing on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Making C++ “Qt-er” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Inheriting Qt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Using a Qt String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Building a Qt Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Signals, Slots, and Meta-Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Making the Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Revisiting the Build Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Connection to Something New . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Collections and Iterators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Iterating the QList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Filling the List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 More Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Special Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
■CHAPTER 2
Rapid Application Development Using Qt . . . . . . . . . . . . . . . . . 33 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Event-Driven Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Using Designer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 vii
viii
■CONTENTS
From Designer to Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 The Final Touches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
■CHAPTER 3
Widgets and Layouts
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Creating Dialogs in Qt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Size Policies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Layouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Common Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 QPushButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 QLabel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 QLineEdit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 QCheckBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 QRadioButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 QGroupBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 QListWidget . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 QComboBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 QSpinBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 QSlider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 QProgressBar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Common Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Even More Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Validating User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Validators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
■CHAPTER 4
The Main Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Windows and Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Single Document Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Multiple Document Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Comparing Single and Multiple Document Interfaces . . . . . . . . . . 111 Application Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Resource File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Project File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Application Icon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Dockable Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
■CONTENTS
PART 2
■■■
■CHAPTER 5
The Qt Building Blocks
The Model-View Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Showing Data by Using Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Providing Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Limiting Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Limiting Selection Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 A Single Column List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Creating Custom Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 A Delegate for Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Custom Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Creating Your Own Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Creating Custom Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 A Read-Only Table Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 A Tree of Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Editing the Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Sorting and Filtering Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
■CHAPTER 6
Creating Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Composing Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Changing and Enhancing Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Catching the Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Creating Custom Widgets from Scratch . . . . . . . . . . . . . . . . . . . . . . . 171 Your Widgets and Designer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Promotion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Providing a Plugin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
■CHAPTER 7
Drawing and Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Drawing Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 The Drawing Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Transforming the Reality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Painting Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 The Graphics View. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 Interacting Using a Custom Item . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 OpenGL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
ix
x
■CONTENTS
■CHAPTER 8
Files, Streams, and XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Working with Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Working with Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 Working with Streams. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 DOM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 Reading XML Files with SAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 Files and the Main Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
■CHAPTER 9
Providing Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 Creating Tooltips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 Creating HTML-Formatted Tooltips. . . . . . . . . . . . . . . . . . . . . . . . . . . 259 Inserting Images into Tooltips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 Applying Multiple Tooltips to a Widget . . . . . . . . . . . . . . . . . . . . . . . . 260 Providing What’s This Help Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 Embedding Links into What’s This Help Tips . . . . . . . . . . . . . . . . . . 264 Taking Advantage of the Status Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Creating Wizards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 Assisting the User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 Creating the Help Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 Putting It Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
■CHAPTER 10 Internationalization and Localization. . . . . . . . . . . . . . . . . . . . . 279 Translating an Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 Extracting the Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 Linguist: A Tool for Translating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 Set Up a Translation Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 Qt Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 Dealing with Other Translation Cases . . . . . . . . . . . . . . . . . . . . . . . . 287 Find the Missing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 Translating on the Fly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 Other Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 Dealing with Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298 Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
■CONTENTS
■CHAPTER 11 Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 Plugin Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 Extending Qt with Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 Creating an ASCII Art Plugin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 Extending Your Application Using Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . 317 Filtering Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 Merging the Plugin and the Application . . . . . . . . . . . . . . . . . . . . . . . 323 A Factory Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 Non-Qt Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
■CHAPTER 12 Doing Things in Parallel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 Basic Threading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 Building a Simple Threading Application . . . . . . . . . . . . . . . . . . . . . . 334 Synchronizing Safely. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336 Protecting Your Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338 Protected Counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 Locking for Reading and Writing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341 Sharing Resources Among Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 Getting Stuck . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 Producers and Consumers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347 Signaling Across the Thread Barrier. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352 Passing Strings Between Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . 353 Sending Your Own Types Between Threads . . . . . . . . . . . . . . . . . . . 356 Threads, QObjects, and Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 Pitfalls when Threading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 The User Interface Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360 Working with Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363 Running uic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363 The Shell and Directions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
■CHAPTER 13 Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 A Quick Introduction to SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 What Is a Database? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 Inserting, Viewing, Modifying, and Deleting Data. . . . . . . . . . . . . . . 372 More Tables Mean More Power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 Counting and Calculating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
xi
xii
■CONTENTS
Qt and Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378 Making the Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378 Querying Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380 Establishing Several Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382 Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382 The Structure of the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384 The User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384 The Database Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392 Putting Everything Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397 Model Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398 The Query Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399 The Table Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399 The Relational Table Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
■CHAPTER 14 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403 Using the QtNetwork Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403 Working with Client Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403 Creating an FTP Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404 Creating an HTTP Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417 Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424 Reliability’s Role with UDP and TCP . . . . . . . . . . . . . . . . . . . . . . . . . . 424 Servers, Clients, and Peers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425 Sending Images Using TCP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425 Broadcasting Pictures Using UDP . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
■CHAPTER 15 Building Qt Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 QMake . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 The QMake Project File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 Working with Different Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450 Building Libraries with QMake. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453 Building Complex Projects with QMake . . . . . . . . . . . . . . . . . . . . . . . 454 The CMake Build System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457 Managing a Simple Application with QMake . . . . . . . . . . . . . . . . . . 457 Working with Different Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461 Building Libraries with CMake . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465 Managing Complex Projects with CMake . . . . . . . . . . . . . . . . . . . . . 466 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
■CONTENTS
■CHAPTER 16 Unit Testing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471 Unit Testing and Qt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472 The Structure of a Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472 Testing Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474 Implementing the Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475 Data-Driven Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479 Testing Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483 Testing a Spin Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483 Driving Widgets with Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487 Testing Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490 Testing for Real . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491 The Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492 The Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492 Handling Deviations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
PART 3
■■■
■APPENDIX A
Appendixes
Third-Party Tools
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
Qt Widgets for Technical Applications: Qwt . . . . . . . . . . . . . . . . . . . . . . . . 502 wwWidgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503 QDevelop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504 Edyuk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
■APPENDIX B
Containers, Types, and Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . 507 Containers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507 Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507 Specialized Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508 Associative Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509 Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509 Types by Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509 The Variant Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510 Macros and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511 Treating Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511 Random Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511 Iterating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
xiii
Foreword M
y very first computer, a ZX81, did not have a graphical user interface. Compared with today’s offerings, I’d say it hardly had graphics at all. That computer never got me excited about programming, mostly because the manuals were in English and I didn’t yet know how to read the language. Then I met the ABC80, a Swedish computer from Luxor. It had the same Z80 processor, 16 kilobytes of RAM, and no real graphics to talk about. It did have an introduction to BASIC in Swedish, though, so it got me started with programming. My next computer experience was an Atari ST. I must admit that in the beginning I used it mostly for gaming. But as time passed I was thrilled about the possibilities of the Atari for programming. I wrote games, utilities, and painting applications. I also ran into something that I learned to like: an API for handling windows and drawing graphics. Moving on, I got a PC. I learned C and C++, as well as how to do 3D graphics in software (this was before 3D graphics cards). I was introduced to the Internet and learned lots of new things from newsgroups and FAQs. I also got my first paid job as a programmer, processing scientific data using FORTRAN. At Chalmers University I met Tru64 UNIX and X Windows. The API for doing graphics felt awkward, so I went looking for something better. That was when I found Qt. Back then, it just solved my problem of the day: showing a couple of dialogs and drawing some graphics. But the architecture got me hooked. Over time, I used Qt more and more. I soon tried to figure out what it was that made Qt so easy to use. The flexibility of the signals and slots concept that enabled me to connect widgets and objects to each other was one reason. As was the up-to-date reference documentation— nothing was left undocumented. And the naming made it easy to find the class and method I was looking for. The name said it all. Qt brought me to KDE and Linux. I learned to love GCC, Makefiles, and shell scripting. The thing that thrilled me about Qt was that no matter what the task was, it fit right into its architecture. Today, with Qt 4.0, the API covers most of the tasks that you might want to perform. Graphics, files, databases, networking, printing—you name it. Qt helps me solve my problems quickly and easily. I’ve recently become more and more involved in the Qt community. It all started with my original “Independent Qt Tutorial” that introduced Qt 3.0 (you can still find it at www.thelins.se/qt). I’m also a part of the administration team at QtCentre, which is where I met the technical reviewer of this book, Witold Wysota. QtCentre (www.qtcentre.org) is a community-driven forum, a wiki, and a news site—the natural meeting place for Qt developers. Just over a year ago, Apress posted this question in the jobs section: Is there anyone who wants to write a book about Qt? That was the starting point of the book that you are reading right now. Johan Thelin M.Sc.E.E. xv
About the Author ■JOHAN THELIN has worked with software development since 1995 and has experience ranging from embedded systems to server-side enterprise software. He started using Qt in 2000 and has loved using it ever since. Since 2002 Johan has provided the Qt community with tutorials, articles, and help (most notably, he wrote the “Independent Qt Tutorial”). He currently works as a consultant focusing on embedded systems, FPGA design, and software development.
xvii
About the Technical Reviewer ■WITOLD WYSOTA, Institute of Computer Science, Warsaw University of Technology, was born in Wroclaw, Poland. He has a Master of Science degree in Computer Science from the Warsaw University of Technology (WUT), where he is currently a PhD candidate. As such, he gives lectures about Qt and conducts exercises using Qt for programming interactive applications. Witold has been a Qt user since 2004 and was an active contributor to QtForum.org community forum before January 2006—when he established QtCentre.org with Axel Jäger, Daniel Kish, Jacek Piotrowski, and Johan Thelin. It has since become the biggest actively maintained, community-based Qt-related site and forum. Witold has been practicing the traditional Seven Star Praying Mantis Kung-Fu style since 1989 and has achieved success in domestic tournaments. He is interested in IT, sports, martial arts, astrophysics, and history. He lives in Warsaw.
xix
Acknowledgments T
here are so many people I want to thank—everybody involved in the project has been helpful, positive, and supportive. It has been a great time working with all of you. First, many thanks go to Witold Wysota, who has provided me with feedback, technical input, and kind words. Without his support I could not have completed this project. I would also like to thank Jason Gilmore from Apress for his excellent feedback and writing tips. Thanks to him, the text is far more enjoyable to read. Jasmin Blanchette of Trolltech helped me by producing screenshots from the Mac. The excellent support team at Trolltech also clarified unclear issues and fixed bugs. Everyone at Trolltech has been very positive and supportive. I want to thank all the people at Apress: Matt Wade, who gave me the chance to do this; Elizabeth Seymour, Grace Wong, and Tracy Brown Collins for managing the project. An extra thanks to Tracy who pushed me the last mile to get the project done on time. Without the help of Nancy Sixsmith’s language skills, the text would not have been as easy to read. Thanks to her attention to detail and excellent writing abilities, the text reads as well as it does today. There are so many people involved in this project that I have not worked with so closely. I’m still very grateful to their efforts and appreciate their skills. Many thanks go to Kelly Winquist, Dina Quan, Brenda Miller, April Milne, and Paulette McGee.
xxi