Transcription of An Introduction to Design Patterns in C++ with Qt 4
1 An Introduction to DesignPatterns in C++ with Qt 8/3/06 4:25 PM Page 5/22/06 1:15 PM Page 1 Upper Saddle River, NJ Boston Indianapolis San FranciscoNew York Toronto Montreal London Munich Paris MadridCapetown Sydney Tokyo Singapore Mexico CityAn Introduction to Design Patternsin C++ with Qt 4 Alan EzustPaul 8/3/06 4:25 PM Page iiiMany of the designations used by manufacturers and sellers to distinguish their products are claimed as trade-marks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the des-ignations have been printed with initial capital letters or in all authors and publisher have taken care in the preparation of this book, but make no expressed or implied war-ranty of any kind and assume no responsibility for errors or omissions.
2 No liability is assumed for incidental orconsequential damages in connection with or arising out of the use of the information or programs 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 Corporate and Government Sales(800) sales outside the United States, please contact:International Book Is Safari EnabledThe Safari Enabled icon on the cover of your favorite technology book means the book isavailable through Safari Bookshelf. When you buy this book, you get free access to the onlineedition for 45 Bookshelf is an electronic reference library that lets you easily search thousands of technical books, find codesamples, download chapters, and access technical information whenever and wherever you need gain 45-day Safari Enabled access to this book: Go to Complete the brief registration form Enter the coupon code 1 AMQ-56YL-KJ1A-LXLU-GGLTIf you have difficulty registering on Safari Bookshelf or accessing the online edition, please e-mail us on the Web: of Congress Cataloging-in-Publication DataEzust, Introduction to Design Patterns in C++ with Qt 4 / Alan Ezust, Paul bibliographical references and 0-13-187905-7 (pbk.)
3 : alk. paper)1. C++ (Computer program language) 2. Software Patterns . 3. Computer software Ezust, Paul. II. 3 dc222006011947 Copyright 2007 Pearson Education, material may only be distributed subject to the terms and conditions set forth in the Open Publication License, or later. (The latest version is presently available at )For information regarding permissions, write to:Pearson Education, and Contracts DepartmentOne Lake StreetUpper Saddle River, NJ 07458 Fax: (201) 236-3290 ISBN 0-13-187905-7 Text printed in the United States on recycled paper at Courier in Stoughton, printing, August 8/3/06 4:25 PM Page ivThis book is dedicated to Miriam Ezust,without whom none of our work would have been 8/3/06 4:25 PM Page 8/3/06 4:25 PM Page viContentsPreface xixAcknowledgments xxiiiRationale for the Book xxvAbout the Authors xxviiPA RT IIntroduction to C++ and Qt 4 21C++ Introduction of C++ Brief History of C++ : Open-Source *nix from Source.
4 ++ First Example and Output , Types, and Literals ++ Simple Types and Command Line Arguments ++ Standard Library Strings Keyword const and Memory Access The Unary Operators & and * Operators new and delete 8/3/06 4:25 PM Page * and *const Variables 43 Points of Departure 44 Review Questions 452 Classes Definitions Access Specifiers to UML Relationships of a Class Keyword static Constructors and Assignment Operators Member Functions 68 Review Questions 793 Introduction to Qt Project: Using QApplication and QLabel , qmake, and Project Files #include: Finding Header Files make Command Up Files Help Online Guidelines and Naming Conventions Qt Core Module and Dates 91 Points of Departure 93 Review Questions 8/3/06 4.
5 25 PM Page viii4 Lists to Containers and Iteration 99 Points of Departure 102 Review Questions 1035 Functions Declarations Functions Arguments Overloading Passing by Value Passing by Reference to const Return Values References from Functions on const-ness Functions versus Macro Expansion 127 Review Questions 1336 Inheritance and Polymorphism Derivation Client Code Example with Polymorphism from an Abstract Base Class Design , Hiding, and Overriding , Destructors, and Copy Assignment Operators Command-Line Arguments and ArgumentList 159 Points of Departure 164 Review Questions 8/3/06 4:25 PM Page ixPA RT I IHigher-Level Programming 1667 Libraries Containers Other Libraries Libraries: Dependency Management Libraries Libraries: A Lab Exercise the Linker Path and Components 178 Review Questions 1808 Introduction to Design Patterns and the Visitor Pattern and Files: QDir and QFileInfo Pattern the Visitor Using Inheritance 186 Review Questions 1909 QObject s Child Managment Pattern: Parents and Children Children and the Event Loop : A First Look to Slots and Slots and moc: A Checklist and Objects () and Internationalization 211 Point of Departure 211 Review Questions 8/3/06 4.
6 25 PM Page x10 Generics and Containers and Templates Function Templates Class Templates Containers, Composites,and Aggregates Shared Classes , Algorithms, and Operators Pattern Benefits of the Serializer Map Example 229 Review Questions 23511Qt GUI Widgets Categories and QSettings QSettings: Saving and Restoring Application State Input Dialogs and Widgets and Resources of Widgets Spacing, Stretching, and Struts Moving Widgets across Layouts , QMenus, and QMenuBars , QToolbars, and QActionGroups The Command Pattern and QDockWidgets of a QStringList 272 Points of Departure 274 Review Questions 8/3/06 4:25 PM Page xi12 Concurrency and Process Control Processes and Environment Qonsole: Writing an Xterm in Qt Qonsole with Keyboard Events and QThread QPixmap and QThread Animation Example: Movie Player Movie Player with QTimer Multiple Threads, Queues, and Loggers Example: Giant Thread Safety and QObjects : QProcess and QThread 303 Review Questions 30513 Validation and Regular Expressions Expressions Regular Expression Syntax Regular Expressions: Phone Number Recognition Expression Validation 316 Review Questions 31914 Parsing XML Qt XML Module Parsing , Tree Structures, and DOM Visitor Pattern.
7 DOM Tree Walking Generation of XML with DOM 335 Review Questions 34015 Meta Objects, Properties, and Reflective Programming : The MetaObject Pattern 8/3/06 4:25 PM Page Identification and qobject_cast Macro: Describing QObject Properties Class: Accessing Properties : An Extension of QObject Containers: PropsMap 355 Review Questions 35716 More Design Patterns Patterns Abstract Factory Abstract Factories and Libraries qApp and Singleton Pattern Creation Rules and friend Functions(What Friends Are Really For) Benefits of Using Factories Pattern Revisited Exporting to XML Importing Objects with an Abstract Factory Fa ade Pattern Functional Fa ade Smart Pointers: auto_ptr FileTagger: Fa ade Example 385 Points of Departure 389 Review Questions 39017 Models and Views : What about the Controller?
8 Form Models Form Models Form Views Unforseen Types Controlling Actions DataObject Form Model 4 Models and Views Models 8/3/06 4:25 PM Page Models Extended Tree Widget Items 418 Review Questions 42118Qt SQL Classes to MySQL and Result Sets Models 429 Review Questions 433 PART IIIC++ Language Reference 43419 Types and Expressions of Logical Expressions and Unsigned Integral Types Expression Conversions Conversions Typecasting Using ANSI C++ Typecasts static_cast and const_cast reinterpret_cast Why Not Use C-style Casts? Type Identification (RTTI) typeid Operator Selection Operators 457 Point of Departure 458 Review Questions 46120 Scope and Storage Class and Definitions Scope Default Scope of Identifiers: A Summary File Scope versus Block Scope and operator:: Class Globals, statics, and QObject 8/3/06 4.
9 25 PM Page Anonymous Namespaces Open Namespaces Namespace, static Objects and extern 476 Review Questions 47821 Statements and Control Structures Statements Exception Handling Exception Types throwing Things Around try and catch More about throw Rethrown Exceptions Exception Expressions 497 Review Questions 50222 Memory Access Pathology Pointer Pathology with Heap Memory Access Summary to Arrays Arithmetic , Functions, and Return Values Kinds of Arrays Pointer Operations Happens If new Fails? set_new_handler(): Another Approach to New Failures Using set_new_handler and bad_alloc Checking for null: The Updated Way to Test for New Failures Chapter Summary 519 Review Questions 8/3/06 4:25 PM Page xv23 Inheritance in Detail Pointers and Virtual Tables and virtual Destructors Inheritance Multiple Inheritance Syntax Multiple Inheritance with Abstract Interfaces 531 Point of Departure Resolving Multiple Inheritance Conflicts , protected, and private Derivation 536 Review Questions 53924 Miscellaneous Topics with Variable-Length Argument Lists Sharing 543PA RT I VProgramming Assignments 54825MP3 Jukebox Assignments Model: Mp3 File.
10 Generating Playlists : An Enumerated id3lib Serialization Mp3 File Related Classes Queries and Filters and Views: PlayList Source Selector Persistent Settings Edit Form View for FileTagger Database View 569 Points of Departure 8/3/06 4:25 PM Page xviPA RT VAppendices 572 Appendix A: C++ Reserved Keywords 575 Appendix B: Standard Headers 577 Appendix C: The Development Environment Preprocessor: For #including Files the Linker Common Linker Error Messages Building a Debuggable Target gdb Quickstart Finding Memory Errors Assistant and Designer IDEs and Development Tools UML Modeling Tools jEdit 598 Bibliography 601 Index 8/3/06 4:25 PM Page 8/3/06 4:25 PM Page xviiiPrefaceC++ had been in use for many years before it was standardized in 1989, whichmakes it a relatively mature language compared to others that are in popular usetoday.