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. Accessing QObject class properties which is stored in QList
Forum Updated to NodeBB v4.3 + New Features

Accessing QObject class properties which is stored in QList

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmlqpropertyqtquickqtquick2
4 Posts 2 Posters 1.9k 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.
  • P Offline
    P Offline
    pra7
    wrote on last edited by
    #1

    Hello, I am having trouble in accessing a property in QML.

    QList<ClassB> is a property and i am able to access that but when i try to access the properties of classB in QML i am getting type error/Undefined. Following is the code:

    //ClassA.h

    #include <QObject>
    #include "classb.h"
    
    Q_DECLARE_METATYPE(QList<ClassB*>)
    class ClassA : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QList<ClassB*> objList READ getClassBDetails)
    
    public:
        explicit ClassA(QObject *parent = nullptr);
    
        QList<ClassB *>getClassBDetails();
    
    private:
        QList<ClassB*> m_list;
        ClassB m_Bobj;
    
    };
    

    //ClassA.cpp

    #include "classa.h"
    
    ClassA::ClassA(QObject *parent) : QObject(parent)
    {
        m_list.append(&m_Bobj);
    }
    
    QList<ClassB *> ClassA ::getClassBDetails()
    {
        return m_list;
    }
    

    //ClassB.h

    #include <QObject>
    
    class ClassB : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(int val READ getval WRITE setval NOTIFY valChanged)
    public:
        explicit ClassB(QObject *parent = nullptr);
    
        int m_val = 10;
    
        int getval();
        void setval(int val);
    
    signals:
        void valChanged();
    
    public slots:
    };
    

    //ClassB.cpp

    #include "classb.h"
    ClassB::ClassB(QObject *parent) : QObject(parent)
    {
    
    }
    
    int ClassB::getval()
    {
        return m_val;
    }
    
    void ClassB::setval(int val)
    {
        m_val = val;
        emit valChanged();
    }
    

    //Main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    #include "classa.h"
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
    
        ClassA aObj;
        QQmlApplicationEngine engine;
        engine.rootContext()->setContextProperty("classAObj",&aObj);
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    }
    

    //Main.qml

    import QtQuick 2.6
    import QtQuick.Window 2.2
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        Component.onCompleted: {
            console.log("value========",classAObj.objList[0].val)
        }
    }
    

    If i try to access "classAObj.objList[0].val" i am getting TypeError: Cannot read property 'val' of undefined error. What is that i am doing wrong here ?

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

      Hi,

      You didn't registrer ClassB to be used by QML.

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

      P 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        You didn't registrer ClassB to be used by QML.

        P Offline
        P Offline
        pra7
        wrote on last edited by
        #3

        @SGaist Which one I should use Q_DECLARE_METATYPE or registermetatype ? And ClassB is already a QObject so I thought it's not required .

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

          ClassB, you are using it in a property.

          By the way, why not use QObjectList and a Q_INVOKABLE method ? Might be more straightforward.

          More information on how to integrate CPP with QML here.

          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

          • Login

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