Table of Contents

[Pages:588] Rapid GUI Programming with Python and Qt: The Definitive Guide to PyQt Programming

Table of Contents

Copyright..................................................................................................... 1 About the Author........................................................................................ 2 Production.................................................................................................. 2 Introduction............................................................................................... 3

The Structure of the Book............................................................................................................................................................... 5 Acknowledgements......................................................................................................................................................................... 7

Python Programming.................................................................................. 8

Data Types and Data Structures..................................................................................................................................................... 8 Executing Python Code............................................................................................................................................................... 8 Variables and Objects................................................................................................................................................................ 12 Numbers and Strings................................................................................................................................................................. 16 Collections................................................................................................................................................................................. 32 Built-in Functions..................................................................................................................................................................... 42 Summary.................................................................................................................................................................................... 45 Exercises.................................................................................................................................................................................... 46

Control Structures......................................................................................................................................................................... 48 Conditional Branching.............................................................................................................................................................. 49 Looping...................................................................................................................................................................................... 52 Functions................................................................................................................................................................................... 58 Exception Handling................................................................................................................................................................... 70 Summary.................................................................................................................................................................................... 76 Exercises.................................................................................................................................................................................... 77

Classes and Modules..................................................................................................................................................................... 80 Creating Instances..................................................................................................................................................................... 82 Methods and Special Methods.................................................................................................................................................. 84 Inheritance and Polymorphism............................................................................................................................................... 105 Modules and Multi-File Applications....................................................................................................................................... 111 Summary.................................................................................................................................................................................. 114 Exercises................................................................................................................................................................................... 115

Basic GUI Programming.......................................................................... 116

Introduction to GUI Programming............................................................................................................................................. 116 A Pop-up Alert in 25 Lines....................................................................................................................................................... 117 An Expression Evaluator in 30 Lines...................................................................................................................................... 122 A Currency Converter in 70 Lines........................................................................................................................................... 129 Signals and Slots...................................................................................................................................................................... 134 Summary.................................................................................................................................................................................. 144 Exercise.................................................................................................................................................................................... 145

Dialogs......................................................................................................................................................................................... 145 Dumb Dialogs........................................................................................................................................................................... 147 Standard Dialogs...................................................................................................................................................................... 154 Smart Dialogs........................................................................................................................................................................... 161 Summary.................................................................................................................................................................................. 169 Exercise.................................................................................................................................................................................... 170

Main Windows............................................................................................................................................................................. 172 Creating a Main Window......................................................................................................................................................... 174 Handling User Actions............................................................................................................................................................ 201 Summary.................................................................................................................................................................................. 212 Exercise.................................................................................................................................................................................... 214

Using Qt Designer........................................................................................................................................................................ 215 Designing User Interfaces........................................................................................................................................................ 217 Implementing Dialogs............................................................................................................................................................. 228 Testing Dialogs........................................................................................................................................................................ 233 Summary................................................................................................................................................................................. 234 Exercise.................................................................................................................................................................................... 235

Data Handling and Custom File Formats................................................................................................................................... 236

Rapid GUI Programming with Python and Qt: The Definitive Guide to PyQt Programming

Rapid GUI Programming with Python and Qt: The Definitive Guide to PyQt Programming

Prepared for Paul Waddell, Safari ID: pwaddell@u.washington.edu

By Mark Summerfield ISBN: 9780132354189 Publisher: Prentice Hall

Print Publication Date: 2007/10/19

User number: 905221 Copyright 2007, Safari Books Online, LLC.

This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior

written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that

otherwise violates the Safari Terms of Service is strictly prohibited.

Rapid GUI Programming with Python and Qt: The Definitive Guide to PyQt Programming

Main Window Responsibilities............................................................................................................................................... 239 Data Container Responsibilities............................................................................................................................................. 244 Saving and Loading Binary Files............................................................................................................................................ 250 Saving and Loading Text Files................................................................................................................................................ 258 Saving and Loading XML Files............................................................................................................................................... 266 Summary.................................................................................................................................................................................. 275 Exercise.................................................................................................................................................................................... 276

Intermediate GUI Programming............................................................. 276

Layouts and Multiple Documents............................................................................................................................................... 277 Layout Policies......................................................................................................................................................................... 278 Tab Widgets and Stacked Widgets.......................................................................................................................................... 279 Splitters................................................................................................................................................................................... 288 Single Document Interface (SDI)............................................................................................................................................ 291 Multiple Document Interface (MDI)...................................................................................................................................... 298 Summary................................................................................................................................................................................. 308 Exercise................................................................................................................................................................................... 309

