[PyQt] PyQt5 NULL QVariant

Phil Thompson phil at riverbankcomputing.com
Thu Apr 21 15:22:37 BST 2016


On 18 Apr 2016, at 1:27 pm, Matthias Kuhn <matthias at opengis.ch> wrote:
> 
> Hi,
> 
> I am currently updating QGIS to PyQt5 (and Qt5 and Python3). Since this update, NULL QVariant strings are converted to empty strings '' and all numbers to 0 when converted from C++ to python objects.
> 
> The documentation is not very verbose on this topic:
> 
> PyQt5 does not support the QPyNullVariant class as it is no longer needed.
> http://pyqt.sourceforge.net/Docs/PyQt5/pyqt_qvariant.html
> 
> sip.enableautoconversion() which is mentioned on the same doc page probably does the job of preserving the semantics but at the same time sacrifices all the advantages of PyQt4's v2 API.

The only disadvantage is having to explicitly call the value() method.

> What is the proper way of handling this?
> 
> I.e. how can I tell if a QVariant is
>  * Invalid
>  * Valid but Null (with or without type information)
>  * Valid (plus value and type)

If you have a QVariant then you have the isValid(), isNull() etc methods.

Phil


More information about the PyQt mailing list