[PyQt] How to have QML interpret a property as null (instead of undefined)?

Russell Warren russ at perspexis.com
Wed Oct 11 17:21:28 BST 2017


I'm trying to have registered object properties appear as their values, OR
as null, in QML.  The only way I've been able to get close to this "can
also be null" behaviour is to define the property as type QVariant:

@pyqtProperty(QVariant, notify = publicPropertyChanged)#'QDateTime')
def my_date(self):
    if self._my_date is None:
        return None
    return QDateTime(self._my_date) # no PyQt autoconvert for
datetime->QVariant

Unfortunately, the QVariant that ends up in QML with `return None` is being
interpreted as `undefined` in QML/JS. I want it to be `null` in QML/JS.

I expect (but can't confirm yet) that a QVariant of type "QMetaType::Void"
*might* get interpreted as `null` in JS, but am unable to create such a
QVariant with PyQt.  The first problem is that neither QMetaType.create or
QMetaType.construct are available.

Even more interesting is that something (PyQt?) is actively preventing me
from creating a Void instance...

>>> foo = QVariant(QVariant.Type(QtCore.QMetaType.Void))
Trying to create a QVariant instance of QMetaType::Void type, an invalid
QVariant will be constructed instead
>>> foo.type()
0

Background aside... how do I create a pyqtProperty that QML will interpret
as QDateTime *or* null?  Same question applies for any type other than
QDateTime.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20171011/9f0717ad/attachment.html>


More information about the PyQt mailing list