Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [solved] Can we access ListView model Role In QML ?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 2 Posters 2.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rafaelSorel
    wrote on last edited by
    #1

    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
    @

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      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
      @

      157

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rafaelSorel
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          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.

          157

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rafaelSorel
            wrote on last edited by
            #5

            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 ?

            1 Reply Last reply
            0
            • p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

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

              157

              1 Reply Last reply
              0
              • R Offline
                R Offline
                rafaelSorel
                wrote on last edited by
                #7

                Well thanks it worked like a charm.

                1 Reply Last reply
                0
                • p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on last edited by
                  #8

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

                  157

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved