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 access Qlist of structure elements in QML
Forum Updated to NodeBB v4.3 + New Features

How to access Qlist of structure elements in QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
1 Posts 1 Posters 234 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.
  • P Offline
    P Offline
    Praveen.Illa
    wrote on last edited by
    #1

    Hi Team,

    How can I access Qlist of struct elements in QML.
    I have implemented as follows, but the output is not working as expected.
    Can some one please help me how to get the values of qlist struct elements in qml

    sample.cpp

    #include "sample.h"
    
    int xVal[5] = {1,2,3,4,5};
    int yVal[5] = {6,7,8,9,10};
    
    Sample::Sample(QObject *parent) : QObject(parent)
    {
    
    }
    
    void Sample::prepareList()
    {
        listOfObjects obj;
    
        for(int iLoop = 0; iLoop < 5; iLoop++)
        {
            obj.xVal = xVal[iLoop];
            obj.yval = yVal[iLoop];
    
            listObj.append(obj);
        }
    }
    
    QVariant Sample::getList()
    {
        return QVariant::fromValue(listObj);
    }
    

    sample.h

    #ifndef SAMPLE_H
    #define SAMPLE_H
    
    typedef struct
    {
        int xVal;
        int yval;
    }listOfObjects;
    Q_DECLARE_METATYPE(listOfObjects);
    
    class Sample : public QObject
    {
        Q_OBJECT
    public:
        explicit Sample(QObject *parent = nullptr);
    
        Q_PROPERTY(QVariant varlist READ getList)
    
    public slots:
        void prepareList();
        void printList();
    
    
    private:
        QList<listOfObjects> listObj;
        QVariant getList();
    };
    
    #endif // SAMPLE_H
    

    main.cpp

    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
    
        qmlRegisterType<Sample>("Sample", 1, 0, "SampleObj");
    
        const QUrl url(QStringLiteral("qrc:/main.qml"));
        QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                         &app, [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        }, Qt::QueuedConnection);
        engine.load(url);
    
        return app.exec();
    }
    

    main.qml

    import QtQuick 2.12
    import QtQuick.Window 2.12
    
    import Sample 1.0
    
    Window {
        id: mainWindow
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        property var obj: ({})
    
        SampleObj {
            id: sampleId
        }
    
        Component.onCompleted: {
            sampleId.prepareList()
            obj = sampleId.varlist
    
            for(var i = 0; i < obj.length; i++) {
                console.log(obj[i]) //Expected: console.log("(x, y) = " + obj[i].xVal, obj[i].yVal )
            }
        }
    }
    

    The console log is giving the outputs as
    qml: QVariant(listOfObjects, )
    qml: QVariant(listOfObjects, )
    qml: QVariant(listOfObjects, )
    qml: QVariant(listOfObjects, )
    qml: QVariant(listOfObjects, )

    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