An introduction to design patterns in C++ with Qt

5 downloads 260 Views 327KB Size Report
Preface xix. Preface to the Second Edition xxii. Acknowledgments xxv. About the Authors xxx. Part I: Design Patterns and Qt 4. 1. Chapter 1: C++ Introduction. 3.
Design

An Introduction to

Patterns in C++ with

Qt,

2nd Edition

Alan Ezust and Paul Ezust

PRENTICE

HALL

Upper Saddle River, NJ New York



Boston

Montreal

Toronto





Indianapolis

London

Cape Town Sydney Tokyo Singapore •



San Francisco

Munich •

Mexico



Paris

City



Madrid

Contents

Foreword

xv

Preface

Preface

xix to

the Second Edition

Acknowledgments

xxv

About the Authors

Part I:

Design

Chapter

Patterns and

xxii

xxx

Qt 4

1: C++ Introduction

1 3

1.1

Overview of C++

3

1.2

A Brief History of C++

4

1.3

C++ First

5

1.4

Standard

1.5

Introduction

1.6

qmake,

1.7

Getting Help Online

24

1.8

Strings

24

1.9

Streams

26

1.10

File Streams

29

1.11

Qt Dialogs for

1.12

Identifiers, Types, and Literals

37

1.13

C++

Simple Types

40

1.14

The

Keyword

52

1.15

Pointers and

Example and

Input to

Output

Functions

Project Files, and

User

Makefile

Input/Output

const

Memory Access

8 11 17

34

54

vii

viii

An Introduction

to

Design

Patterns in C++ with

1.16

Reference Variables

1.17

const*

1.18

Review

Chapter

2:

and

Qt"1, 2nd Edition

60 61

*const

64

Questions

Top of the

67

class

2.1

First, There Was

2.2

Class Definitions

69

2.3

Member Access

71

2.4

Encapsulation

2.5

Introduction

2.6

Friends of

2.7

Constructors

77

2.8

Destructors

80

2.9

The

2.10

Class Declarations and Definitions

86

2.11

Copy Constructors

88

2.12

Conversions

2.13

const

2.14

Subobjects

2.15

Exercise:

2.16

Review

Chapter 3:

a

67

struct

Specifiers

74 UML

to

75

Class

Keyword

76

81

static

and

Assignment Operators

92

Member Functions

95 97

Classes

98

Questions

Introduction

108 to

Qt

113

3.1

Style Guidelines, Naming Conventions

114

3.2

The

116

3.3

QtCreator—An IDE for Qt

3.4

Exercises: Introduction

3.5

Review

Chapter

Qt Core Module

to

Qt

Questions

4: Lists

Programming

119 120 121 122

4.1

Introduction

4.2

Iterators

123

4.3

Relationships

129

to

Containers

122

Contents

4.4

Exercise:

4.5

Review

Relationships

ix

132

Questions

134

Chapter 5: Functions

135

5.1

Overloading Functions

135

5.2

Optional Arguments

138

5.3

Operator Overloading

141

5.4

Parameter

Passing by Value

146

5.5

Parameter

Passing by

148

5.6

References

to const

5.7

Function

5.8

Returning References from

5.9

Overloading

5.10

inline

Return

on

152

Values

154 Functions

Functions with

5.12

Exercise:

5.13

Review

155

const

158

Variable-Length Argument Lists

Encryption

Questions

167

6: Inheritance and

Polymorphism

Simple

6.2

Derivation with

Polymorphism

6.3

Derivation from

an

6.4

Inheritance

6.6

162 164

6.1

6.5

154

Functions

5.11

Chapter

Reference

168

Derivation

168 177

Abstract Base Class

Design Overloading, Hiding,

184 189

and

Overriding

191

Constructors, Destructors, and Copy Assignment Operators

193

6.7

Processing

6.8

Containers

6.9

Managed Containers, Composites,

6.10

Containers of Pointers

210

6.11

Review

228

Command-Line

Arguments

198 204

and

Questions

Chapter 7: Libraries and Design

Patterns

Aggregates

206

234

7.1

Building and Reusing Libraries

236

7.2

Exercise:

244

Installing

Libraries

x

An Introduction

Design

to

Patterns in C++ with

Qt'",

2nd Edition

7.3

Frameworks and

7.4

Design Patterns

248

7.5

Review

257

247

Components

Questions

Chapter 8: QObject, QApplication, Signals, and Slots

259

8.1

Values and

Objects

263

8.2

Composite

Pattern: Parents and Children

264

8.3

QApplication and the Event

8.4

q_object and

8.5

Signals

8.6

QObject

8.7

QTestLib

8.8

Exercises: QObject, QApplication,

8.9

Review

moc:

A

272

Loop

checklist

274

and Slots

275

Lifecycle

277 277

Signals, and

Slots

Questions

282 282

Chapter 9: Widgets and Designer

283

9.1

284

Widget Categories

9.2

Designer

9.3

Dialogs

9.4

Form

Introduction

286 290

Layout

292 and Resources

9.5

Icons,

9.6

Layout of Widgets

298

9.7

Designer Integration with Code

306

9.8

Exercise:

Forms

313

9.9

The Event

Loop: Revisited

314

9.10

Paint Events,

9.11

Review

Chapter

Images,

Input

295

Drawing Images

323

Questions

325

10: Main Windows and Actions

10.1

