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.
The above comments actually apply to any SIP generated module, not just PyQt.
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.
Qt implements i18n support through the Qt Linguist application, the QTranslator class, and the QApplication::translate(), QObject::tr() and QObject::trUtf8() methods. Usually the tr() method is used to obtain the correct translation of a message. The translation process uses a message context to allow the same message to be translated differently. tr() is actually generated by moc and uses the hardcoded class name as the context. On the other hand, QApplication::translate() allows to context to be explicitly stated.
Unfortunately, because of the way Qt implents tr() (and trUtf8()) it is not possible for PyQt to exactly reproduce its behavour. The PyQt implementation of tr() (and trUtf8()) uses the class name of the instance as the context. The key difference, and the source of potential problems, is that the context is determined dynamically in PyQt, but is hardcoded in Qt. In other words, the context of a translation may change depending on an instance's class hierarchy.
class A(QObject): def __init__(self): QObject.__init__(self) def hello(self): return self.tr("Hello") class B(A): def __init__(self): A.__init__(self) a = A() a.hello() b = B() b.hello() |
In the above the message is translated by a.hello() using a context of A, and by b.hello() using a context of B. In the equivalent C++ version the context would be A in both cases.
The PyQt behaviour is unsatisfactory and may be changed in the future. It is recommended that QApplication.translate() be used in preference to tr() (and trUtf8()). This is guaranteed to work with current and future versions of PyQt and makes it much easier to share message files between Python and C++ code. Below is the alternative implementation of A that uses QApplication.translate().
class A(QObject): def __init__(self): QObject.__init__(self) def hello(self): return qApp.translate("A","Hello") |
Note that the code generated by pyuic uses QApplication.translate().