最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Null and Undefined in QML and C++ - Stack Overflow

programmeradmin1浏览0评论

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

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
Share Improve this question edited Jan 11, 2017 at 11:08 BaCaRoZzo 7,6928 gold badges53 silver badges86 bronze badges asked Jan 11, 2017 at 8:39 derMderM 13.7k5 gold badges58 silver badges97 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 16

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.

发布评论

评论列表(0)

  1. 暂无评论