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. How to pass QAbstractListModel derived class to QML using setProperty?
Forum Updated to NodeBB v4.3 + New Features

How to pass QAbstractListModel derived class to QML using setProperty?

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 4.1k 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.
  • K Offline
    K Offline
    kappa
    wrote on last edited by
    #1

    Hello!

    On QML side I have an element, that contains a ListView plus some additional elements like scroll bars, control buttons etc.
    On C++ side I have a QObjectList of some items that I'm passing to my QML object using:
    @
    QObject* qObject = myView->findObject<QObject*>("myObjectName");
    qObject->setProperty("myModel", QVariant::fromValue((QObjectList) *pointerToMyList);
    @

    Now I need to change my QObjectList to QAbstractListModel derived class, but I cannot find a way to pass a pointer to QML using qObject->setProperty. All the examples I saw, use the setContextProperty. I want to avoid that, because the name of my qObject I am receiving via network and I can have many ListViews on a single page. So using my way, I'm just finding a correct QML object on the view and then change its model.

    So finally the question is: how can I pass the pointer (to the object of class derived from QAbstractListModel) to QML using qObject->setProperty method?

    Thanks in advance,
    kappa

    1 Reply Last reply
    0
    • frederikF Offline
      frederikF Offline
      frederik
      wrote on last edited by
      #2

      You need to register QAbstractItemModel as qml type in C++:
      @qmlRegisterType<QAbstractItemModel>();@

      From then on it should work.
      Make sure you declare the property like this:
      @Q_PROPERTY(QAbstractItemModel *myModel READ myModel NOTIFY modelChanged)@

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kappa
        wrote on last edited by
        #3

        Thank you!

        I just also needed to add

        @Q_DECLARE_METATYPE(CListModel* )@

        to .h file.

        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