A sample chapter is available free from InformIT—click "Sample Content" and then "Download the chapter" (almost at the bottom of the page).
This book teaches programmers how to write programs using PyQt 4, the Python bindings for the Qt 4 application development framework. PyQt 4 is probably the fastest and easiest route into GUI programming and works on Windows, Mac OS X, and most operating systems that use the X Window System, such as Linux and many Unices. Note that the book is also useful for PySide programmers. (Be aware that from PyQt4.6, PyQt now has two slightly different APIs—but this only affects Python 3 out of the box and doesn't affect Python 2 users unless they want it to: see Downloads (Python 3 Downloads).)
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, including scientists and engineers, who need to do some programming in support of their work. It is also suitable for students—the only prerequisite is basic knowledge of an object oriented programming language, for example, Java or C++, or of course Python itself.
The book is ~550 pages (648 including the appendices and index), has a foreword by Phil Thompson creator of PyQt, and is published by Prentice Hall. It can also be bought from amazon (#ad) and other online and local book stores.
Only the printed editions are definitive—although available in electronic formats, "ebooks" usually restrict your rights, and they are often retypeset which can introduce errors. A legal PDF version is available from InformIT. Translations are arranged by the publisher and their quality can vary considerably.
The source code, including mkpyqt.py
and Make PyQt is
available in two formats, pyqtbook.tar.gz
(713K suitable for any platform), and pyqtbook.zip (909K Windows line endings)
[updated 2011-02-15]. These examples have been tested
with Python 2.5 and PyQt 4.2-4.6. (See the bottom of the page
for Downloads (Python 2.6+) and
Downloads (Python 3) and for information
PySide Porting.)
There is also an Errata [updated 2014-10-21].
Part I is designed to quickly teach Python to non-Python programmers so that they can use the rest of the book. This part is also useful to Python programmers because it covers some of the non-GUI Qt classes, and some of the Python techniques that are often used in GUI programming, such as partial function application.
Most of the text is in Parts II, III, and IV. The chapters in these parts cover similar ground to my C++/Qt books, but have been written entirely from scratch with different examples and written from a totally PyQt 4 perspective. Also, some topics are included that are not covered by my other books, for example, the rich text engine, and some more advanced model/view programming techniques.
Although the examples work fine with both Python 2.5 and Python 2.6, I've created Python 2.6–specific versions that take some advantage of Python 2.6's Python 3–like features. The code is available in two formats, pyqtbook26.tar.gz (720K suitable for any platform), and pyqtbook26.zip (926K Windows line endings) [updated 2011-06-20]. These examples have been tested on Linux with Python 2.6 and PyQt 4.4-4.6. There are also two new versions, pyqtbook27.tar.gz (720K suitable for any platform), and pyqtbook27.zip (933K Windows line endings) [updated 2014-05-06]—these use the ‟new” nicer signal-slot and emit syntaxes introduced by PyQt 4.5, but are untested.
Mac users please note that PyQt does not seem to be
buildable with Python 2.6 on Mac OS X, at least not for
me and for some readers in the summer of 2009, so Mac users are
recommended to use Python 2.5, or maybe try Python 3! (I have
now heard from one reader who has successfully got PyQt working with
Python 2.6 on his Mac. Essentially what he did was to change the
/usr/locale/Qt4.6/mkspecs/default
symbolic link to point
directly to the compiler rather than Xcode—in his case to
macx-g++40
. In fact, since he was on a PowerPC rather than an
x86-based Mac he also had to modify the qconfig.pri
file's
QT_CONFIG
statement to remove x86
. He then unpacked
the PyQt Mac tarball and did the usual installation process and it
worked. I haven't tried this myself and don't plan to—I'm
switching exclusively to using Python 3 now.)
From PyQt4.6, PyQt has two APIs, API#1 (the original), and API#2
(new). API#2 is more Pythonic and eliminates QString
and
QVariant
, and is a bit nicer to use. API#1 remains best for
those using PyQt to prototype C++/Qt applications. API#1 is the default
for PyQt4.x with Python 2.x, and for PyQt4.0-4.5 with
Python 3.x. API#2 is the default for PyQt4.6+ with
Python 3.x, and for PySide. The book uses API#1, but the
differences are not too big. Basically, most of the changes affect uses
of QDataStream
, QSettings
, and QTextStream
,
and of course any use of QString
methods (since in API#2,
QString
is gone, so we use str
).
Versions of the examples for Python 3.0 and PyQt 4.5.1 using API#1
are available in the usual two formats, pyqtbook3.tar.gz (718K suitable for any
platform), and pyqtbook3.zip (920K Windows
line endings) [updated 2010-07-21]. The
examples have also been ported to Python 3.1 and PyQt 4.7.3
using API#2: pyqtbook31.tar.gz (723K
suitable for any platform), and pyqtbook31.zip (918K Windows line endings)
[updated 2010-08-24]. Note that both sets of
Python 3 examples are straight conversions of the book's original
examples and don't take any specific advantage of Python 3 or
PyQt 4.5 (or later) features, except for using
str.format()
rather than the %
operator. The API#2
versions don't always match up with the examples in the book (because
the book uses API#1), but as already noted, the differences aren't huge
and only affect a few specific classes.
PySide is highly compatible with PyQt. However, I have not released PySide versions of the book's examples because I haven't had time to complete porting them. If you want to use the book's examples with PySide I strongly recommend that you start from the pyqtbook31.tar.gz (723K suitable for any platform), or pyqtbook31.zip (918K Windows line endings) archives since PySide uses API#2 which is what the examples in these archives use. Note that Eric Thomson has ported all the book's examples to Python 2.7/PySide.
To convert any particular example to PySide the steps are as follows:
python3
in the first line with python
.
from __future__ import division
from __future__ import print_function
from future_builtins import *
PyQt4
with PySide
throughout (e.g., in the
imports){}
in format strings with
{0}
, {1}
, etc., as appropriate.[0]
at the end of every QFileDialog.*()
function call. (In PyQt these return a—possibly
empty—filename; in PySide they return a filename–filter
2-tuple, but all the examples only need the filename.)QT_VERSION_STR
with qVersion()
.PYQT_VERSION_STR
with PySide.__version__
;
you will also need to add import PySide
in the imports after
the future imports.@pyqtSignature
with @Slot
.def isAlive(qobj): ...
with def isAlive(qobj):
return Shiboken.shiboken.isValid(qobj)
. This assumes import
Shiboken
. (I learnt this from Eric Thomson.)chr
with unichr
.These conversions are sufficient in most cases. However, a few of the examples' behavior is not correct even after these changes. And unfortunately, I don't have the time to investigate.
For more information on the tools the book teaches see the Python website, the Qt Software website (makers of Qt), the Riverbank website (makers of PyQt 4), and the PySide website (makers of PySide, official Nokia-sponsored Python bindings for Qt, largely PyQt compatible). And for those interested in scientific and engineering programming the Python(x,y) Python distribution should be of interest.
See also my Python Programming Tips.
Like all my books and most of my other writings, this book was written using The Lout Typesetting System.
Your Privacy • Copyright © 2006 Qtrac Ltd. All Rights Reserved.