<div dir="ltr"><div>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:</div><div><br></div><div><div><font face="monospace, monospace" color="#351c75">@pyqtProperty(QVariant, notify = publicPropertyChanged)#'QDateT<wbr>ime')</font></div><div><font face="monospace, monospace" color="#351c75">def my_date(self):</font></div><div><font face="monospace, monospace" color="#351c75"> if self._my_date is None:<br></font></div><div><font face="monospace, monospace" color="#351c75"> return None</font></div><div><font face="monospace, monospace" color="#351c75"> return QDateTime(self._my_date) # no PyQt autoconvert for datetime->QVariant</font></div><div><br></div></div><div>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.</div><div><br></div><div>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.</div><div><br></div><div>Even more interesting is that something (PyQt?) is actively preventing me from creating a Void instance...</div><div><br></div><div><font face="monospace, monospace" color="#351c75">>>> foo = QVariant(QVariant.Type(QtCore.<wbr>QMetaType.Void))<br></font></div><div><font face="monospace, monospace" color="#351c75">Trying to create a QVariant instance of QMetaType::Void type, an invalid QVariant will be constructed instead</font></div><div><font face="monospace, monospace" color="#351c75">>>> foo.type()</font></div><div><font face="monospace, monospace" color="#351c75">0</font></div><div><br></div><div>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.</div><div><br></div>
</div>