I want to set a model in qml from a string representing this models name and a attribute.
So in my minimal example the model is otherModel with the attribute description. And this string is part of another model myModel.
In my real application i load that stuff from a config file where the models name and attribute is a string.
class MyModel(QtCore.QObject):
def __init__(self, other_model_name_and_attribute:str):
QtCore.QObject.__init__(self)
self._other_model_name_and_attribute = other_model_name_and_attribute
@QtCore.Property(str, constant=True)
def other_model_name_and_attribute(self) -> str:
return self._other_model_name_and_attribute
and
class OtherModel(QtCore.QObject):
def __init__(self, description:str):
QtCore.QObject.__init__(self)
self._description = description
@QtCore.Property(str, constant=True)
def description(self) -> str:
return self._description
Then register the models:
mm = MyModel(other_model_name_and_attribute='otherModel.description')
om = OtherModel(description='Hello')
self._view.context.setContextProperty('myModel', mm)
self._view.context.setContextProperty('otherModel', om)
And here i want wo set the otherModel
Repeater {
model: myModel
delegate: Item {
Text {
text: myModel.other_model_name_and_attribute # or 'otherModel.description'
}
}
}
The last code block is the most interesting for this question. I want to set the model for the "Text" views text attribute to otherModel.description but i only have 'otherModel.description' as a string value.
So how can i set a qml model and attribute from a string?