Events, the Clipboard, and Drag & Drop.................................................................................................................................... 310 The Event Handling Mechanism............................................................................................................................................. 310 Reimplementing Event Handlers............................................................................................................................................ 312 Using the Clipboard................................................................................................................................................................. 318 Drag and Drop......................................................................................................................................................................... 319 Summary................................................................................................................................................................................. 325 Exercise.................................................................................................................................................................................... 326

Custom Widgets.......................................................................................................................................................................... 327 Using Widget Style Sheets...................................................................................................................................................... 328 Creating Composite Widgets................................................................................................................................................... 331 Subclassing Built-in Widgets.................................................................................................................................................. 333 Subclassing QWidget............................................................................................................................................................... 335 Summary.................................................................................................................................................................................. 351 Exercise.................................................................................................................................................................................... 352

Item-Based Graphics................................................................................................................................................................... 353 Custom and Interactive Graphics Items................................................................................................................................. 355 Animation and Complex Shapes............................................................................................................................................. 373 Summary................................................................................................................................................................................. 382 Exercise................................................................................................................................................................................... 384

Rich Text and Printing................................................................................................................................................................ 384 Rich Text Editing..................................................................................................................................................................... 386 Printing Documents................................................................................................................................................................ 402 Summary.................................................................................................................................................................................. 415 Exercise.................................................................................................................................................................................... 416

Model/View Programming.......................................................................................................................................................... 417 Using the Convenience Item Widgets..................................................................................................................................... 419 Creating Custom Models......................................................................................................................................................... 427 Creating Custom Delegates..................................................................................................................................................... 439 Summary................................................................................................................................................................................. 446 Exercise.................................................................................................................................................................................... 447

Databases.................................................................................................................................................................................... 447 Connecting to the Database.................................................................................................................................................... 448 Executing SQL Queries........................................................................................................................................................... 449 Using Database Form Views................................................................................................................................................... 454 Using Database Table Views................................................................................................................................................... 460 Summary.................................................................................................................................................................................. 473 Exercise.................................................................................................................................................................................... 474

Advanced GUI Programming................................................................... 475

Advanced Model/View Programming........................................................................................................................................ 475 Custom Views.......................................................................................................................................................................... 476 Generic Delegates.................................................................................................................................................................... 483 Representing Tabular Data in Trees....................................................................................................................................... 492 Summary................................................................................................................................................................................. 506 Exercise................................................................................................................................................................................... 506

Rapid GUI Programming with Python and Qt: The Definitive Guide to PyQt Programming

Rapid GUI Programming with Python and Qt: The Definitive Guide to PyQt Programming

Prepared for Paul Waddell, Safari ID: pwaddell@u.washington.edu

By Mark Summerfield ISBN: 9780132354189 Publisher: Prentice Hall

Print Publication Date: 2007/10/19

User number: 905221 Copyright 2007, Safari Books Online, LLC.

This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior

written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that

otherwise violates the Safari Terms of Service is strictly prohibited.

Rapid GUI Programming with Python and Qt: The Definitive Guide to PyQt Programming

Online Help and Internationalization........................................................................................................................................ 508 Online Help............................................................................................................................................................................. 509 Internationalization................................................................................................................................................................. 512 Summary................................................................................................................................................................................. 520 Exercise.................................................................................................................................................................................... 521

Networking.................................................................................................................................................................................. 521 Creating a TCP Client.............................................................................................................................................................. 523 Creating a TCP Server............................................................................................................................................................. 529 Summary................................................................................................................................................................................. 534 Exercise.................................................................................................................................................................................... 535

Multithreading............................................................................................................................................................................ 536 Creating a Threaded Server..................................................................................................................................................... 538 Creating and Managing Secondary Threads........................................................................................................................... 543 Implementing a Secondary Thread......................................................................................................................................... 552 Summary.................................................................................................................................................................................. 557 Exercise.................................................................................................................................................................................... 558 This Is Not Quite The End....................................................................................................................................................... 559

Installing................................................................................................. 560

Installing on Windows................................................................................................................................................................ 560 Installing on Mac OS X............................................................................................................................................................... 565 Installing on Linux and Unix...................................................................................................................................................... 570

Selected PyQt Widgets............................................................................. 574 Selected PyQt Class Hierarchies.............................................................. 579