QActions, QMenus, and QMenuBars

10.2

Regions

and QDockWidgets

10.3

QSettings:

10.4

Clipboard

Saving

and

327 337

Restoring Application

and Data Transfer

326

Operations

State

339

341

Contents

xi

10.5

The Command Pattern

343

10.6

tr() and

351

10.7

Exercises: Main Windows and Actions

352

10.8

Review

Questions

353

Chapter 11: Generics and Containers

355

11.1

Generics and

355

11.2

Generics,

11.3

Sorted

11.4

Function Pointers and Functors

11.5

Flyweight

11.6

Exercise: Generics

375

11.7

Review

376

Chapter 12.1

Internationalization

Templates

Algorithms,

and

362

Operators

Map Example Pattern:

365 368

Implicitly Shared Classes

371

Questions

12: Meta

Objects, Properties,

QMetaObject—The

MetaObject

and Reflective

Programming

Pattern

Identification and qobject_cast

12.2

Type

12.3

Q_PROPERTY

12.4

QVariant Class:

12.5

Dynamic Properties

Macro—Describing QObject Properties Accessing Properties

378 378 380 383

386 389

12.6

MetaTypes, Declaring,

12.7

invokeMethod ()

396

12.8

Exercises: Reflection

397

12.9

Review

397

Chapter

and

Registering

Questions

13: Models and Views

394

399

13.1

Model-View-Controller (MVC)

400

13.2

Qt Models and Views

402

13.3

Table Models

414

13.4

Tree Models

424

13.5

Smarter Pointers

429

13.6

Exercises: Models and Views

431

13.7

Review

433

Questions

xii

An Introduction

Chapter

Design

to

Patterns in C++ with

14: Validation and

Qt,

2nd Edition

Regular Expressions

434

14.1

Input Masks

435

14.2

Validators

438

14.3

Regular Expressions

440

14.4

Regular Expression

14.5

Subclassing

14.6

Exercises: Validation and

14.7

Review

Chapter

15:

Validation

449

451

QValidator

Regular Expressions

454

Questions

456

Parsing XML

457 460

15.1

The

Qt XML Parsers

15.2

SAX

Parsing

15.3

XML, Tree

15.4

XML Streams

476

15.5

Review

479

Chapter

462

Structures, and DOM

466

Questions

16: More

Design

Patterns

481

16.1

Creational Patterns

481

16.2

Memento Pattern

491

16.3

Facade Pattern

498

16.4

Review

Questions

505

Concurrency

506

Chapter

17:

17.1

QProcess and Process Control

17.2

QThread

17.3

Exercises: QThread and QtConcurrent

536

17.4

Review

537

Chapter

and

506 522

QtConcurrent

Questions

18: Database

Programming

539

18.1

QSqlDatabase:

18.2

Queries and Result Sets

546

18.3

Database Models

548

18.4

Review

549

Connecting to SQL

Questions

from

Qt

541

Contents

Part II: C++

Chapter

xiii

Language Reference

550

19: Types and Expressions



552

19.1

Operators

553

19.2

Statements and Control Structures

557

19.3

Evaluation of Logical

564

19.4

Enumerations

565

19.5

Signed and Unsigned Integral Types

567

19.6

Standard

570

19.7

Explicit Conversions

19.8

Safer Typecasting

19.9

Overloading Special Operators

19.10 Runtime

Using ANSI

19.13 Review

Chapter

20:

574 C++ Typecasts

Types

587

Operators

and

574 581

Identification

19.11 Member Selection 19.12 Exercises:

Conversions

Expression

Type

Expressions

590

Expressions

Questions

592 594

Scope and Storage Class

595

20.1

Declarations and Definitions

595

20.2

Identifier

597

20.3

Storage

20.4

Namespaces

610

20.5

Review

615

Chapter

21:

Scope

Class

606

Questions

Memory Access

617

Pathology

21.1

Pointer

21.2

Further Pointer Pathology with

21.3

Memory Access Summary

623

21.4

Introduction

624

21.5

Pointer Arithmetic

626

21.6

Arrays, Functions, and Return Values

627

21.7

Different Kinds of Arrays

629

to

618

Heap Memory

Arrays

620

xiv

An Introduction

to

Design

Patterns in C++ with Qt, 2nd Edition

21.8

Valid Pointer

Operations

629

21.9

Arrays and Memory: Important Points

632

21.10 Exercises: 21.11

Review

Chapter 22:

633

Memory Access

634

Questions

Inheritance in Detail

Pointers,

635 Tables

635

22.1

virtual

22.2

Polymorphism and

22.3

Multiple

22.4

public, protected, and private Derivation

22.5

Review

Part III:

virtual

virtual

Destructors

638

Inheritance

642

650 652

Questions

653

Programming Assignments

Chapter 23: MP3 Jukebox Assignments

655

23.1

Phonon/MultiMediaKit

657

23.2

Pkylist

657

23.3

Playlists

658

23.4

Source Selector

659

23.5

Database

23.6

Star

23.7

Sorting, Filtering, and Editing Playlists

Setup

Playlists

661 661

Delegates

661

Appendix A:

C++ Reserved Keywords

662

Appendix B:

Standard Headers

664

Appendix C: Development Tools Appendix D: Alan's Quick Start Guide

666 to

Debian for

Programmers

691

Appendix E: C++/Qt Setup

698

Bibliography

706

Index

709