在 QML 中绑定到 C++ 子对象的 属性 是否安全?

Is it safe to bind to C++ subobject's property in QML?

C++:
数据库 C++ 对象作为上下文 属性.
公开给 QML 数据库 C++ 对象有一个方法 getDbpointObject() returns 指向 databasePoint C++ 对象的指针。
databasePoint C++ 对象有一个 属性 名为 cppProp.

main.cpp:

// expose database object to qml
database databaseObj;
engine.rootContext()->setContextProperty("database", (QObject*)&databaseObj);
// register databasePoint class
qmlRegisterType<databasePoint>("DBPoint", 1, 0, "DBPoint");

database.h:

databasePoint *database::getDbpointObject()

databasePoint.h:

Q_PROPERTY(QVariant cppProp READ cppProp WRITE setcppProp NOTIFY cppPropChanged)

QML:
qmlComp 是自定义 QML 组件。
qmlComp 有一个名为 qmlCompProp.
的 QML 属性 qmlComp 创建完成后,databasePoint c++ 对象被分配给 qmlCompProp。

qmlComp.qml:

Item 
{
property var qmlCompProp: ({})   // qml property
Component.onCompleted:
    {
        qmlCompProp = database.getDbpointObject() // qml property holds the databasePoint c++ object
    }       
}

问题:
在 binding.qml 中,QML 属性 bindProp 绑定到 myQmlComp.qmlCompProp.cppProp
这个绑定安全吗?
绑定总是会被解析吗?
databasePoint c++ 对象被分配给 Component.onCompleted 中的 qmlCompProp。在此之前,qmlCompProp 是一个空对象。对绑定分辨率有影响吗?
binding.qml 中的属性评估顺序是否会影响绑定解析?

binding.qml:

property int bindProp: myQmlComp.qmlCompProp.cppProp // is this binding safe?
qmlComp{id: myQmlComp}

是的,理论上应该是安全的。在对象存在之前绑定不会发生,并且在 Component.onCompleted() 退出之后它才会存在。该值将被解析,绑定将保持不变,除非您破坏它,并且属性的顺序无关紧要。

Qt 支持的回应:

在 binding.qml 中,QML 属性 bindProp 绑定到 myQmlComp.qmlCompProp.cppProp 这个绑定安全吗?

Looks like.

绑定总是会被解析吗?

Assuming no reference issues, yes

databasePoint c++ 对象已分配给 Component.onCompleted 中的 qmlCompProp。在此之前,qmlCompProp 是一个空对象。对绑定分辨率有影响吗?

This case should work. However, the binding is initially made for the empty object in that case (depending on how this is all instantiated) and then after the onCompleted, it updates the binding to refer to that real cppProp (instead of dummy one it created to the empty object).

binding.qml 中的属性评估顺序是否会影响绑定解析?

Not in this case at least. You could have some issue if you had other dependent properties for the binding, like if you had an array in between and its index was another property.