Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to set qml listview model in C++
Forum Updated to NodeBB v4.3 + New Features

How to set qml listview model in C++

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 7.2k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    apap_
    wrote on last edited by
    #1

    Hi,

    I dynamically create a custom qml listview in my C++ and it works great, but I can't set the model. Like others listview properties, I tried with "setProperty" but at runtime it can't find the model roles :
    @
    const MyListModel* myModel = new MyListModel();
    myListView->setProperty("model", myModel);
    @

    Someone has an idea ?

    Regards

    1 Reply Last reply
    0
    • T Offline
      T Offline
      thisisbhaskar
      wrote on last edited by
      #2

      When you say you dynamically create a custom qml listview, do you have a ListView component in your qml file and creating an instance of it in your c++ code?? I don't see you using QDeclarativeCompoenent to load the component and create an object out of it?

      Generally this is how it is done.
      @
      QDeclarativeView view;
      QDeclarativeEngine *engine = view.engine();
      QDeclarativeComponent component(engine, QUrl::fromLocalFile("listview.qml"));

      QObject *myObject = component.create();
      myObject->setProperty("model",myModel);
      @

      1 Reply Last reply
      0
      • A Offline
        A Offline
        apap_
        wrote on last edited by
        #3

        Yes I do this, but my problem is that the model roles can't be found at runtime.
        Which type of model do you use in your example ?
        For my part, I use a derived class of QAbstractListModel.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          thisisbhaskar
          wrote on last edited by
          #4

          Ok.. I had a QList<QObject*> which I have set as model.

          @
          contactsList.append(new contactDetails(firstName, phoneNumber));

          ctxt->setContextProperty("favouritesModel", QVariant::fromValue(contactsList));
          @

          where contactsList is

          @QList<QObject*> contactsList;
          @

          and contactDetails is a QObject derived class with two properties contactName and contactNumber.

          I have not tried to set QAbstractListModel as a model.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            srikanth_trulyit
            wrote on last edited by
            #5

            You should have the roles set to your c++ model using setRoles. In QML simply specify the rolename in the model delegate

            @
            ListView {
            id : myListView;
            ....
            deletegate: myDelegate;

            Component {
            id: myDelegate;
            Text {
            id: myText
            text: myCustomTextRole // this is the name of the role you should handle in data() of model class
            }
            }
            }
            @

            1 Reply Last reply
            0
            • A Offline
              A Offline
              apap_
              wrote on last edited by
              #6

              I solved my problem, I was trying to set directly a pointer to the "model" property of the qml listview but it can't be done like this. The solution is to use the QDeclarativeContext :
              @
              QDeclarativeEngine *engine = new QDeclarativeEngine();
              QDeclarativeContext context = engine->rootContext();
              MyListModel
              myModel = new MyListModel();
              context->setContextProperty("myModelName", myModel);
              myQMLListView->setProperty("model", context->contextProperty("myModelName"));
              @

              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