PyQt4 implements two APIs for
QVariant. v1 (the default
for Python v2) exposes the
QVariant class to Python and
requires applications to explicitly convert a
to the actual value. v2 (the default for Python v3) does not expose the
QVariant class to Python and automatically converts a
QVariant to the actual value. While this is usually the
best thing to do, it does raise problems of its own:
- Information is lost when converting between a C++
QVariantand the corresponding Python object. For example a
QVariantdistinguishes between signed and unsigned integers but Python doesn’t. Normally this doesn’t matter but some applications may need to make the distinction.
- There is no obvious way to represent a null
QVariantas a standard Python object. PyQt4 introduced the
QPyNullVariantclass to address this problem.
Multiple APIs are intended to help manage an application’s use of an old API to a newer, incompatible API. They cannot be used to temporarily change the behaviour - modules that rely on different API versions cannot be used in the same application.
In PyQt5 the implementation of
QVariant is different to
those of PyQt4. By default the behaviour is the same as PyQt4’s v2 API.
However it is possible to temporarily suppress the automatic conversion of a
QVariant to a Python object and to return a wrapped
QVariant instead - behaviour similar to PyQt4’s
v1 API - by calling the
The actual value of a wrapped Python
obtained by calling its
value() method. (Note
that in PyQt4’s v1 API this method is called
PyQt5 does not support the
QPyNullVariant class as a null
QVariant is automatically converted to
None and vice