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. Exposing QQmlListProperty to QML
Qt 6.11 is out! See what's new in the release blog

Exposing QQmlListProperty to QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 524 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.
  • J Offline
    J Offline
    James A
    wrote on last edited by
    #1

    I am trying to expose C++ class containing QQmlListProperty to QML and displaying the data using Reapter as shown below. But I am unable to view the Data in the qml page

     Row{
            Repeater{
                model: guests
                delegate: Text {
    
                    text: name
                }
            }
        }
    

    main.cpp

     QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
    
        QQmlContext* context = engine.rootContext();
    
     qRegisterMetaType<QQmlListProperty<Person>>("QQmlListProperty<Person>");
    
        BirthdayParty party;
    
        party.appendGuest(new Person("abc",123));
        party.appendGuest(new Person("xyz",123));
    
        context->setContextObject(&party);
    
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
        return app.exec();
    

    I am using the below class for exposing to qml
    birthdaypart.h

    class BirthdayParty : public QObject
    {
        Q_OBJECT
    
        Q_PROPERTY(Person *host READ host WRITE setHost)
        Q_PROPERTY(QQmlListProperty<Person> guests READ guests NOTIFY guestsChanged)
    
        QML_ELEMENT
    public:
        BirthdayParty(QObject *parent = nullptr);
    
        Person *host() const;
        void setHost(Person *);
    
        QQmlListProperty<Person> guests();
        void appendGuest(Person*);
        int guestCount() const;
        Person *guest(int) const;
        void clearGuests();
        void replaceGuest(int, Person*);
        void removeLastGuest();
    
    signals:
        void guestsChanged(QQmlListProperty<Person> guests);
    
    private:
        static void appendGuest(QQmlListProperty<Person>*, Person*);
        static int guestCount(QQmlListProperty<Person>*);
        static Person* guest(QQmlListProperty<Person>*, int);
        static void clearGuests(QQmlListProperty<Person>*);
        static void replaceGuest(QQmlListProperty<Person>*, int, Person*);
        static void removeLastGuest(QQmlListProperty<Person>*);
    
        Person *m_host;
        QVector<Person *> m_guests;
    };
    

    birthdayparty.cpp

    
    Person *BirthdayParty::host() const
    {
        return m_host;
    }
    
    void BirthdayParty::setHost(Person *c)
    {
        m_host = c;
    }
    
    QQmlListProperty<Person> BirthdayParty::guests()
    {
        return {this, this,
                 &BirthdayParty::appendGuest,
                 &BirthdayParty::guestCount,
                 &BirthdayParty::guest,
                 &BirthdayParty::clearGuests,
                 &BirthdayParty::replaceGuest,
                 &BirthdayParty::removeLastGuest};
    }
    
    void BirthdayParty::appendGuest(Person* p) {
        m_guests.append(p);
    }
    
    
    int BirthdayParty::guestCount() const
    {
        return m_guests.count();
    }
    
    Person *BirthdayParty::guest(int index) const
    {
        return m_guests.at(index);
    }
    
    void BirthdayParty::clearGuests() {
        m_guests.clear();
    }
    
    void BirthdayParty::replaceGuest(int index, Person *p)
    {
        m_guests[index] = p;
    }
    
    void BirthdayParty::removeLastGuest()
    {
        m_guests.removeLast();
    }
    
    // ![0]
    
    void BirthdayParty::appendGuest(QQmlListProperty<Person>* list, Person* p) {
        reinterpret_cast< BirthdayParty* >(list->data)->appendGuest(p);
    }
    
    void BirthdayParty::clearGuests(QQmlListProperty<Person>* list) {
        reinterpret_cast< BirthdayParty* >(list->data)->clearGuests();
    }
    
    void BirthdayParty::replaceGuest(QQmlListProperty<Person> *list, int i, Person *p)
    {
        reinterpret_cast< BirthdayParty* >(list->data)->replaceGuest(i, p);
    }
    
    void BirthdayParty::removeLastGuest(QQmlListProperty<Person> *list)
    {
        reinterpret_cast< BirthdayParty* >(list->data)->removeLastGuest();
    }
    
    Person* BirthdayParty::guest(QQmlListProperty<Person>* list, int i) {
        return reinterpret_cast< BirthdayParty* >(list->data)->guest(i);
    }
    
    int BirthdayParty::guestCount(QQmlListProperty<Person>* list) {
        return reinterpret_cast< BirthdayParty* >(list->data)->guestCount();
    }
    
    

    Person class contains two fileds

      Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
      Q_PROPERTY(int shoseSize READ shoseSize WRITE setShoseSize NOTIFY shoseSizeChanged)
    
    1 Reply Last reply
    0
    • J Offline
      J Offline
      James A
      wrote on last edited by
      #2

      I am able to fix the issue by adding the below lines in the main.cpp file

        //    qRegisterMetaType<QQmlListProperty<Person>>("QQmlListProperty<Person>");
          qmlRegisterType<Person>("Person", 1, 0, "Person");
          qmlRegisterType<BirthdayParty>("BirthdayParty", 1, 0, "BirthdayParty");
      
      1 Reply Last reply
      1

      • Login

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