When extending QML with custom types written in C++, I started to wonder about differences between JS and C++.
This time I was wondering, if null
-checks on the C++-side are sufficiant, or if there is something like a "QUndefined
" that I might need to consider as well. As far as I can see, this is not described here.
Or to put it differently:
undefined
on the QML sidenull
on the QML sidenull
on the C++ sideYou're certainly on the right track with your answer, but here's some more information.
What happens when you write to a property depends on what that property is, for instance, is it a QObject property? etc. Depending on the type of the property, you may also get an error on assignment (if for instance you are writing null
to a numeric type property like double).
Let's assume, for the sake of the answer that you are setting a property on a QObject
. You may set it to undefined
if the property has a RESET
function defined for the Q_PROPERTY. Quoting the documentation:
A RESET function is optional. It is for setting the property back to its context specific default value. e.g., QWidget::cursor has the typical READ and WRITE functions, QWidget::cursor() and QWidget::setCursor(), and it also has a RESET function, QWidget::unsetCursor(), since no call to QWidget::setCursor() can mean reset to the context specific cursor. The RESET function must return void and take no parameters.
To be more specific, if you have a Q_PROPERTY that has a RESET
function, when you write undefined
to that property, the RESET
function will be called.
For setting properties to null
, the answer is located right above that last reference, basically, if the property contains a QObject*
-derived type, it will store a nullptr
.
The last case you ask about is assigning null
to a property on the C++ side. For intents and purposes, I'm assuming you are asking about a case like this:
Q_PROPERTY(QObjectDerived* myThing READ myThing NOTIFY myThingChanged);
...
QObjectDerived *myThing() { return m_myThing; }
...
void somethingElse() {
m_myThing = 0; // or NULL, nullptr, whatever floats your boat
emit myThingChanged();
}
In this case, the QML-side of this property (myInstanceId.myThing
) would end up as null
, from memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With