What is PyQt?
PyQt is a set of Python v2 and v3 bindings for
Digia's Qt application framework and runs on all platforms supported by Qt including Windows, MacOS/X and Linux. PyQt4 supports Qt v4 and will build against Qt v5. PyQt5 supports Qt v5. The bindings are implemented as a set of Python modules and contain over 300 classes and over 6,000 functions and methods.
Like Qt, PyQt is available on all platforms under a variety of licenses including the GNU GPL v3 and a commercial license. Unlike Qt, PyQt is not available under the LGPL. You can purchase the commercial version of PyQt here.
PyQt does not include a copy of Qt. You must obtain a correctly licensed copy of Qt yourself. However, a binary Windows installer of the GPL version of PyQt v4 is provided and this includes a copy of the LGPL version of Qt.
PyQt4 contains the following Python modules.
- The QtCore module contains the core non-GUI classes, including the event loop and Qt's signal and slot mechanism. It also includes platform independent abstractions for Unicode, threads, mapped files, shared memory, regular expressions, and user and application settings.
- The QtGui module contains the majority of the GUI classes. These include a number of table, tree and list classes based on the model-view-controller design pattern. Also provided is a sophisticated 2D canvas widget capable of storing thousands of items including ordinary widgets.
- The QtHelp module contains classes for creating and viewing searchable documentation and being able to integrate online help with PyQt applications. It is based on the C++ port of the Lucene text search engine.
- The QtNetwork module contains classes for writing UDP and TCP clients and servers. It includes classes that implement FTP and HTTP clients and support DNS lookups. Network events are integrated with the event loop making it very easy to develop networked applications.
- The QtOpenGL module contains classes that enable the use of OpenGL in rendering 3D graphics in PyQt applications.
- The QtDBus module contains classes for implementing access to remote applications using the DBus protocol.
- The QtDeclarative module contains classes for integrating Python with the QML declarative language.
- The QtSql module contains classes that integrate with open-source and proprietary SQL databases. It includes editable data models for database tables that can be used with GUI classes. It also includes an implementation of SQLite.
- The QtSvg module contains classes for displaying the contents of SVG files. It supports the static features of SVG 1.2 Tiny.
- The QtTest module contains functions that enable unit testing of PyQt applications. PyQt does not implement the complete Qt unit test framework. Instead it assumes that the standard Python unit test framework will be used and implements those functions that simulate a user interacting with a GUI.
- The QtXml module implements SAX and DOM interfaces to Qt's XML parser.
- The QtXmlPatterns module implements XQuery and XPath support for XML and custom data models.
- The phonon module implements a multimedia framework that enables the use of audio and video content in PyQt applications. On Windows DirectX is used as the backend, on MacOS/X QuickTime is used as the backend, and on Linux GStreamer is used as the backend.
- The QtMultimedia module implements low-level multimedia functionality. Application developers would normally use the phonon module.
- The QtAssistant module implements a simple interface for integrating Qt Assistant with PyQt applications to provide online help. Qt Assistant is itself implemented using QtHelp.
- The QtDesigner module contains classes that allow Qt Designer to be extended using PyQt.
- The QAxContainer module implements access to ActiveX controls and COM objects. It is only available in the commercial version of PyQt for Windows.
- The Qt module consolidates the classes contained in all of the modules described above into a single module. This has the advantage that you don't have to worry about which underlying module contains a particular class. It has the disadvantage that it loads the whole of the Qt framework, thereby increasing the memory footprint of an application. Whether you use this consolidated module, or the individual component modules is down to personal taste.
- The DBus support module is installed as dbus.mainloop.qt. This module is now deprecated as the dbus-python package will not be ported to Python v3. Instead the QtDBus module should be used for all new applications.
- The uic module implements support for handling the XML files created by Qt Designer that describe the whole or part of a graphical user interface. It includes classes that load an XML file and render it directly, and classes that generate Python code from an
XML file for later execution.
PyQt also contains a number of utility programs.
- pyuic4 corresponds to the Qt uic utility. It converts GUIs created using Qt Designer to Python code.
- pyrcc4 corresponds to the Qt rcc utility. It embeds arbitrary resources (eg. icons, images, translation files) described by an XML resource collection file in a Python module. These resources can then be accessed from a PyQt application as files in a virtual filesystem.
- pylupdate4 corresponds to the Qt lupdate utility. It extracts all of the translatable strings from Python code and creates or updates translation files. These are then used by Qt Linguist to manage the translation of those strings.
PyQt brings together the Qt C++ cross-platform application framework and the cross-platform interpreted
Qt is more than a GUI toolkit. It includes abstractions of network sockets, threads, Unicode, regular expressions, SQL databases, SVG, OpenGL, XML, a fully functional web browser, a help system, a multimedia framework, as well as a rich collection of GUI widgets.
Qt classes employ a signal/slot mechanism for communicating between objects that is type safe but loosely coupled making it easy to create re-usable
Qt also includes Qt Designer, a graphical user interface designer. PyQt is able to generate Python code from Qt Designer. It is also possible to add new GUI controls written in Python to Qt Designer.
Python is a simple but powerful object-orientated language. Its simplicity makes it easy to learn, but its power means that large and complex applications
can be created. Its interpreted nature means that Python programmers are very productive because there is no edit/compile/link/run development cycle.
Much of Python's power comes from its comprehensive set of extension modules providing a wide variety of functions including HTTP servers, XML parsers, database access, data compression tools and, of course, graphical user interfaces. Extension modules are usually implemented in either Python, C or C++. Using tools such as SIP it is relatively straight forward to create an extension module that encapsulates an existing C or C++ library. Used in this way, Python can then become the glue to create new applications from established libraries.
PyQt combines all the advantages of Qt and Python. A programmer has all the power of Qt, but is able to exploit it with the simplicity of Python.
For more details, including some simple programming examples, you can download a copy of the PyQt Whitepaper.