Rapid GUI Programming with Python and Qt: The Definitive Guide to PyQt Programming

Rapid GUI Programming with Python and Qt: The Definitive Guide to PyQt Programming

Prepared for Paul Waddell, Safari ID: pwaddell@u.washington.edu

By Mark Summerfield ISBN: 9780132354189 Publisher: Prentice Hall

Print Publication Date: 2007/10/19

User number: 905221 Copyright 2007, Safari Books Online, LLC.

This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior

written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that

otherwise violates the Safari Terms of Service is strictly prohibited.

Rapid GUI Programming with Python and Qt: The Definitive Guide to PyQt Programming Page 1 Return to Table of Contents

Copyright

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.

TCheopupbliyshrerigoffherstexScelalenftadirsciouBntsoonothkissbooOk wnhelninoredere#d in9q0uan5ti2ty f2or1bulk

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 corpsales@

For sales outside the United States, please contact:

International Sales international@

Visit us on the Web:

Library of Congress Cataloging-in-Publication Data

Summerfield, Mark Rapid GUI programming with Python and Qt / Mark Summerfield.

p. cm. Includes bibliographical references and index. ISBN 0-13-235418-7 (pbk.: alk. paper) 1. Graphical user interfaces (Computer systems) 2. C++ (Computer program language) QA76.9.????? 2006 005.4'37--dc22

?????

Copyright ? 2007 Pearson Education, Inc.

All rights reserved. Printed in the United States of America.

Rapid GUI Programming with Python and Qt: The Definitive Guide to PyQt Programming

Rapid GUI Programming with Python and Qt: The Definitive Guide to PyQt Programming

Prepared for Paul Waddell, Safari ID: pwaddell@u.washington.edu

By Mark Summerfield ISBN: 9780132354189 Publisher: Prentice Hall

Print Publication Date: 2007/10/19

User number: 905221 Copyright 2007, Safari Books Online, LLC.

This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior

written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that

otherwise violates the Safari Terms of Service is strictly prohibited.

Rapid GUI Programming with Python and Qt: The Definitive Guide to PyQt Programming Page 2 Return to Table of Contents

Trolltech?, Qt?, Qtopia?, and the Trolltech and Qtopia logos are registered trademarks of Trolltech ASA.

Text printed in the United States on recycled paper at Courier in Stoughton, Massachusetts.

First printing, November 2007

Dedication

This book is dedicated to Andrea Summerfield

About the Author

Mark Summerfield

Mark graduated in computer science with first class honors from the University of Wales Swansea. He followed this with a year's postgraduate research before going into industry. He spent many years working as a software engineer for a variety of firms before joining Trolltech. He spent almost three years as Trolltech's documentation manager, during which he founded and edited Trolltech's technical journal, Qt Quarterly, and co-wrote C ++ GUI Programming with Qt 3, and later C++ GUI Programming with Qt 4. Mark owns Qtrac Ltd., qtrac.eu, where he works as an independent author, editor, trainer, and consultant, specializing in C++, Qt, and Python.

Production

The text was written using gvim and marked up with the Lout typesetting language. The index was compiled by the author, with the assistance of a PyQt program developed for the purpose. All the diagrams were produced using Lout. Almost all of the code snippets were extracted directly from the example programs using Lout in conjunction with a Python script. The icons used in the example programs are mostly from KDE (The "K" Desktop Environment), with a few created by the author. The images used in the book's margins are from the Open Clip Art Library, with some other images coming from Project Gutenberg. SVG images were converted to EPS using Inkscape. The Linux screenshots were taken with KSnapshot, and the Windows screenshots were captured and saved using a tiny PyQt application; in both cases the .png images were converted to .eps using ImageMagick. The monospaced font used for code is derived from Crystal, modified using FontForge. Wikipedia proved itself to be useful in all kinds of ways, including being the source of the flag images, and was frequently referred to for ideas, information, and sample data. The marked-up text was previewed using gv and evince, and converted to PostScript by Lout, then to PDF by Ghostscript.

Rapid GUI Programming with Python and Qt: The Definitive Guide to PyQt Programming

Rapid GUI Programming with Python and Qt: The Definitive Guide to PyQt Programming

Prepared for Paul Waddell, Safari ID: pwaddell@u.washington.edu

By Mark Summerfield ISBN: 9780132354189 Publisher: Prentice Hall

Print Publication Date: 2007/10/19

