Quantcast
Channel: Qt DevNet forums: Qt Quick 1283365070**
Viewing all articles
Browse latest Browse all 4972

[solved] Can we access ListView model Role In QML ?

$
0
0
From example presented in here http://qt-project.org/doc/qt-5/qtquick-modelviewsdata-cppmodels.html: a model has been defined in model.cpp : class Animal { public:     Animal(const QString &type, const QString &size);     ... };   class AnimalModel : public QAbstractListModel {     Q_OBJECT public:     enum AnimalRoles {         TypeRole = Qt::UserRole + 1,         SizeRole     };       AnimalModel(QObject *parent = 0);     ... };   QHash<int, QByteArray> AnimalModel::roleNames() const {     QHash<int, QByteArray> roles;     roles[TypeRole] = "type";     roles[SizeRole] = "size";     return roles; }   int main(int argc, char ** argv) {     QGuiApplication app(argc, argv);       AnimalModel model;     model.addAnimal(Animal("Wolf", "Medium"));     model.addAnimal(Animal("Polar bear", "Large"));     model.addAnimal(Animal("Quoll", "Small"));       QQuickView view;     view.setResizeMode(QQuickView::SizeRootObjectToView);     QQmlContext *ctxt = view.rootContext();     ctxt->setContextProperty("myModel", &model);     ...- And here is the qml file : ListView {     id: currentList     width: 200; height: 250       model: myModel     delegate: Text { text: "Animal: " + type + ", " + size } } The problem is that I cannot access to the “type” or “size” Role from outside ListView . The only access that I could make is the whole text : currentList.currentItem.text My question is how can I access the type and size role of myModel, may be something like this (of course this does not work): currentList.currentItem.text.type currentList.currentItem.text.size

Viewing all articles
Browse latest Browse all 4972

Trending Articles