Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
33
rated 0 times [  39] [ 6]  / answers: 1 / hits: 17676  / 8 Years ago, wed, january 11, 2017, 12:00:00

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:




  • What happens on the C++ side when I set a property to undefined on the QML side

  • What happens on the C++ side when I set a property to null on the QML side

  • What happens on the QML side when I set a property to null on the C++ side


More From » c++

 Answers
120

You'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.


[#59395] Sunday, January 8, 2017, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jarrodfletchers

Total Points: 75
Total Questions: 94
Total Answers: 95

Location: Netherlands
Member since Thu, Jul 1, 2021
3 Years ago
jarrodfletchers questions
Sat, Sep 25, 21, 00:00, 3 Years ago
Mon, Jan 21, 19, 00:00, 5 Years ago
Sun, Dec 16, 18, 00:00, 6 Years ago
Sun, Nov 4, 18, 00:00, 6 Years ago
;