PyQt Whitepaper
www.riverbankcomputing.com
Abstract
This whitepaper provides an introduction to Agile programming using PyQt—the Python bindings
for the Qt application development framework. The whitepaper briefly makes the case for the
Agile programming approach and for the use of the very high level dynamic language, Python. It
also presents some of the key benefits available from the Qt application development framework
and shows how the combination of Qt and Python—PyQt, provides an ideal combination for rapidly
creating powerful and flexible GUI applications. PyQt applications can run on any platform that
has PyQt, Python, and the Qt libraries installed, simply by copying the application—and with no
source code changes necessary. (It is also possible to create stand-alone executables.) The currently
supported platforms include all versions of Windows fromWindows 98 to Vista, andmost Unixes and
Unix clones that run X11, including Mac OS X, Linux, Solaris, and HP-UX. Using PyQt, developers
can write applications with native look and feel on all these platforms, without having to waste
their time on ironing out platform differences or filling in gaps, since all these details are handled
automatically by PyQt.
Riverbank
Computing Limited
Copyright 2009 Riverbank Computing Limited. All rights reserved.
v1.1
PyQt Whitepaper
Contents
1. Executive Summary .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 2
2. The Case for Agile Development .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 2
3. The Case for Python .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 3
4. The Case for Qt .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 5
5. The Case for PyQt .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 6
5.1. An Example Application .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 7
5.2. An Example CustomWidget .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 10
6. Success Stories .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 12
7. Conclusion .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 13
1. Executive Summary
Many organisations need to develop GUI applications—some as in-house tools, and others for their
customers to buy and deploy. Two problems have traditionally beset GUI programming. First, the
ability to write applications that will run on all the required platforms. Even those who only deploy
on Windows are faced with a variety of APIs and version differences that can end up consumimg a
lot of developer time. And nowadays, servers are as often Linux-based asWindows-based,while both
Mac OS X and Linux are making significant inroads in the desktop space. Second, developing GUI
applications can be very time consuming and technically challenging using some of the languages
and libraries that are currently available. This can narrow the range of people who can usefully
be involved in developing GUI applications, a significant loss for software aimed at scientists,
engineers, doctors, and other professionals whose involvement could make products more suitable
for their peers.
PyQt offers a solution to both the problems just described, and also brings many benefits that are
not available from other technologies. In this whitepaper we will make the case for the Agile devel-
opment approach to GUI application development, and then make the case for the two foundations
on which PyQt is built—the Python programming language and the Qt application development
framework. We will thenmake the case for PyQt itself, and follow this with two examples, one show-
ing code extracts from a simplified PyQt application, and the other showing the complete code for a
very basic customwidget. These examples are provided to help programmersand other readerswith
programming knowledge to get a more direct feel for what PyQt code looks like and for how it works.
Then we will discuss at just a few of the applications that have been developed using PyQt to give a
flavour of what has been done and a taste of what is possible.
2. The Case for Agile Development
Back in the 1980s a new development paradigm was developed—RAD (Rapid Application Develop-
ment). The purpose of RAD was to increase the speed and quality of development at a time when
development processes were slow and cumbersome. Applications often took so long to develop that
the requirements changed before the application was completed.
Some of the general ideas that RAD introduced were:
• Deadlines are more important than the feature set.
• The approach to development should be multi-disciplinary—analysis, design, and development
occurring at the same time.
• The quality of the code produced is not the most important goal.
• Higher costs are acceptable in order to use tools that will reduce development time.
• Multiple prototypes will be produced, and they are intended to be discarded.
2 of 14
PyQt Whitepaper
While high-end CASE (Computer Aided Software Engineering) tools were developed as a response
to the popularity of RAD, at the other end of the spectrum, Microsoft probably had the most
practical impact on RADwith the introduction of Visual Basic. This tool not only helped with RAD,
it facilitated the production of simple applications by technical professionals with little or no prior
knowledge of software development.
RAD, as originally defined, fell out of favour due to a number of limitations, in particular:
• Due to the reduced focus on features, the resulting applications tended to provide less customer
satisfaction.
• The throwing away of prototypes was considered to be wasteful.
• Applications were difficult to scale since their architecture had evolved from prototypes rather
than being the result of careful design analysis.
Agile software development represents a successor approach to RAD. Agile development is
characterized by:
• Responsiveness to change.
• Increased speed to market.
• Lower cost.
• Greater delivered value.
• Iterative and incremental development—working software is the measure of progress.
Agile development focuses on iterative development with working software (versus prototypes)
delivered on a regular basis, typically every fewweeks. These iterations cover the entire spectrumof
software development: planning, requirements analysis, design, coding, testing, and documentation.
Unlike more traditional methods, the emphasis on each of these iterations is adapting to the
problems and feedback revealed and reported regarding the previous iteration.
Practically speaking,agile development occupiesamiddle groundbetweenRADandmore traditional
development practices. It addresses the weaknesses of RADwhile leveraging the strengths of more
traditional methods, but without their overhead.
3. The Case for Python
As the popularity of dynamic languages increased with non-professional software developers, their
usage evolved from prototyping/RAD tools into languages of choice for many applications. They
have also proved very popular with software professionals, since they improve development speed
in two ways. First, dynamic languages don’t force programmers to go through a time-consuming
edit-compile-link-runsequence—newcode can simply be edited and run. Second,dynamic languages
are usually higher level, that is, programmers can express what they want using less code, so they
are more productive.
The most popular RAD language, Visual Basic, brought significant disadvantages as well as advan-
tages. In particular, it can be difficult to build large maintainable applications using Visual Basic,
and it is also targeted at a single platform, Windows. With the introduction of the .NET toolkit,
Microsoft introduced Visual Basic.NET. This language differs from Visual Basic in some important
ways, largely due to the fact that its underlying execution environment was designed to support non-
dynamic programming languages such as C#. Some programmers have found it difficult to move old
code to the new environment, and this has led to more interest in exploring alternatives.
As agile development practices continue to gain traction, software developers have begun to turn
to dynamic languages to meet their needs. Dynamic languages are a new, more powerful subset of
what were originally known as “scripting” languages. A general way of describing the difference is
that a scripting language supports simple ways of specifying tasks to be performed, while dynamic
languages borrow features from traditional compiled programming languages and provide a robust
and dynamic execution environment. Another way to view the role and power of dynamic languages
3 of 14
PyQt Whitepaper
is as “integration” or “application” languages; they can be used to quickly and easily integrate diverse
off-the-shelf functionality into a single application.
In the same way that agile software development techniques are a compromise between RAD and
more traditional software development methodologies, dynamic languages inhabit a middle ground
between scripting languages and strongly typed/compiled programming languages. While tradi-
tional programming languages are well suited to complex and lower level tasks such as framework
development,dynamic languages aremore appropriate for application development. Using dynamic
languages also makes it much easier and quicker to adapt to changes, such as those driven by user
requirements, or by external forces such as Government regulations.
Python is an open source dynamic language that was created by Guido van Rossum in 1990
(www.python.org).Since that time, it hasmatured and grown in popularity to be one of the top tenmost
referenced computer languages on the Internet (see www.tiobe.com/tpci.htm). The case can be made
that Python is an “agile” dynamic language; here are some of its highlights:
• The language and its extensive standard library are intuitive to use and easy to learn.
• Beginners can very quickly be productive with Python.
• Experts canmake use of Python’smany advanced features, such as partial function application,
metaprogramming,and threading—there are no artificial limits. And all Python objects are first
class, whether they are built into the language or custommade by Python programmers.
• The language has a simple yet elegant object-oriented design.
• Python code is easy to read as well as easy to write—and Python is so high level that it can
almost be considered “executable pseudo-code”.
• The language is highly scalable—it is used for projects varying in size from hundreds, to
thousands, to tens of thousands, to hundreds of thousands of lines of code.
• It is very suited to rapid development and makes refactoring easy.
• Programs are portable across platforms and (especially when using PyQt) can be deployed on all
platforms without requiring changes.
• Has a very extensive standard library,withmany additional libraries and frameworksavailable,
rivaling .NET in functionality.
• Is easily extensible, by writing custom libraries in Python, or by writing extensions in other
languages such as C and C++.
• Programs are concise—a Python solution is about 50% the size of a comparable C++ solution.
• Increasingly being used in academia to teach programming—and not just for computer science
students—so Python skills and knowledge are becoming more readily available.
The development cycle also tends to be both easier and faster with Python. Like many dynamic
languages, Python is interpreted from automatically compiled byte-code. This process is invisible
to programmers, resulting in a fast edit-run cycle versus the traditional edit-compile-link-run
cycle. Additionally, since Python is a managed execution environment, all run-time errors show
up in a standardized exception format. Combined with automatic memory management (garbage
collection), this prevents an entire class of difficult-to-debug problems encountered by traditional
programming languages such as C and C++.
Integration is easily accomplished with Python; extension modules are usually implemented in
either Python, C or C++. Using tools such as SIP (www.riverbankcomputing.com/software/sip) and
Boost.Python (www.boost.org) it is relatively straightforward to create an extension module that en-
capsulates an existing C or C++ library. This is also useful in situationswhere performance sensitive
algorithms need to be implemented outside Python’s managed execution environment. And for nu-
meric processing the NumPy and SciPy libraries (www.scipy.org) offer considerable additional function-
ality, including compact and fast n-dimensional arrays.
4 of 14
PyQt Whitepaper
Application developers often have to handle the complexities of the packaging and distribution
of their applications. There are tools available for turning Python applications into distributable
executables. These tools enable applications to be distributed without the source code and without
the target system needing to have Python or the libraries used by the application installed on the
target machines. PyInstaller (pyinstaller.python-hosting.com) and py2exe (www.py2exe.org) are two
of the more popular ones.
While Python is an open source language and freely available under a very liberal license, there
are commercial offerings available for those organizations that require it. These provide the
investment protection that many companies look for when choosing a development technology. For
example,ActivePython (www.activestate.com) is a commercially supported distribution of Python and
commercial IDEs such as WingIDE (www.wingware.com) are also available.
4. The Case for Qt
Qt is a cross-platform application development framework developed by the Qt Software division
of Nokia Corporation (www.qtsoftware.com). Users of Qt include Adobe, Google, IBM, Sharp, and
Siemens. Adobe Photoshop Album and Google Earth are just two examples of widely used cross-
platform Qt applications. Qt is the foundation on which Linux’s answer to Windows—KDE (the “K”
Desktop Environment)—is built. Qt is implemented in C++ and is fully object-oriented—it provides
more than 600 classes, all with sensible defaults and useful functionality out of the box, and all able
to be customized and subclassed to meet programmer requirements.
Qt uses Unicode throughout and is supplied with tools to make translation and localisation as
straightforward as possible. In addition, Qt comes with Qt Designer, a GUI tool itself written in Qt
that providesameans of designing dialogs visually. QtDesigner fully understandsQt’s sophisticated
yet easy to use layout system, and creates dialogs that automatically adjust to their content, rather
than using the fixed sizes and positions that can be so frustrating to users of Visual Basic applica-
tions. Qt Designer generates XML files that are converted into C++, a step that is taken care of au-
tomatically by the build process. Although separating code and presentation is preferred by many
development teams, Qt’s layout system is designed for hand-coding, so using Qt Designer is not nec-
essary for those who prefer all-code development.
One key innovation that Qt introduced is the signals and slots mechanism. Now widely copied in
other GUI libraries, this mechanism provides an abstraction for handling events. While it is easy to
get all the details of mouse clicks and key presses in Qt through its low-level event handling system,
more often what programmers really care about is what the user actually wants to do, not how they
asked to do it. For example, no matter whether the user typed Ctrl+S or clicked a Save toolbar button,
or clicked a File→Save menu option, in almost every case, all that really matters is that they want to
save their work. The signals and slotsmechanism provides the high level view that makes it easy to
handle the “save” action—or any other action—no matter how it was invoked.
Signals and slots is a generalisation of the Observer design pattern that provides type-safe
communication between objects. When an object changes state or has some other significant event
occur (such as when a button is clicked), the object emits a signal (e.g., “clicked”). If we are interested
in an occurrence, we can connect to the relevant object and to the type of signal the object sends
that we are interested in—for example, we can connect a particular button’s “clicked” signal to the
method we want called when the button is clicked. Neither the emitting object (e.g., the button), nor
the receiving object (e.g., the object whose method will be called), need have any knowledge of the
other. All that must be known is that the one emits a suitable signal and that the other can connect
to the signal. Signals can be emitted with data, and the data is passed in a type-safe way, avoiding
the risks and limitations of the callbacks that most older frameworks employ.
Qt comes with a large and comprehensive set of widgets (“controls” and “containers” in Windows-
speak) from simple line edits to calendars, tables, tree views, all the way up to a web browser widget
that supportsHTML,XHTML,CSS,JavaScript,HTML canvas,andAJAX.In fact,many applications
are built purely using Qt’s standard widgets. Nonetheless, one of the outstanding features of the
framework is its support for creating customwidgets. Customwidgets can be created by subclassing
existing widgets, or they can be created from scratch with the programmer exercising complete con-
5 of 14
PyQt Whitepaper
trol over their appearance and behaviour. And Qt offers a lot more than GUI widgets; here are some
of its other highlights:
• Excellent 2D graphics support, including semi-transparency, antialiasing, a floating-point coor-
dinate system, gradients, painter paths, and automatic double-buffering (off-screen rendering)
for flicker-free updates. 3D graphics is supported using OpenGL, and support for all the most
popular bitmap image formats, including PNG, JPEG, GIF, and BMP, and support for reading
and writing SVG vector images is built in.
• Audio and video playback using the Phonon library integration.
• GUI testing with the QTest module; this allows tests to perform mouse actions and key presses
to mimic users.
• Support for wizards (“assistants” in Mac-speak)—these are dialogs that take users step-by-step
through tasks; they are most frequently used by installers.
• A model/view framework for MVC (Model-View-Controller)-style programming to support the
separation of data from how it is visualised and edited. This framework can be used with your
own data structures, or with Qt’s database classeswith which it is fully integrated. The uniform
databaseAPIworks the samenomatterwhat platformQt runs on and nomatterwhich database
Qt is used with.
• Rich text viewing and editing using HTML syntax that can be written in source code. Rich text
(including embedded images) can be printed or saved in PDF format.
• Libraries for parsing andwriting XML, including conventionalDOMandSAX parsers,and some
very fast Qt-specific XML classes.
• Powerful and flexible networking classes with support for major high level protocols such as
HTTP and FTP, as well as low level TCP and UDP classes. These classes can be used asyn-
chronously so as not to block the user interface, or synchronously in non-GUI threads.
• Solid and efficient cross-platform support for threading—the Qt library is multithreaded by
default, and provides both low level threading classes and a high level concurrency abstraction
to make threaded programming as simple as possible.
• Native look and feel on all platforms including Vista, and with full support for programmer
customization, for example using simple CSS (Cascading Style Sheet)-type styles, or by creating
style subclasses for extremely detailed control.
Unlike some other
本文档为【pyqt-whitepaper-a4】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。