Home > Blockchain >  How to access Qlist of structure elements in QML
How to access Qlist of structure elements in QML

Time:03-09

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

CodePudding user response:

use QVariantList instead of QVariant for the return type

code like below

.pro file

QT  = quick qml



SOURCES  = \
        ctestforqml.cpp \
        main.cpp

resources.files = main.qml 
resources.prefix = /$${TARGET}
RESOURCES  = resources \
    Resources.qrc

CONFIG  = qmltypes
QML_IMPORT_NAME = com.demo.cppobject
QML_IMPORT_MAJOR_VERSION = 1

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS  = target

HEADERS  = \
    ctestforqml.h

ctestforqml.h

#ifndef CTESTFORQML_H
#define CTESTFORQML_H

#include <QObject>
#include <QQmlEngine>

class CTestforQML : public QObject
{
    Q_OBJECT
    QML_ELEMENT
public:
    Q_INVOKABLE QVariantList getData() const;

public:
    explicit CTestforQML(QObject *parent = nullptr);

signals:

};

#endif // CTESTFORQML_H

ctestforqml.cpp

#include "ctestforqml.h"
#include <QJsonObject>

QVariantList CTestforQML::getData() const
{
    QVariantList list;
    QJsonObject json;
    for (int i = 0; i < 10; i   )
    {
        json.insert("name", "demo"   QString::number(i));
        json.insert("value", QString::number(i));
        list.append(json);
    }
    return list;
}

CTestforQML::CTestforQML(QObject *parent)
    : QObject{parent}
{

}

main.cpp

import QtQuick

import com.demo.cppobject

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    CTestforQML {
        id: testForQml
    }

    Component.onCompleted: {
        let val = testForQml.getData()
        for (let i = 0; i < val.length; i   )
        {
            console.log(val[i].name, val[i].value);
        }
    }
}
  • Related