Unicode support was added to Qt in v2.0 and to Python in v1.6. In Qt, Unicode support is implemented using the QString class. It is important to understand that QStrings, Python string objects and Python Unicode objects are all different but conversions between them are automatic in many cases and easy to achieve manually when needed.
Whenever PyQt expects a QString as a function argument, a Python string object or a Python Unicode object can be provided instead, and PyQt will do the necessary conversion automatically.
You may also manually convert Python string and Unicode objects to QStrings by using the QString constructor as demonstrated in the following code fragment.
qs1 = QString('Converted Python string object') qs2 = QString(u'Converted Python Unicode object') |
In order to convert a QString to a Python string object use the Python str() function. Applying str() to a null QString and an empty QString both result in an empty Python string object.
In order to convert a QString to a Python Unicode object use the Python unicode() function. Applying unicode() to a null QString and an empty QString both result in an empty Python Unicode object.
When an instance of a C++ class is not created from Python it is not possible to access the protected member functions, or emit the signals, of that instance. Attempts to do so will raise a Python exception. Also, any Python methods corresponding to the instance's virtual member functions will never be called.
Throughout the bindings, the None value can be specified wherever NULL is acceptable to the underlying C++ code.
Equally, NULL is converted to None whenever it is returned by the underlying C++ code.
PyQt represents void * values as objects of type sip.voidptr. Such values are often used to pass the addresses of external objects between different Python modules. To make this easier, a Python integer (or anything that Python can convert to an integer) can be used whenever a sip.voidptr is expected. Also a sip.voidptr may be converted to an integer by using the int() builtin function.
PyQt implements the full set of Qt's thread classes. Python, of course, also has its own thread extension modules. It is possible to use either of the APIs so long as you follow some simple rules.
If you use the Qt API then the very first import of one of the PyQt modules must be done from the main thread.
If you use the Python API then all calls to PyQt (including any imports must be done from one thread only. Therefore, if you want to make calls to PyQt from several threads then you must use the Qt API.
If you want to use both APIs in the same application then all calls to PyQt must be done from threads created using the Qt API.
C++ does not garbage collect unreferenced class instances, whereas Python does. In the following C++ fragment both colours exist even though the first can no longer be referenced from within the program:
c = QColor(); c = QColor(); |
In the corresponding Python fragment, the first colour is destroyed when the second is assigned to c:
c = QColor() c = QColor() |
In Python, each colour must be assigned to different names. Typically this is done within class definitions, so the code fragment would be something like:
self.c1 = QColor() self.c2 = QColor() |
Sometimes a Qt class instance will maintain a pointer to another instance and will eventually call the destructor of that second instance. The most common example is that a QObject (and any of its sub-classes) keeps pointers to its children and will automatically call their destructors. In these cases, the corresponding Python object will also keep a reference to the corresponding child objects.
So, in the following Python fragment, the first QLabel is not destroyed when the second is assigned to l because the parent QWidget still has a reference to it.
p = QWidget() l = QLabel('First label',p) l = QLabel('Second label',p) |
Access to C++ variables is supported. They are accessed as Python instance variables. For example:
tab = QTab() tab.label = "First Tab" tab.r = QRect(10,10,75,30) |
Global variables and static class variables are effectively read-only. They can be assigned to, but the underlying C++ variable will not be changed. This may change in the future.
Access to protected C++ class variables is not supported. This may change in the future.
It is not possible to define a new Python class that sub-classes from more than one Qt class.