User number: 905221 Copyright 2007, Safari Books Online, LLC.

This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior

written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that

otherwise violates the Safari Terms of Service is strictly prohibited.

Rapid GUI Programming with Python and Qt: The Definitive Guide to PyQt Programming Page 3 Return to Table of Contents

All the editing and processing was done on Fedora and Kubuntu systems. The cover was provided by the publisher, with the picture suggested by the author in view of the fact that Python is used to calibrate and analyze data from the Hubble Space Telescope. The screenshots were taken on Windows XP, Mac OS X, and Linux/KDE. All the example programs have been tested on Windows, Linux, and Mac OS X, using Python 2.5, Qt 4.2, and PyQt 4.2, and additionally on Linux using Qt 4.3.

Introduction

This book teaches how to write GUI applications using the Python programming language and the Qt application development framework. The only prior knowledge assumed is that you can program in some object-oriented programming language, such as C++, C#, Java, or of course Python itself. A slight familiarity with HTML is also assumed, and some knowledge of regular expresssions would be beneficial. A knowledge of GUI programming is not required since all the key concepts are covered.

The book will be useful to people who program professionally as part of their job, whether as full-time software developers, or those from other disciplines who need to do some programming in support of their work. It is also suitable for undergraduate and postgraduate students, particularly those doing courses or research that includes a substantial computing element. The exercises (with solutions) are provided especially to help students.

Python is probably the easiest to learn and nicest scripting language in widespread use, and Qt is probably the best library for developing GUI applications. The combination of Python and Qt, "PyQt", makes it possible to develop applications on any supported platform and run them unchanged on all the supported platforms, for example, all modern versions of Windows, Linux, Mac OS X, and most Unix-based systems. No compilation is required thanks to Python being interpreted, and no source code changes to adapt to different operating systems are required thanks to Qt abstracting away the platformspecific details. We only have to copy the source file or files to a target machine that has both Python and PyQt installed and the application will run.

If you are new to Python: Welcome! You are about to discover a language that is clear to read and write, and that is concise without being cryptic. Python supports many programming paradigms, but because our focus is on GUI programming, we will take an object-oriented approach everywhere except in the very early chapters.

Python is a very expressive language, which means that we can usually write far fewer lines of Python code than would be required for an equivalent application written in, say, C++ or Java. This makes it possible to show some small but complete examples throughout the

Rapid GUI Programming with Python and Qt: The Definitive Guide to PyQt Programming

Rapid GUI Programming with Python and Qt: The Definitive Guide to PyQt Programming

Prepared for Paul Waddell, Safari ID: pwaddell@u.washington.edu

By Mark Summerfield ISBN: 9780132354189 Publisher: Prentice Hall

Print Publication Date: 2007/10/19

User number: 905221 Copyright 2007, Safari Books Online, LLC.

This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior

written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that

otherwise violates the Safari Terms of Service is strictly prohibited.

Rapid GUI Programming with Python and Qt: The Definitive Guide to PyQt Programming Page 4 Return to Table of Contents

text, and makes PyQt an ideal tool for rapidly and easily developing GUI applications, whether for prototyping or for production use.

Since the emphasis of the book is on GUI programming, although Part I provides a fastpaced Python tutorial, it also includes some PyQt coverage. This material is clearly marked (just like this paragraph, with "Qt" in the margin) to make it easy for experienced Python programmers to skip the Python they already know. Parts II, III, and IV of the book are all PyQt-specific and assume that readers can already program in Python, whether from previous experience or from reading Part I.

Figure 1. The eric4 IDE--a PyQt4 application

Quite often in programming we reach decision points when there are several possible approaches we could take. Reference books and the online documentation identify what classes, methods, and functions are available, and in some cases provide examples, but such documents rarely provide a broader context. This book gives the necessary context, highlighting the key decision points for GUI programming, and offering insights into the pros and cons, so that you can decide for yourself what the right policy is for your particular

Rapid GUI Programming with Python and Qt: The Definitive Guide to PyQt Programming

Rapid GUI Programming with Python and Qt: The Definitive Guide to PyQt Programming

Prepared for Paul Waddell, Safari ID: pwaddell@u.washington.edu

By Mark Summerfield ISBN: 9780132354189 Publisher: Prentice Hall

Print Publication Date: 2007/10/19

User number: 905221 Copyright 2007, Safari Books Online, LLC.

This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior

written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that

otherwise violates the Safari Terms of Service is strictly prohibited.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download