Next Noobie question...on C++ / QML integration
-
I have a C++ object that corresponds to an instance of a QML file. I have properties in the C++ class:
Q_PROPERTY(bool canBeStale READ canBeStale WRITE setCanBeStale) Q_PROPERTY(double* dataLink READ pDataLink WRITE setDataLink) Q_PROPERTY(QString dataTag READ strUDT WRITE setUDT) Q_PROPERTY(double value READ getValue WRITE setValue NOTIFY dataUpdate)
And similar properties in the QML:
id: root readonly property alias api : datahlpr property bool canbeStale : false property var dataLink property var dataTag
Is there a way to map the QML and C++ properties as presently I have added to the QML:
Component.onCompleted: { //Set-up mappings to C++ object api.dataTag = root.dataTag api.dataTag = root.dataTag api.canBeStale = root.canbeStale }
-
I have a C++ object that corresponds to an instance of a QML file. I have properties in the C++ class:
Q_PROPERTY(bool canBeStale READ canBeStale WRITE setCanBeStale) Q_PROPERTY(double* dataLink READ pDataLink WRITE setDataLink) Q_PROPERTY(QString dataTag READ strUDT WRITE setUDT) Q_PROPERTY(double value READ getValue WRITE setValue NOTIFY dataUpdate)
And similar properties in the QML:
id: root readonly property alias api : datahlpr property bool canbeStale : false property var dataLink property var dataTag
Is there a way to map the QML and C++ properties as presently I have added to the QML:
Component.onCompleted: { //Set-up mappings to C++ object api.dataTag = root.dataTag api.dataTag = root.dataTag api.canBeStale = root.canbeStale }
@SPlatten I am not sure what you mean by the "corresponding" QML file here. The C++ defines an object that is instantiable in QML directly which already has the properties exposed and so on. When you instantiate it, you will assign the properties with values from your QML environment.
So for example, say you have exposed
MyComponent
from C++ with the properties you mentioned.Then in some QML component that uses this you will have (e.g.):
MyClientComponent.qml
Item { ... MyComponent { id: mycomp canbeStale: true dataTag: "bob" ...
You can bind to and from the
MyComponent
properties in the client QML component in the usual way. -
@SPlatten I am not sure what you mean by the "corresponding" QML file here. The C++ defines an object that is instantiable in QML directly which already has the properties exposed and so on. When you instantiate it, you will assign the properties with values from your QML environment.
So for example, say you have exposed
MyComponent
from C++ with the properties you mentioned.Then in some QML component that uses this you will have (e.g.):
MyClientComponent.qml
Item { ... MyComponent { id: mycomp canbeStale: true dataTag: "bob" ...
You can bind to and from the
MyComponent
properties in the client QML component in the usual way. -
@Bob64 , what I was really asking for was is there a way to auto assign properties of the same name in the QML object to the C++ equivalent object ?
@SPlatten I think the issue that I still don't understand what you mean by "equivalent" object. Can you give a minimal example that shows more of the structure of your real system? The code in your original post just showed properties so it was difficult to see what components/classes were involved and how they related to each other.
-
Does binding help? Check the small example here.
https://stackoverflow.com/questions/41232999/two-way-binding-c-model-in-qml -
Does binding help? Check the small example here.
https://stackoverflow.com/questions/41232999/two-way-binding-c-model-in-qml -
Maybe my simple example I posted earlier this year helps you:
https://forum.qt.io/topic/135077/creating-a-c-identifier-for-a-dynamically-generated-object/2