Question on using "id:" to identify the properties dynamically: How do I set "id" programmatically in C++ code? Is "id" a dynamic property on the object?
Here is the use case: I'm connecting to a piece of hardware. That hardware is configured through a set of parameters. The parameters are named with strings. The parameter set differs depending on what firmware you have installed. I want to expose those parameters as properties so I can create QML ui to edit them.
How I currently do it:
- I have a Parameters object which has a Q_PROPERTY in it for each parameter which is named the parameter string name.
- The Q_PROPERTY exposes a Fact object which has a value property as well as meta data such as min/max ranges, user visible descriptions and so forth.
- I can then bind to the parameter in QML like this: parameters.PROP_1.value
- The top level Parameters object exists as a container for the properties because there will be additional sets of information like this. There is also telemetry data which comes back from the board, which I want to also expose as Facts. By using the top level object it removes possible collision on the namespace between the two sets of values.
What I would like to do:
- I can query the hardware for all the parameter names. Using that I'd like to attach the parameter Fact properties to the Parameters object dynamically at run time.
- This way if the client code gets out of sync with the firmware parameter set it will still work since the property list is added at runtime.
Also, thanks a ton for all the help.
You don't. Why would you want to do that? You said you wanted to access the object in QML.
No, id is not reflected on the C++ side.Is "id" a dynamic property on the object?
In my opinion you should expose the data as a plain-old QAbstractListModel model. You can then connect to that model on QML side using a view (e.g. ListView) or a custom UI.Here is the use case: I'm connecting to a piece of hardware. That hardware is configured through a set of parameters. The parameters are named with strings. The parameter set differs depending on what firmware you have installed. I want to expose those parameters as properties so I can create QML ui to edit them.
How I currently do it:
- I have a Parameters object which has a Q_PROPERTY in it for each parameter which is named the parameter string name.
- The Q_PROPERTY exposes a Fact object which has a value property as well as meta data such as min/max ranges, user visible descriptions and so forth.
- I can then bind to the parameter in QML like this: parameters.PROP_1.value
- The top level Parameters object exists as a container for the properties because there will be additional sets of information like this. There is also telemetry data which comes back from the board, which I want to also expose as Facts. By using the top level object it removes possible collision on the namespace between the two sets of values.
What I would like to do:
- I can query the hardware for all the parameter names. Using that I'd like to attach the parameter Fact properties to the Parameters object dynamically at run time.
- This way if the client code gets out of sync with the firmware parameter set it will still work since the property list is added at runtime.
As far as setting an id from the C++ side I was just trying to figure out how to use your example. The objects are all created in C++ and then pushed into a QML context. Given that, using an id won't work.
If I use a list model I won't be able to do something like this in QML will I?
TextInput {
text: parameters.PROP_1.value
}
You surely have a QML declaration somewhere which refers to a particular parameter (and you can do that via id), otherwise your QML code wouldn't know how to "call" a particular parameter as it might not exist.
If you put that in a delegate referencing a particular model entry then it would be simpler:If I use a list model I won't be able to do something like this in QML will I?
TextInput {
text: parameters.PROP_1.value
}
javascript Code:
ListView { id: view model: myModel // ... Row { width: view.width Text { text: model.name } Slider { minimumValue: model.minimum maximumValue: model.maximum value: model.value onValueChanged: model.value = value } Text { text: model.value } } }To copy to clipboard, switch view to plain text mode
You could even have different types of parameters and construct a proper delegate depending on the type (e.g. putting a slider for numeric parameters and text field for textual ones).
Last edited by wysota; 7th December 2014 at 22:26.
What you could try is a map
where the key is the property name and the value is a Fact objectQt Code:
{ Q_OBJECT Q_PROPERTY(QVariantMap facts .... ) };To copy to clipboard, switch view to plain text mode
Or exposing the map directly as a context property (if it does not change during runtime).
Cheers,
_
QVariantMap - That look like exactly what I need. Thank you very much.
Bookmarks