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. Exporting C++ QList<int> to QML
Qt 6.11 is out! See what's new in the release blog

Exporting C++ QList<int> to QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 2 Posters 8.0k Views 2 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.
  • F Offline
    F Offline
    FreeRollen
    wrote on last edited by
    #1

    I am new to qt quick and I a create a simple C++ class with Q_PROPERTY(QList<int> age READ age ). I want to be able to access the age.count from my qml so and can print all the ages out but i am getting an error saying.. qrc:/settings.qml:26: TypeError: Property 'count' of object 1,2,3,5 is not a function;

    #ifndef PERSON_H
    #define PERSON_H
    
    #include <QObject>
    
    class Person : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
        Q_PROPERTY(int shoeSize READ shoeSize WRITE setShoeSize)
        Q_PROPERTY(QList<int> age READ age  )
    
    public:
        Person(QObject *parent = 0);
    
        QString name() const;
        void setName(const QString &);
    
    
        int shoeSize() const;
        void setShoeSize(int);
    
        QList<int> age();
    
    private:
        QString m_name;
        int m_shoeSize;
        QList<int> m_age;
    
     signals:
        void nameChanged();
    };
    
    #endif // PERSON_H
    
    
    #include "person.h"
    #include <QList>
    
    
    Person::Person(QObject *parent)
    : QObject(parent), m_shoeSize(0)
    {
        setName("ERNIE");
        m_age.append( 1 );
        m_age.append( 2 );
        m_age.append( 3 );
        m_age.append( 5 );
    
    }
    
    QString Person::name() const
    {
        return m_name;
    }
    
    void Person::setName(const QString &n)
    {
        m_name = n;
        emit nameChanged();
    }
    
    int Person::shoeSize() const
    {
        return m_shoeSize;
    }
    
    void Person::setShoeSize(int s)
    {
        m_shoeSize = s;
    }
    
    QList<int> Person::age()
    {
        return m_age;
    }
    
    
    Rectangle
    {
        id:settingsWindow
        visible: true
        //visibility: "FullScreen"
        width: 800
        height: 480
        color:"#71469A"
        anchors.top: parent.top
        anchors.topMargin: 35
    
        Component.onCompleted: {
             for (var i=0; i<3; i++)
                 console.log("here")
    
             console.log("index 0: " + mPeeps.age[0])
             console.log("index 1: " + mPeeps.age[2])
             console.log("index 3: " + mPeeps.age[3])
    
             console.log("count: " + mPeeps.age.count() )
    
             //for (var w=0; w<mPeeps.age.count(); w++)
            //     console.log("items[i]")
         }
    
    }
    
    
    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by p3c0
      #2

      @FreeRollen
      The CPP QList of variants gets converted to Javascript Arrays automcatically when exposed to QML. So since in QML it is identified as an array, there is no Javascript count method but length. So use that instead.
      More info here:
      http://doc.qt.io/qt-5/qtqml-cppintegration-data.html#sequence-type-to-javascript-array

      157

      1 Reply Last reply
      2

      • Login

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