Command Prompt, Inc.
Did you know Command Prompt, does Python?

GUI Programming with Python: QT Edition

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 http://www.opencontent.org/openpub/).

‘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.


Dedication

This book is dedicated to Irina.

Table of Contents
Preface
Who is using PyQt
For whom is this book intended
How to read this book
Conventions
Acknowledgments
1. Introduction
Python
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
Menubar
Toolbars
Project management
BlackAdder Configuration
Editing
Python shell
Conclusion
4. Introduction to Python
Programming fundamentals
The Rules
Constructions
Conclusion
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
Conclusion
7. Signals and Slots in Depth
The concept of signals and slots
Connecting with signals and slots
Disconnecting
A parser-formatter using signals and slots
Conclusion
8. String Objects in Python and Qt
Introduction
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
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
Introduction
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
Actions
Menus
Toolbars
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
Conclusion
15. A More Complex Framework: Multiple Documents, Multiple Views
Introduction
Document/View Manager
The Document Manager
Document
View
The actual application
Conclusion
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
Conclusion
17. Creating Application Functionality
Introduction
The view
The document
Saving and loading documents
Undo, redo and other editing functions
Conclusion
18. Application Configuration
Platform differences
The Python way of handling configuration settings
Implementing configurations settings for Kalam
Settings in Qt 3.0
Conclusion
19. Using Dialog Windows
Modal: a preferences dialog
Non-modal: Search and replace
Conclusion
20. A Macro Language for Kalam
Executing Python code from Python
Integrating macros with a GUI
Creating a macro API from an application
Conclusion
21. Drawing on Painters and Canvases
Working with painters and paint devices
QCanvas
Conclusion
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
Conclusion
24. Printing
The QPrinter class
Adding printing to Kalam
Putting ink to paper
Conclusion
25. Internationalizing an Application
Translating screen texts
26. Delivering your Application
Introduction
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
NumPy
PyQwt
C. First Steps with Sip
Introduction
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
Directives
Accepted C++ / Qt constructs
SIPLIB Functions
Bibliography
Index
List of Examples
1-1. Bootstrapping a Python application
6-1. hello1.py — hello world
6-2. hello2.py — a better hello world
6-3. fragment from hello3.py
6-4. Fragment from hello5.py
6-5. Fragment from hello4.py
6-6. frmconnect.py
6-7. dlgconnect.py — 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. datasource.py — connecting and disconnecting signals and slots
7-9. An XML parser with signals and slots
8-1. qstring1.py — conversion from QString to a Python string.
8-2. qstring2.py - second try of saving a QString to a file
8-3. empty.py - feeding zero bytes to a QCString
8-4. null.py - empty and null QCStrings and Python strings
8-5. emptyqstring.py - 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. sitecustomize.py — saving a useful function from wanton destruction
8-9. uniqstring3.py - messing with Unicode strings using utf-8 as default encoding
8-10. uniqstring1.py - coercing Python strings into and from QStrings
8-11. uniqstring2.py - coercing Python strings into and from QStrings
9-1. refs.py - showing object references
9-2. circular.py - circululululular references
9-3. qtrefs1.py — about Qt reference counting
9-4. qtrefs2.py - keeping a Qt widget alive
9-5. qtrefs3.py - Qt parents and children
9-6. Eradicating a widget
9-7. children.py - getting the children from a single parent
9-8. Iterating over children
9-9. sigslot.py - 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. event1.py - handling mouse events in PyQt
10-2. action.py - Using a QAction to group data associated with user commands
10-3. fragment from mdi.py - ten little scribbling windows
10-4. event2.py - using QWidget to create a custom, double-buffered drawing widget.
10-5. snippet from event3.py - a peach puff drawing board
10-6. fragment from action2.py - You cannot create a QPixmap before a QApplication
10-7. buttons.py - Four pushbuttons saying ‘hello'.
10-8. label.py - a label associated with an edit control
10-9. radio.py - a group of mutually exclusive options
10-10. listbox.py - A listbox where data can be associated with an entry
10-11. tree.py - building a tree
10-12. layout.py - two box layouts and adding and removing buttons dynamically to a layout
10-13. geometry.py - setting the initial size of an application
10-14. dialogs.py - opening message and default dialogs boxes
10-15. fragment from dialogs.py - opening a file dialog
10-16. fragment from dialogs.py - opening a font dialog
10-17. fragment from dialogs.py - opening a color dialog
10-18. from dv_qt.py - using Qt utility classes
10-19. fragment from db_python.py - using Python utility classes
10-20. Using QMimeSourceFactory (application.py)
10-21. thread1.py — Python threads without gui
10-22. Python threads and a PyQt gui window
11-1. dlgcomplex.py — a subclass of frmcomplex.py
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. typometer.py - 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. charmap.py - a Unicode character selection widget
22-1. remote.py - remote control application
22-2. view.py - the main view of the remote control application
22-3. button.py - 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. setup.py - a sample setup script
26-3. MANIFEST.in
C-1. Interface for QRegExp::match


Copyright © 2000-2014 Command Prompt, Inc. All Rights Reserved. All trademarks property of their respective owners.