QList<QObject *> data model roles in QML. Ok in example and not Ok in real life.
-
That is my QML code instantly,
ComboBox { ... textRole: "categoryName" model: converter.categoryModel onCurrentIndexChanged : { console.log(model.modelData.name) //converter.setSourceUnitModel(model.category) } ...
ComboBox
sees"categoryName"
and shows data, but when I try to access any other role ("model.modelData.name"
) it can not access in any modification of this string.
Here are properties:Q_PROPERTY(Category category READ category WRITE setCategory NOTIFY categoryChanged) Q_PROPERTY(double coefficient READ coefficient WRITE setCoefficient NOTIFY coefficientChanged) Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) Q_PROPERTY(QString categoryName READ categoryName WRITE setCategoryName NOTIFY categoryNameChanged)
error:
TypeError: Cannot read property 'name' of undefined
what is wrong?Example copy-past:
ListView { width: 100; height: 100 model: myModel delegate: Rectangle { height: 25 width: 100 color: model.modelData.color Text { text: name } } }
and in this case when
MyModel
is registered with qqmlcontext property let access to all the roles this way.
So, again, what is wrong with my code? -
That is my QML code instantly,
ComboBox { ... textRole: "categoryName" model: converter.categoryModel onCurrentIndexChanged : { console.log(model.modelData.name) //converter.setSourceUnitModel(model.category) } ...
ComboBox
sees"categoryName"
and shows data, but when I try to access any other role ("model.modelData.name"
) it can not access in any modification of this string.
Here are properties:Q_PROPERTY(Category category READ category WRITE setCategory NOTIFY categoryChanged) Q_PROPERTY(double coefficient READ coefficient WRITE setCoefficient NOTIFY coefficientChanged) Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) Q_PROPERTY(QString categoryName READ categoryName WRITE setCategoryName NOTIFY categoryNameChanged)
error:
TypeError: Cannot read property 'name' of undefined
what is wrong?Example copy-past:
ListView { width: 100; height: 100 model: myModel delegate: Rectangle { height: 25 width: 100 color: model.modelData.color Text { text: name } } }
and in this case when
MyModel
is registered with qqmlcontext property let access to all the roles this way.
So, again, what is wrong with my code?@Kofr AFAIK It works in
ListView
'sdelegate
only because properties of model are made available for it. It won't work outside of the delegate and thus in case ofComboBox
modelData
wont work.
To access the model data you can useget
(for ListModel
) and passing it in your casecurrentIndex
. For C++ model you can write your ownQ_INVOKABLE
function.