Home > OS >  Is it safe to bind to C subobject's property in QML?
Is it safe to bind to C subobject's property in QML?

Time:12-10

C :
database c object is exposed to QML as a context property.
database c object has a method getDbpointObject() that returns pointer to databasePoint C object.
databasePoint C object has a property named 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 is a custom QML component.
qmlComp has a QML property named qmlCompProp.
On completion of qmlComp creation, databasePoint c object is assigned to qmlCompProp.

qmlComp.qml:

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

Question:
In binding.qml, QML property bindProp is binded to myQmlComp.qmlCompProp.cppProp
Is this binding safe?
Will the binding always be resolved?
databasePoint c object is assigned to qmlCompProp in Component.onCompleted. Until then, qmlCompProp is an empty object. Will it have an impact on binding resolution?
Will the order of properties evaluation in binding.qml have an impact on binding resolution?

binding.qml:

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

CodePudding user response:

Yes, that should be safe in theory. The binding doesn't happen until the object exists, and it doesn't exist until after Component.onCompleted() exits. The value will be resolved, the binding will be maintained unless you break it, and the order of properties shouldn't matter.

CodePudding user response:

Response from Qt Support:

In binding.qml, QML property bindProp is binded to myQmlComp.qmlCompProp.cppProp Is this binding safe?
Looks like.

Will the binding always be resolved?
Assuming no reference issues, yes

databasePoint c object is assigned to qmlCompProp in Component.onCompleted. Until then, qmlCompProp is an empty object. Will it have an impact on binding resolution?
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).

Will the order of properties evaluation in binding.qml have an impact on binding resolution?
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.

  • Related