GUI Programming with Python: QT Edition

Boudewijn Rempt

Copyright (c) 2001 by Command Prompt, Inc. This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at

‘Distribution of substantively modified versions of this document is prohibited without the explicit permission of the copyright holder.' to the license reference or copy.

‘Distribution of the work or derivative of the work in any standard (paper) book form is prohibited unless prior permission is obtained from the copyright holder.' to the license reference or copy.

Although every reasonable effort has been made to incorporate accurate and useful information into this book, the copyright holders make no representation about the suitability of this book or the information therein for any purpose. It is provided "as is" without expressed or implied warranty.


This book is dedicated to Irina.

Table of Contents
Who is using PyQt
For whom is this book intended
How to read this book
1. Introduction
GUI programming with Python
About the BlackAdder IDE
I. Introduction to the BlackAdder IDE
2. Installation
Installing BlackAdder
Installing sip and PyQt without BlackAdder
3. Interface
Project management
BlackAdder Configuration
Python shell
4. Introduction to Python
Programming fundamentals
The Rules
5. Debugging
Running scripts
Setting breakpoints
Stepping along
Debugging Techniques
If all else fails
II. PyQt fundamentals
6. Qt Concepts
Python, Qt and PyQt
As simple as they come
A better Hello World
Designing forms
7. Signals and Slots in Depth
The concept of signals and slots
Connecting with signals and slots
A parser-formatter using signals and slots
8. String Objects in Python and Qt
String conversions
QCString — simple strings in PyQt
Unicode strings
9. Python Objects and Qt Objects
Pointers and references
Circular references
Qt objects, Python objects and shadow objects
References and ownership
Other C++ objects
Connecting signals and slots
Object and class introspection
10. Qt Class Hierarchy
Base classes
Application classes
Widget foundations: QWidget
Basic widgets
Advanced widgets
Layout managers
Dialogs and Standard Dialogs
Qt Utility classes and their Python equivalents
11. Qt Designer, BlackAdder and uic
Advanced Designer topics
III. Creating real applications with PyQt
12. Application Frameworks
Architecture: models, documents and views
Macro languages
Project layout
13. Actions: menus, toolbars and accelerators
Keyboard accelerators
Setting an application icon
14. Automatic testing with PyUnit
About unittests
Starting out
A first testcase
Collecting tests in a test suite
A more complicated test
Large projects
Testing signals and slots
15. A More Complex Framework: Multiple Documents, Multiple Views
Document/View Manager
The Document Manager
The actual application
16. User Interface Paradigms
Tabbed documents
Back to the MDI windows
A row of split windows
A stack of documents
A more complex view management solution
17. Creating Application Functionality
The view
The document
Saving and loading documents
Undo, redo and other editing functions
18. Application Configuration
Platform differences
The Python way of handling configuration settings
Implementing configurations settings for Kalam
Settings in Qt 3.0
19. Using Dialog Windows
Modal: a preferences dialog
Non-modal: Search and replace
20. A Macro Language for Kalam
Executing Python code from Python
Integrating macros with a GUI
Creating a macro API from an application
21. Drawing on Painters and Canvases
Working with painters and paint devices
22. Gui Design in the Baroque Age
Types of gui customization
Faking it with bitmaps
Creating themes with QStyle
23. Drag and drop
Handling drops
Initiating drags
24. Printing
The QPrinter class
Adding printing to Kalam
Putting ink to paper
25. Internationalizing an Application
Translating screen texts
26. Delivering your Application
Packaging source
Starting with distutils.
Creating Unix RPM packages
Windows installers
Desktop integration
27. Envoi
IV. Appendices
A. Reading the Qt Documentation
B. PyQwt: Python Bindings for Qwt
C. First Steps with Sip
How sip works
Creating .sip files
Things sip can't do automatically
Where to look to start writing your own wrappers/bindings
Sip usage and syntax
Accepted C++ / Qt constructs
SIPLIB Functions
List of Tables
1-1. GUI Toolkits for Python
7-1. Matrix of QObject.connect() combinations.
10-1. Qt and Python high-level datastructures
10-2. Qt and Python network classes
C-1. C++ access specifiers and sip
C-2. Command line options
C-3. Macros in Makefile Templates
C-4. Flags in member function declaration
C-1. Error flags in sipParseArgs()
C-2. Format specifiers for sipParseArgs()
List of Figures
10-1. Qt Inheritance Hierarchy (only the most important classes)
10-2. Object Ownership Hierarchy
20-1. Playing with eval()
20-2. Playing with exec
20-3. Playing with execfile()
List of Examples
1-1. Bootstrapping a Python application
6-1. — hello world
6-2. — a better hello world
6-3. fragment from
6-4. Fragment from
6-5. Fragment from
6-7. — the subclass of the generated form
7-1. A stupid button which is not reusable
7-2. A simple callback system
7-3. A central registry of connected widgets
7-4. Connecting a signal to a slot
7-5. Connection a dial to a label with signals and slots
7-6. Python signals and slots
7-7. Python signals and slots with arguments
7-8. — connecting and disconnecting signals and slots
7-9. An XML parser with signals and slots
8-1. — conversion from QString to a Python string.
8-2. - second try of saving a QString to a file
8-3. - feeding zero bytes to a QCString
8-4. - empty and null QCStrings and Python strings
8-5. - feeding zero bytes to a QString
8-6. Loading an utf-8 encoded text
8-7. Building a string from single Unicode characters
8-8. — saving a useful function from wanton destruction
8-9. - messing with Unicode strings using utf-8 as default encoding
8-10. - coercing Python strings into and from QStrings
8-11. - coercing Python strings into and from QStrings
9-1. - showing object references
9-2. - circululululular references
9-3. — about Qt reference counting
9-4. - keeping a Qt widget alive
9-5. - Qt parents and children
9-6. Eradicating a widget
9-7. - getting the children from a single parent
9-8. Iterating over children
9-9. - a simple signals/slots implementation in Python, following the Observer pattern
9-10. Object introspection using Qt
9-11. Object introspection using Python
10-1. - handling mouse events in PyQt
10-2. - Using a QAction to group data associated with user commands
10-3. fragment from - ten little scribbling windows
10-4. - using QWidget to create a custom, double-buffered drawing widget.
10-5. snippet from - a peach puff drawing board
10-6. fragment from - You cannot create a QPixmap before a QApplication
10-7. - Four pushbuttons saying ‘hello'.
10-8. - a label associated with an edit control
10-9. - a group of mutually exclusive options
10-10. - A listbox where data can be associated with an entry
10-11. - building a tree
10-12. - two box layouts and adding and removing buttons dynamically to a layout
10-13. - setting the initial size of an application
10-14. - opening message and default dialogs boxes
10-15. fragment from - opening a file dialog
10-16. fragment from - opening a font dialog
10-17. fragment from - opening a color dialog
10-18. from - using Qt utility classes
10-19. fragment from - using Python utility classes
10-20. Using QMimeSourceFactory (
10-21. — Python threads without gui
10-22. Python threads and a PyQt gui window
11-1. — a subclass of
11-2. Setting default values
12-1. A simple document-view framework
12-2. Scripting an application is easy
13-1. Defining a complex toggle action
15-1. A testcase for a document manager
15-2. The document manager class
15-3. The document class
15-4. The view class
15-5. The application class
21-1. - A silly type-o-meter that keeps a running count of how many characters are added to a certain document and shows a chart of the typerate...
21-2. - a Unicode character selection widget
22-1. - remote control application
22-2. - the main view of the remote control application
22-3. - the class that implements the pixmapped buttons
22-4. A Qt 2 custom style - a minimalist implementation of the classic Mac style in PyQt.
22-5. Testing styles
23-1. Handling drop events
23-2. Drag and drop
25-1. Installing the translator
26-1. README
26-2. - a sample setup script
C-1. Interface for QRegExp::match