You are previewing An Introduction to Design Patterns in C++ with Qt 4.
O'Reilly logo
An Introduction to Design Patterns in C++ with Qt 4

Book Description

Learn C++, Patterns, and Qt 4 Cross-Platform Development

Master C++ and design patterns together, using the world's leading open source framework for cross-platform development: Qt 4.

An Introduction to Design Patterns in C++ with Qt 4 is a complete tutorial and reference that assumes no previous knowledge of C, C++, objects, or patterns. You'll walk through every core concept, one step at a time, learning through an extensive collection of Qt 4.1-tested examples and exercises.

By the time you're done, you'll be creating multithreaded GUI applications that access databases and manipulate XML files--applications that run on platforms including Windows, Linux, Unix, and Mac OS X. Best of all, you'll be writing code that's efficient, reusable, and elegant.

  • Learn objects fast: classes, inheritance, polymorphism, and more

  • Master powerful design patterns

  • Discover efficient high-level programming techniques using libraries, generics, and containers

  • Build graphical applications using Qt widgets, models, and views

  • Learn advanced techniques ranging from multithreading to reflective programming

  • Use Qt's built-in classes for accessing MySQL data

  • Includes a complete C++ language reference

Table of Contents

  1. Copyright
    1. Dedication
  2. Bruce Perens’ Open Source Series
  3. Preface
    1. How to Use This Book
    2. A Note about Formats and Book Production
    3. Style Conventions
  4. Acknowledgments
  5. Rationale for the Book
  6. About the Authors
  7. I. Introduction to C++ and Qt 4
    1. 1. C++ Introduction
      1. 1.1. Overview of C++
      2. 1.2. A Brief History of C++
      3. 1.3. Setup: Open-Source Platforms
        1. 1.3.1. *nix
        2. 1.3.2. Downloading from Source
      4. 1.4. Setup: Win32
      5. 1.5. C++ First Example
        1. Comments
        2. #include
        3. The Using Namespace Directive
        4. Declaring and Initializing Variables
        5. Selection
        6. Iteration
      6. 1.6. Input and Output
        1. Exercises: Input and Output
      7. 1.7. Identifiers, Types, and Literals
        1. Exercises: Identifiers, Types, and Literals
      8. 1.8. C++ Simple Types
        1. 1.8.1. main and Command Line Arguments
        2. 1.8.2. Arithmetic
        3. Exercises: C++ Simple Types
      9. 1.9. C++ Standard Library Strings
      10. 1.10. Streams
        1. Exercise: Streams
      11. 1.11. The Keyword const
      12. 1.12. Pointers and Memory Access
        1. 1.12.1. The Unary Operators & and *
        2. 1.12.2. Operators new and delete
        3. Exercises: Pointers and Memory Access
      13. 1.13. const* and *const
      14. 1.14. Reference Variables
      15. Points of Departure
      16. Review Questions
    2. 2. Classes
      1. 2.1. Structs
      2. 2.2. Class Definitions
      3. 2.3. Member Access Specifiers
      4. 2.4. Encapsulation
      5. 2.5. Introduction to UML
        1. 2.5.1. UML Relationships
      6. 2.6. Friends of a Class
      7. 2.7. Constructors
      8. 2.8. Subobjects
      9. 2.9. Destructors
      10. 2.10. The Keyword static
        1. Block-Scope static
        2. static Initialization
      11. 2.11. Copy Constructors and Assignment Operators
      12. 2.12. Conversions
      13. 2.13. const Member Functions
        1. Exercises: Classes
      14. Review Questions
    3. 3. Introduction to Qt
      1. 3.1. Example Project: Using QApplication and QLabel
      2. 3.2. Makefile, qmake, and Project Files
        1. 3.2.1. #include: Finding Header Files
        2. 3.2.2. The make Command
        3. 3.2.3. Cleaning Up Files
      3. 3.3. Getting Help Online
      4. 3.4. Style Guidelines and Naming Conventions
      5. 3.5. The Qt Core Module
      6. 3.6. Streams and Dates
        1. Exercise: The Qt Core Module
      7. Points of Departure
      8. Review Questions
    4. 4. Lists
      1. 4.1. Introduction to Containers
      2. 4.2. Iterators
        1. 4.2.1. QStringList and Iteration
      3. 4.3. Relationships
        1. Exercises: Relationships
        2. Exercises: Contact List
      4. Points of Departure
      5. Review Questions
    5. 5. Functions
      1. 5.1. Function Declarations
      2. 5.2. Overloading Functions
        1. Function Call Resolution
        2. Exercises: Overloading Functions
      3. 5.3. Optional Arguments
      4. 5.4. Operator Overloading
        1. Exercises: Operator Overloading
      5. 5.5. Parameter Passing by Value
      6. 5.6. Parameter Passing by Reference
      7. 5.7. References to const
      8. 5.8. Function Return Values
      9. 5.9. Returning References from Functions
      10. 5.10. Overloading on const-ness
        1. Exercises: Overloading on const-ness
      11. 5.11. Inline Functions
      12. 5.12. Inlining versus Macro Expansion
        1. Exercises: Encryption
      13. Review Questions
    6. 6. Inheritance and Polymorphism
      1. 6.1. Simple Derivation
        1. Member Initialization for Base Classes
        2. Extending
        3. 6.1.1. Inheritance Client Code Example
      2. 6.2. Derivation with Polymorphism
        1. Exercise: Derivation with Polymorphism
      3. 6.3. Derivation from an Abstract Base Class
        1. Back to Programming
      4. 6.4. Inheritance Design
      5. 6.5. Overloading, Hiding, and Overriding
        1. Function Hiding
      6. 6.6. Constructors, Destructors, and Copy Assignment Operators
        1. Constructors
        2. Order of Initialization
        3. Copy Assignment Operators
        4. Copy Constructors
        5. Destructors
      7. 6.7. Processing Command-Line Arguments
        1. 6.7.1. Derivation and ArgumentList
        2. Exercise: Inheritance and Polymorphism
      8. Points of Departure
      9. Review Questions
  8. II. Higher-Level Programming
    1. 7. Libraries
      1. 7.1. Code Containers
      2. 7.2. Reusing Other Libraries
      3. 7.3. Organizing Libraries: Dependency Management
        1. 7.3.1. Installing Libraries
      4. 7.4. Installing Libraries: A Lab Exercise
        1. 7.4.1. Fixing the Linker Path
      5. 7.5. Frameworks and Components
      6. Review Questions
    2. 8. Introduction to Design Patterns
      1. 8.1. Iteration and the Visitor Pattern
        1. 8.1.1. Directories and Files: QDir and QFileInfo
        2. 8.1.2. Visitor Pattern
        3. 8.1.3. Customizing the Visitor Using Inheritance
          1. Decoupling
        4. Exercises: Iteration and the Visitor Pattern
      2. Review Questions
    3. 9. QObject
      1. 9.1. QObject’s Child Managment
        1. Exercise: QObject’s Child Managment
      2. 9.2. Composite Pattern: Parents and Children
        1. 9.2.1. Finding Children
      3. 9.3. QApplication and the Event Loop
        1. 9.3.1. Layouts: A First Look
        2. 9.3.2. Connecting to Slots
        3. 9.3.3. Signals and Slots
        4. Exercises: Signals and Slots
      4. 9.4. Q_OBJECT and moc: A Checklist
      5. 9.5. Values and Objects
        1. Exercises: QObject
      6. 9.6. tr() and Internationalization
      7. Point of Departure
      8. Review Questions
    4. 10. Generics and Containers
      1. 10.1. Generics and Templates
        1. 10.1.1. Function Templates
        2. Exercises: Function Templates
        3. 10.1.2. Class Templates
        4. Exercises: Class Templates
      2. 10.2. Containers
      3. 10.3. Managed Containers, Composites, and Aggregates
        1. Exercise: Managed Containers, Compositions and Aggregates
      4. 10.4. Implicitly Shared Classes
      5. 10.5. Generics, Algorithms, and Operators
        1. Exercises: Generics, Algorithms, and Operators
      6. 10.6. Serializer Pattern
        1. 10.6.1. Benefits of the Serializer
        2. Exercises: Serializer Pattern
      7. 10.7. Sorted Map Example
        1. Exercise: QSets and QMaps
      8. Review Questions
    5. 11. Qt GUI Widgets
      1. 11.1. Widget Categories
      2. 11.2. QMainWindow and QSettings
        1. 11.2.1. QSettings: Saving and Restoring Application State
      3. 11.3. Dialogs
        1. 11.3.1. Input Dialogs and Widgets
        2. Exercise: Dialogs
      4. 11.4. Images and Resources
      5. 11.5. Layout of Widgets
        1. 11.5.1. Spacing, Stretching, and Struts
        2. 11.5.2. Moving Widgets across Layouts
        3. Exercises: Layout of Widgets
      6. 11.6. QActions, QMenus, and QMenuBars
      7. 11.7. QActions, QToolbars, and QActionGroups
        1. 11.7.1. The Command Pattern
        2. Exercises: QActions, QMenus, and QMenuBars
        3. Exercise: Card Game GUI
          1. 11.7.1.1. Design Suggestions
      8. 11.8. Regions and QDockWidgets
      9. 11.9. Views of a QStringList
        1. Exercise: Views of a QStringList
      10. Points of Departure
      11. Review Questions
    6. 12. Concurrency
      1. 12.1. QProcess and Process Control
        1. 12.1.1. Processes and Environment
        2. 12.1.2. Qonsole: Writing an Xterm in Qt
        3. 12.1.3. Qonsole with Keyboard Events
        4. Exercises: QProcess and Process Control
      2. 12.2. Threads and QThread
        1. 12.2.1. QPixmap and QThread Animation Example: Movie Player
        2. 12.2.2. Movie Player with QTimer
        3. 12.2.3. Multiple Threads, Queues, and Loggers Example: Giant
        4. 12.2.4. Thread Safety and QObjects
        5. Exercise: Threads and QThread
      3. 12.3. Summary: QProcess and QThread
      4. Review Questions
    7. 13. Validation and Regular Expressions
      1. 13.1. Validators
      2. 13.2. Regular Expressions
        1. 13.2.1. Regular Expression Syntax
        2. 13.2.2. Regular Expressions: Phone Number Recognition
          1. The Problem
        3. Exercises: Regular Expressions: Phone Number Recognition
      3. 13.3. Regular Expression Validation
        1. Exercises: Validation and Regular Expressions
      4. Review Questions
    8. 14. Parsing XML
      1. 14.1. The Qt XML Module
      2. 14.2. Event-Driven Parsing
      3. 14.3. XML, Tree Structures, and DOM
        1. 14.3.1. Visitor Pattern: DOM Tree Walking
        2. 14.3.2. Generation of XML with DOM
        3. Exercise: XML, Tree Structures, and DOM
      4. Review Questions
    9. 15. Meta Objects, Properties, and Reflective Programming
      1. 15.1. Anti-patterns
      2. 15.2. QMetaObject: The MetaObject Pattern
      3. 15.3. Type Identification and qobject_cast
      4. 15.4. Q_PROPERTY Macro: Describing QObject Properties
      5. 15.5. QVariant Class: Accessing Properties
      6. 15.6. DataObject: An Extension of QObject
        1. Exercises: Meta Objects, Properties, and Reflective Programming
      7. 15.7. Property Containers: PropsMap
      8. Review Questions
    10. 16. More Design Patterns
      1. 16.1. Creational Patterns
        1. 16.1.1. Abstract Factory
        2. 16.1.2. Abstract Factories and Libraries
        3. 16.1.3. qApp and Singleton Pattern
        4. 16.1.4. Creation Rules and friend Functions (What Friends Are Really For)
        5. 16.1.5. Benefits of Using Factories
          1. Polymorphism from Constructors
        6. Exercises: Creational Patterns
      2. 16.2. Serializer Pattern Revisited
        1. 16.2.1. Exporting to XML
        2. 16.2.2. Importing Objects with an Abstract Factory
      3. 16.3. The Façade Pattern
        1. 16.3.1. Functional Façade
        2. 16.3.2. Smart Pointers: auto_ptr
        3. 16.3.3. FileTagger: Façade Example
        4. Exercises: The Façade Pattern
      4. Points of Departure
      5. Review Questions
    11. 17. Models and Views
      1. 17.1. M-V-C: What about the Controller?
      2. 17.2. Dynamic Form Models
        1. Exercises: Dynamic Form Models
        2. 17.2.1. Form Models
        3. 17.2.2. Form Views
        4. 17.2.3. Unforseen Types
        5. 17.2.4. Controlling Actions
        6. 17.2.5. DataObject Form Model
        7. Exercises: DataObject Form Model
      3. 17.3. Qt 4 Models and Views
        1. Views
        2. Model Index
      4. 17.4. Table Models
        1. But How Does It Work?
      5. 17.5. Tree Models
        1. 17.5.1. Extended Tree Widget Items
        2. Exercises: Tree Models
      6. Review Questions
    12. 18. Qt SQL Classes
      1. 18.1. Introduction to MySQL
        1. Connecting to MySQL from Qt
        2. Defining a Table
        3. Inserting Rows
      2. 18.2. Queries and Result Sets
      3. 18.3. Database Models
      4. Review Questions
  9. III. C++ Language Reference
    1. 19. Types and Expressions
      1. 19.1. Operators
        1. Operator Characteristics
      2. 19.2. Evaluation of Logical Expressions
      3. 19.3. Enumerations
      4. 19.4. Signed and Unsigned Integral Types
        1. 8-Bit Integer Example
        2. Exercises: Signed and Unsigned Integral Types
      5. 19.5. Standard Expression Conversions
        1. Automatic Expression Conversion Rules for x op y
        2. Exercise: Standard Expression Conversions
      6. 19.6. Explicit Conversions
      7. 19.7. Safer Typecasting Using ANSI C++ Typecasts
        1. 19.7.1. static_cast and const_cast
          1. Casting Away const
        2. Exercises: static_cast and const_cast
        3. 19.7.2. reinterpret_cast
          1. What Is It Really Used For?
        4. 19.7.3. Why Not Use C-style Casts?
      8. 19.8. Run-Time Type Identification (RTTI)
        1. 19.8.1. typeid Operator
      9. 19.9. Member Selection Operators
      10. Point of Departure
        1. Exercises: Types and Expressions
      11. Review Questions
    2. 20. Scope and Storage Class
      1. 20.1. Declarations and Definitions
      2. 20.2. Identifier Scope
        1. 20.2.1. Default Scope of Identifiers: A Summary
        2. 20.2.2. File Scope versus Block Scope and operator::
        3. Exercise: File Scope Versus Block Scope and Operator::
      3. 20.3. Storage Class
        1. 20.3.1. Globals, statics, and QObject
          1. 20.3.1.1. Globals and const
        2. Exercise: Storage Class
      4. 20.4. Namespaces
        1. 20.4.1. Anonymous Namespaces
        2. 20.4.2. Open Namespaces
        3. 20.4.3. Namespace, static Objects and extern
      5. Review Questions
    3. 21. Statements and Control Structures
      1. 21.1. Statements
      2. 21.2. Selection Statements
        1. Exercise: Selection Statements
      3. 21.3. Iteration
        1. Exercises: Iteration
      4. 21.4. Exceptions
        1. 21.4.1. Exception Handling
        2. 21.4.2. Exception Types
        3. 21.4.3. throwing Things Around
          1. throw() in a Function Signature
        4. 21.4.4. try and catch
        5. Exercise: Try/Catch
        6. 21.4.5. More about throw
        7. 21.4.6. Rethrown Exceptions
        8. 21.4.7. Exception Expressions
        9. Exercises: Exceptions
      5. Review Questions
    4. 22. Memory Access
      1. 22.1. Pointer Pathology
      2. 22.2. Further Pointer Pathology with Heap Memory
      3. 22.3. Memory Access Summary
      4. 22.4. Introduction to Arrays
      5. 22.5. Pointer Arithmetic
      6. 22.6. Arrays, Functions, and Return Values
      7. 22.7. Different Kinds of Arrays
      8. 22.8. Valid Pointer Operations
      9. 22.9. What Happens If new Fails?
        1. 22.9.1. set_new_handler(): Another Approach to New Failures
        2. Exercise: set_new_handler()—Another Approach to new Failures
        3. 22.9.2. Using set_new_handler and bad_alloc
        4. 22.9.3. Checking for null: The Updated Way to Test for New Failures
      10. 22.10. Chapter Summary
      11. Review Questions
    5. 23. Inheritance in Detail
      1. 23.1. Virtual Pointers and Virtual Tables
      2. 23.2. Polymorphism and virtual Destructors
      3. 23.3. Multiple Inheritance
        1. 23.3.1. Multiple Inheritance Syntax
          1. Member Initialization
        2. 23.3.2. Multiple Inheritance with Abstract Interfaces
      4. Point of Departure
        1. 23.3.3. Resolving Multiple Inheritance Conflicts
          1. 23.3.3.1. virtual Inheritance
          2. 23.3.3.2. virtual Base Classes
      5. 23.4. public, protected, and private Derivation
      6. Review Questions
    6. 24. Miscellaneous Topics
      1. 24.1. Functions with Variable-Length Argument Lists
      2. 24.2. Resource Sharing
        1. Exercises: Resource Sharing
  10. IV. Programming Assignments
    1. 25. MP3 Jukebox Assignments
      1. 25.1. Data Model: Mp3File
        1. The Assignment
      2. 25.2. Visitor: Generating Playlists
      3. 25.3. Preference: An Enumerated Type
        1. The Assignment
      4. 25.4. Reusing id3lib
        1. The Assignment
      5. 25.5. PlayListModel Serialization
      6. 25.6. Testing Mp3File Related Classes
      7. 25.7. Simple Queries and Filters
      8. 25.8. Mp3PlayerView
      9. 25.9. Models and Views: PlayList
      10. 25.10. Source Selector
      11. 25.11. Persistent Settings
      12. 25.12. Edit Form View for FileTagger
      13. 25.13. Database View
      14. Points of Departure
  11. V. Appendices
    1. A. C++ Reserved Keywords
    2. B. Standard Headers
    3. C. The Development Environment
      1. C.1. The Preprocessor: For #including Files
      2. C.2. Understanding the Linker
        1. C.2.1. Common Linker Error Messages
          1. C.2.1.1. Unable to Find libxxx.so.x
          2. C.2.1.2. Undefined Reference to [identifier]
          3. C.2.1.3. Undefined Reference to vtable for ClassName
      3. C.3. Debugging
        1. C.3.1. Building a Debuggable Target
        2. Exercise: Building a Debuggable Target
        3. C.3.2. gdb Quickstart
        4. C.3.3. Finding Memory Errors
      4. C.4. Qt Assistant and Designer
      5. C.5. Open-Source IDEs and Development Tools
        1. C.5.1. UML Modeling Tools
        2. C.5.2. jEdit
  12. Bibliography
    1. C++ References
    2. Qt References
    3. OOP References
    4. Docbook References
    5. Miscellaneous References