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



  • 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
    @


  • Moderators

    Hi,

    Welcome to the Forum.
    You must create setter and getter functions for properties type and size in Animal class.
    Also you need to reimplement "data":http://qt-project.org/doc/qt-5/qabstractitemmodel.html#data method.
    Later you can access it like:
    @
    currentList.currentItem.type
    currentList.currentItem.size
    @



  • Thx for your fast reply,
    By that you mean to create setter and getter function in C++ AnimalModel Class thought Q_INVOKABLE function ?


  • Moderators

    No need to create it as Q_INVOKABLE. Just plain methods in Animal class. Also in the constructor of Animal class set the type and size.



  • You mean add the setter and getter in AnimalModel class not Animal Class ?
    And after that how to access the type and size in Qml file ?


  • Moderators

    Well i suppose you are using one of the examples from Qt.
    I checked that one. If you are using the AnimalModel and AnimalClass exactly as it is no need to create setter and getter, it already has and the values are already set.

    Now to access type and size create two Q_INVOKABLE functions in AnimalModel class
    for eg.
    @
    Q_INVOKABLE QString getCurrentType(int index);
    Q_INVOKABLE QString getCurrentSize(int index);
    @
    In its definition return the size and type of the corresponding index that you require(you'll get those from the m_animals list)

    Now the main part i.e to get those properties from the QML you'll need to access those methods,
    @
    listView.model.getCurrentType(listView.currentIndex)
    listView.model.getCurrentSize(listView.currentIndex)
    @



  • Well thanks it worked like a charm.


  • Moderators

    You're Welcome :)
    You can mark the post as solved. Just edit the post title and prepend [solved]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.