0

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?

lixe
  • 1
  • 2
  • I don't understand your question, explain yourself better. – eyllanesc Dec 15 '19 at 21:15
  • 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. – lixe Dec 15 '19 at 21:26
  • I find it strange what you want since it seems that you want to apply a python feature to QML, it seems to me that you have a [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), why do you want to do that? – eyllanesc Dec 15 '19 at 21:30
  • Ok I have a model which is updated every second. Lets say it is a data model for a engine with rpm and ... . In this example it is *otherModel* Then I have a dashboard with views displaying values from *otherModel*. This views are created by parsing a text config file and creating views with a qml loader. Each of the views have a model holding for example width and height of the view. But the value they should display is in the *otherModel*. And which value of the *otherModel* they should display is defined in the text config file. – lixe Dec 15 '19 at 21:59
  • And why don't you make "other_model_name_and_attribute" return the other model directly instead of a str? – eyllanesc Dec 15 '19 at 22:03
  • I already tried a model repository: *model_repo.get_model('otherModel')*. That worked to get the model but not the specific attribute. I have to write *model_repo.get_model('otherModel').rpm* to make it work. But *rpm* is also defined as string in the config file ~_~. Thank you for your help! – lixe Dec 15 '19 at 22:12

1 Answers1

0

So for now i went with an model repository where i register all my models an which has the slot:

@QtCore.Slot(str, result='QVariant')
def get_model(self, model_name):
    return self._models[model_name]

and then in qml i can do:

Text {
    text: model_repo.get_model('otherModel')['rpm']
}

This is not as elegant as i hoped, but for now it works.

lixe
  • 1
  • 2