Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //************* ComplexData.h
- #ifndef COMPLEXDATA_H
- #define COMPLEXDATA_H
- #include <QObject>
- #include <QString>
- class ComplexSubDataA : public QObject
- {
- Q_OBJECT
- Q_PROPERTY(QString nameA READ nameA)
- Q_PROPERTY(int numA READ numA)
- public:
- ComplexSubDataA(QString name, int num) { m_NameA = name; m_NumA = num; }
- QString nameA() const { return m_NameA; }
- int numA() const { return m_NumA; }
- private:
- QString m_NameA;
- int m_NumA;
- };
- class ComplexSubDataB : public QObject
- {
- Q_OBJECT
- Q_PROPERTY(QString nameB READ nameB)
- Q_PROPERTY(int numB READ numB)
- public:
- ComplexSubDataB(QString name, int num) { m_NameB = name; m_NumB = num; }
- QString nameB() const { return m_NameB; }
- int numB() const { return m_NumB; }
- private:
- QString m_NameB;
- int m_NumB;
- };
- class ComplexData : public QObject
- {
- Q_OBJECT
- explicit ComplexData(QObject *parent = nullptr);
- Q_PROPERTY(QString name READ name)
- Q_PROPERTY(int numA READ numA)
- Q_PROPERTY(int numB READ numB)
- Q_PROPERTY(QList<ComplexSubDataA *> cdAList)
- Q_PROPERTY(QList<ComplexSubDataB *> cdBList)
- Q_PROPERTY(QStringList stringList)
- public:
- ComplexData(QString name, int numa, int numb);
- QString name() const { return m_Name; }
- int numA() const { return m_NumA; }
- int numB() const { return m_NumB; }
- QList<ComplexSubDataA *> cdAList() const ;
- void setCDAList(const QList<ComplexSubDataA *> list);
- QList<ComplexSubDataB *> cdBList() const ;
- void setCDBList(const QList<ComplexSubDataB *> list);
- QStringList stringList() const { return m_stringList; }
- void setStringList(QStringList stringList) { m_stringList = stringList; }
- private:
- QString m_Name;
- int m_NumA;
- int m_NumB;
- QList<ComplexSubDataA*> m_cdAList;
- QList<ComplexSubDataB*> m_cdBList;
- QStringList m_stringList;
- };
- #endif // COMPLEXDATA_H
- //************* ComplexData.cpp
- #include "complexdata.h"
- ComplexData::ComplexData(QObject *parent) : QObject(parent)
- {
- }
- ComplexData::ComplexData(QString name, int numa, int numb)
- {
- m_Name = name;
- m_NumA = numa;
- m_NumB = numb;
- }
- QList<ComplexSubDataA *> ComplexData::cdAList() const
- {
- return m_cdAList;
- }
- void ComplexData::setCDAList(const QList<ComplexSubDataA *> list)
- {
- m_cdAList = list;
- }
- QList<ComplexSubDataB *> ComplexData::cdBList() const
- {
- return m_cdBList;
- }
- void ComplexData::setCDBList(const QList<ComplexSubDataB *> list)
- {
- m_cdBList = list;
- }
- //************* main.cpp
- #include <QGuiApplication>
- #include <QQmlApplicationEngine>
- #include <QQmlEngine>
- #include <QQmlContext>
- #include "complexdata.h"
- int main(int argc, char *argv[])
- {
- if (qEnvironmentVariableIsEmpty("QTGLESSTREAM_DISPLAY")) {
- qputenv("QT_QPA_EGLFS_PHYSICAL_WIDTH", QByteArray("213"));
- qputenv("QT_QPA_EGLFS_PHYSICAL_HEIGHT", QByteArray("120"));
- QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
- }
- QList<ComplexSubDataA*> cdAList;
- QList<ComplexSubDataB*> cdBList;
- ComplexData *lclComplexDataInfo = new ComplexData("ComplexDataTop", 3, 4);
- ComplexSubDataA dataA1("1stDataA", 5);
- ComplexSubDataA dataA2("2ndDataA", 7);
- ComplexSubDataA dataA3("3rdDataA", 2);
- cdAList.append(&dataA1);
- cdAList.append(&dataA2);
- cdAList.append(&dataA3);
- ComplexSubDataB dataB1("1stDataB", 5);
- ComplexSubDataB dataB2("2ndDataB", 7);
- ComplexSubDataB dataB3("3rdDataB", 2);
- ComplexSubDataB dataB4("4thDataB", 27);
- cdBList.append(&dataB1);
- cdBList.append(&dataB2);
- cdBList.append(&dataB3);
- cdBList.append(&dataB4);
- lclComplexDataInfo->setCDAList(cdAList);
- lclComplexDataInfo->setCDBList(cdBList);
- QStringList stringInputList;
- stringInputList.append("string1");
- stringInputList.append("string2");
- stringInputList.append("string3");
- stringInputList.append("string4");
- stringInputList.append("string5");
- lclComplexDataInfo->setStringList(stringInputList);
- // qml accessible objects from CanNodeInfo.h
- qmlRegisterUncreatableType<ComplexData>("ComplexDataInfo", 1, 0, "ComplexDataInfo",
- QStringLiteral("ComplexData should not be created in QML."));
- qmlRegisterUncreatableType<ComplexSubDataB>("ComplexSubA", 1, 0, "ComplexSubA",
- QStringLiteral("ComplexSubDataA should not be created in QML."));
- qmlRegisterUncreatableType<ComplexSubDataA>("ComplexSubB", 1, 0, "ComplexSubB",
- QStringLiteral("ComplexSubDataB should not be created in QML."));
- QGuiApplication app(argc, argv);
- QQmlApplicationEngine engine;
- 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.rootContext()->setContextProperty(QStringLiteral("ComplexDataList"), QVariant::fromValue(lclComplexDataInfo));
- engine.load(url);
- return app.exec();
- }
- //************* main.qml
- import QtQuick 2.15
- import QtQuick.Window 2.15
- import QtQuick.Controls 2.5
- import QtQuick.Layouts 1.3
- Window {
- width: 640
- height: 480
- visible: true
- title: qsTr("Complex Data Test")
- ComboBox {
- id: axisComboBox
- objectName: "objComplexComboBox"
- x: 100
- y: 10
- width: 180
- height: 65
- model: ComplexDataList
- textRole: "name"
- delegate: ItemDelegate {
- id: complexDataSelectorName
- Text {
- id: complexName
- text: name
- }
- }
- Component.onCompleted: currentIndex = 1;
- onCurrentIndexChanged: {
- ComplexDataList.model = model.sensorsList;
- print("");
- print("");
- print("ComplexComboBox", model)
- print(" currentIndex", currentIndex)
- print("axisComboBox.model", axisComboBox.model);
- print("name", axisComboBox.model.name);
- print("numA", axisComboBox.model.numA);
- print("numB", axisComboBox.model.numB);
- print("StringList", axisComboBox.model.stringList)
- print("StringList.length", axisComboBox.model.stringList.length)
- print("ComplexAList", axisComboBox.model.cdAList);
- print("ComplexAList.length", axisComboBox.model.cdAList.length);
- print("ComplexAList.numA", axisComboBox.model.cdAList.numA);
- print("ComplexBList", axisComboBox.model.cdBList);
- print("ComplexBList.numB", axisComboBox.model.cdBList[0].numB);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement