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 expose QAbstractListModel derived model from QObject to QML?
QtWS25 Last Chance

How to expose QAbstractListModel derived model from QObject to QML?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmlabstractlistmodel
3 Posts 2 Posters 2.6k 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.
  • L Offline
    L Offline
    ludek.vodicka
    wrote on last edited by
    #1

    I would like to know if there is any macro or way how to register Qt model as property of QObject for QML.

    For example, I have AnimalModel (http://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html#qabstractitemmodel).

    I Know I can pass it to root context of QuickView

    QuickView view;
    view.rootContext()->setContextProperty("myModel", &model);
    

    In case I have QObject registered via Qml macros, I can pass this object to view too:

    view.rootContext()->setContextProperty("obj", pDataObject);
    

    But what If I want to have QObject which holds model of any data?

    For example:

    class DataObject : public QObject
    {
        Q_OBJECT
    
        Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
        Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged)
        ...
    
        AnimalModel m_modelAnimals;
    
        //Is this possible in any way?
        //Q_PROPERTY(AnimalModel modelAnimals READ modelAnimals NOTIFY modelAnimalsChanged)
    };
    
    

    Every example I found until now shows how to pass QAbstractListModel to root context. But none how to use it as QObject property.

    (I know there is QQmlListProperty but QQmlListProperty doesn't support partial refresh. It's always necessary to rebuild all Qml objects)

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Yes you can, using e.g. a Q_INVOKABLE getter or creating a Q_PROPERTY for your model.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • L Offline
        L Offline
        ludek.vodicka
        wrote on last edited by
        #3

        Thanks for reply. I already solved it too.

        It's necessary to return pointer to AnimalModel and not reference. Also there is no necessity for NOTIFY.

        So this is the correct way:

        Q_PROPERTY(AnimalModel * modelAnimals READ modelAnimals CONSTANT)
        AnimalModel* modelAnimals() { return &m_model;}
        
        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