Advanced Qt Programming
Total Page:16
File Type:pdf, Size:1020Kb
ptg From the Library of Wow! eBook Advanced Qt Programming ptg From the Library of Wow! eBook This page intentionally left blank ptg From the Library of Wow! eBook Advanced Qt Programming Creating Great Software with C+ + and Qt 4 Mark Summerfield ptg Upper Saddle River, NJ ·Boston ·Indianapolis ·San Francisco p New York ·Toronto ·Montreal ·London ·Munich ·Paris ·Madrid p Capetown ·Sydney ·Tokyo ·Singapore ·Mexico City From the Library of Wow! eBook 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 author 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 [email protected] For sales outside the United States, please contact: International Sales [email protected] Visit us on the Web: informit.com/aw Library of Congress Cataloging-in-Publication Data Summerfield, Mark. Advanced Qt programming : creating great software with C++ and Qt 4 / Mark Summerfield. p.mcm. Includes bibliographical references and index. ptg ISBN 978-0-321-63590-7 (hardcover : alk. paper) 1. Qt (Electronic resource) 2. Graphical user interfaces (Computer systems) 3. C++ (Computer program language) I. Title. QA76.9.U83S88 2010 005.1’13—dc22 2010019289 Copyright © 2011 Qtrac Ltd. All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, write to: Pearson Education, Inc. Rights and Contracts Department 501 Boylston Street, Suite 900 Boston, MA 02116 Fax: (617) 671-3447 ISBN-13: 978-0-321-63590-7 ISBN-10: 0-321-63590-6 Text printed in the United States on recycled paper at Courier in Westford, Massachusetts. First printing, July 2010 From the Library of Wow! eBook This book is dedicated to Anna Rebecca Paterson ptg From the Library of Wow! eBook This page intentionally left blank ptg From the Library of Wow! eBook Contents at a Glance List of Tables . ........................................................................................... xiii Foreword . .................................................................................................. xv Introduction . ............................................................................................. 1 Chapter 1. Hybrid Desktop/Internet Applications ............................... 5 Chapter 2. Audio and Video ................................................................. 53 Chapter 3. Model/View Table Models ................................................. 87 Chapter 4. Model/View Tree Models .................................................... 129 Chapter 5. Model/View Delegates . ...................................................... 185 Chapter 6. Model/View Views .............................................................. 207 Chapter 7. Threading with QtConcurrent . ............................................ 245 ptg Chapter 8. Threading with QThread . .................................................... 287 Chapter 9. Creating Rich Text Editors .................................................... 317 Chapter 10. Creating Rich Text Documents . ....................................... 359 Chapter 11. Creating Graphics/View Windows .................................... 389 Chapter 12. Creating Graphics/View Scenes . .................................... 409 Chapter 13. The Animation and State Machine Frameworks ............. 469 Epilogue . .................................................................................................. 491 Selected Bibliography . ........................................................................... 495 Index . ........................................................................................................ 499 www.qtrac.eu/aqpbook.html vii From the Library of Wow! eBook This page intentionally left blank ptg From the Library of Wow! eBook Contents List of Tables . ........................................................................................... xiii Foreword . .................................................................................................. xv Introduction . ............................................................................................. 1 Acknowledgements . 3 Chapter 1. Hybrid Desktop/Internet Applications . 5 Internet-Aware Widgets . 6 Using WebKit . ..................................................................................... 21 A Generic Web Browser Window Component . .................... 22 ptg Creating Web Site-Specific Applications . ............................... 30 Embedding Qt Widgets in Web Pages .................................... 44 Chapter 2. Audio and Video ................................................................. 53 Using QSound and QMovie .............................................................. 54 The Phonon Multimedia Framework . .............................................. 60 Playing Music . ........................................................................... 64 Playing Videos ........................................................................... 80 Chapter 3. Model/View Table Models . ................................................. 87 Qt’s Model/View Architecture . ...................................................... 88 Using QStandardItemModels for Tables . ....................................... 90 Changing a Table Model through the User Interface . .......... 91 A QStandardItemModel Subclass for Tables .......................... 102 A QSortFilterProxyModel to Filter Out Duplicate Rows . .......... 107 A QSortFilterProxyModel to Filter In Wanted Rows .................. 109 Creating Custom Table Models . 113 Changing a Table Model through the User Interface . 113 A Custom QAbstractTableModel Subclass for Tables . 116 The QAbstractItemModel API Methods for Tables . 117 Methods to Support Saving and Loading Table Items . 126 From the Library of Wow! eBook ix Chapter 4. Model/View Tree Models .................................................... 129 Using QStandardItemModels for Trees ............................................ 130 Changing a Tree Model through the User Interface . ............. 131 A QStandardItem Subclass for Tree Items ............................... 141 A QStandardItemModel Subclass for Trees . .......................... 143 Creating Custom Tree Models ......................................................... 151 Changing a Tree Model through the User Interface . 152 A Custom Item Class for Tree Items . 155 A Custom QAbstractItemModel Subclass for Trees . 158 The QAbstractItemModel API for Trees . 160 The QAbstractItemModel API for Drag and Drop . 168 Methods for Saving and Loading Tree Items . 180 Chapter 5. Model/View Delegates . 185 Datatype-Specific Editors . 186 Datatype-Specific Delegates . ptg 188 A Read–Only Column or Row Delegate . 188 An Editable Column or Row Delegate . 193 Model-Specific Delegates . .............................................................. 201 Chapter 6. Model/View Views . .............................................................. 207 QAbstractItemView Subclasses . .................................................... 208 Model-Specific Visualizing Views . ................................................... 224 The Visualizer Widget . .............................................................. 225 The Visualizer’s Aggregated Header Widget . ....................... 232 The Visualizer’s Aggregated View Widget . ............................ 235 Chapter 7. Threading with QtConcurrent . ............................................ 245 Executing Functions in Threads . 248 Using QtConcurrent::run() . 252 Using QRunnable . 257 Filtering and Mapping in Threads .................................................... 261 Using QtConcurrent to Filter . .................................................... 270 Using QtConcurrent to Filter and Reduce ............................... 277 Using QtConcurrent to Map .................................................... 281 Chapter 8. Threading with QThread ...................................................... From the Library of 287Wow! eBook Processing Independent Items . ...................................................... 287 Processing Shared Items ................................................................... 302 x Chapter 9. Creating Rich Text Editors . ................................................... 317 Introducing QTextDocument . ......................................................... 318 Creating Custom Text Editors . ........................................................