PyQt
From qtnode
Contents |
What is PyQt?
PyQt is a Python binding for Qt developed by Riverbank, mainly by Phil Thompson. The PyQt bindings allows a developer to create Qt applications in Python that look and work exactly like a C++ version of the same application. All Qt classes are available for use from PyQt applications and the API differences compared to a C++ application are very small (except for language syntax of course). Developing PyQt applications is easy for someone who know a bit of Python and/or a bit of Qt.
The current stable version of PyQt only supports Qt3 as of this writing, but a Qt4 version is on the way.
Getting PyQt
PyQt is generally available packaged in most Linux distributions. To check the newest releases and packages see Riverbank's PyQt download page.
Using PyQt3
A sample Hello world application
TBD: hello world
Signals and slots
TBD: using custom Python methods as slots, normal slots
Using Designer with PyQt3
TBD: example of how to inherit a Designer made class
Modules
TBD: custom modules that need to be imported separaqtely
Using PyQt4
Differences compared to PyQt3
TBD: list the major differences.
Using Designer with PyQt4
NOTE: If you're using Designer with PyQt4, you must be aware. As of the snapshot 20060314, the uic module does not properly add docked windows to the main windows. See the "docked" setting for QDockWidget in designer. Until this bug is fixed, you must add in your code the following:
MainWindow.addDockWidget(QtCore.Qt.LeftDockWidgetArea, dockWidget)
Adding this code will be the same as if the "docked" setting is set to true. This will ensure that the dock widget is properly docked in your application instead of floating around wherever it pleases. :)
Porting a PyQt3 application to PyQt4
Porting a PyQt3 application to PyQt4 generally follows the same steps as porting a C++ Qt3 app to Qt4. The Qt4 porting page has a lot of information related to porting apps, and most of it works for PyQt too.
TBD: some info about what has to be ported for a PyQt3 app to work as a PyQt4 app.
Documentation
PyQt documentation
More documentation regarding PyQt can be found at the PyQt homepage. This page contains much more thorough information regarding PyQt as well as a list of the state of how completely the Qt classes are implemented in PyQt. This documentation is currently for PyQt3 only.
Qt documentation
The PyQt API follows the normal C++ Qt3 API as closely as possible. In 99% of the cases you can use the normal Qt documentation or the included assistant application to learn about classes. Generally just ignore all pointers and C++ references.
All places where QString is used in the Qt API documentation you can use a normal Python string instead. It will be automatically converted to QString as needed. You can use the PyQt class QString if you want though.
Qt API exceptions
There are a few exceptions though. The most notable one is the use of the exec() method used in some classes which is a reserved Python keyword. Thus where the C++ API uses a method called exec() it has in PyQt3 been replaced with a method exec_loop(). This method is present in at least QApplication (for executing the main event loop) and in QDialog (for executing a modal dialog). In PyQT4, this method has changed to the exec_() method.
FAQ
TBD: list some faq:s
When I installed pyqt4, my pyqt3 applications stopped working. How do I fix this?
This happens if you install the two pyqt's on an OS that does dependency tracking. Pyqt3 will be installed with the sip version that matches what it was built with, and pyqt4 will be installed with the newer version it requires. Somehow, for some reason, installing pyqt4 after installing pyqt3 will break pyqt3.
The fix is to install pyqt3 after pyqt4, and to do so from source. In Gentoo, just change a USE flag for pyqt3 and re-emerge it. On a binary distribution, you should probably just download the pyqt3 sources from Riverbank and install those. Make sure you have -devel packages for qt3, obviously.