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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Russell Warren
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.


_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: How to have QML interpret a property as null (instead of undefined)?

Russell Warren
Solution found: Returning a freshly instantiated QObject() does the trick.  QML interprets this as a null.  ie:

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


_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: How to have QML interpret a property as null (instead of undefined)?

oliver-56
Is this a documented technique? If not, I would avoid it. Use only the documented way of doing this, and if none is documented, there is likely a reason that the feature is not available. In that case, find and understand the reason, and fix your design accordingly.

On Thu, 12 Oct 2017 at 11:25 Russell Warren <[hidden email]> wrote:
Solution found: Returning a freshly instantiated QObject() does the trick.  QML interprets this as a null.  ie:

@pyqtProperty(QVariant, notify = publicPropertyChanged)#'QDateTime')
def my_date(self):
    if self._my_date is None:
        return QObject()  # QML interprets this as null
    return QDateTime(self._my_date) # no PyQt autoconvert for datetime->QVariant
_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
--
Oliver
My StackOverflow contributions
My CodeProject articles
My Github projects
My SourceForget.net projects

_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt