Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Exposing QQmlListProperty to QML



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


  • 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");
    

Log in to reply