Advertisement
Guest User

ComplexData c++ to qml

a guest
Sep 30th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.81 KB | None | 0 0
  1. //*************  ComplexData.h
  2.  
  3. #ifndef COMPLEXDATA_H
  4. #define COMPLEXDATA_H
  5.  
  6. #include <QObject>
  7. #include <QString>
  8.  
  9. class ComplexSubDataA : public QObject
  10. {
  11.     Q_OBJECT
  12.  
  13.     Q_PROPERTY(QString nameA READ nameA)
  14.     Q_PROPERTY(int numA READ numA)
  15.  
  16. public:
  17.     ComplexSubDataA(QString name, int num) { m_NameA = name; m_NumA = num; }
  18.  
  19.     QString nameA() const { return m_NameA; }
  20.     int numA() const { return m_NumA; }
  21.  
  22. private:
  23.     QString m_NameA;
  24.     int m_NumA;
  25. };
  26.  
  27. class ComplexSubDataB : public QObject
  28. {
  29.     Q_OBJECT
  30.  
  31.     Q_PROPERTY(QString nameB READ nameB)
  32.     Q_PROPERTY(int numB READ numB)
  33.  
  34. public:
  35.     ComplexSubDataB(QString name, int num) { m_NameB = name; m_NumB = num; }
  36.  
  37.     QString nameB() const { return m_NameB; }
  38.     int numB() const { return m_NumB; }
  39.  
  40. private:
  41.     QString m_NameB;
  42.     int m_NumB;
  43. };
  44.  
  45.  
  46. class ComplexData : public QObject
  47. {
  48.     Q_OBJECT
  49.  
  50.     explicit ComplexData(QObject *parent = nullptr);
  51.  
  52.     Q_PROPERTY(QString name READ name)
  53.  
  54.     Q_PROPERTY(int numA READ numA)
  55.     Q_PROPERTY(int numB READ numB)
  56.  
  57.     Q_PROPERTY(QList<ComplexSubDataA *> cdAList)
  58.     Q_PROPERTY(QList<ComplexSubDataB *> cdBList)
  59.  
  60.     Q_PROPERTY(QStringList stringList)
  61.  
  62. public:
  63.     ComplexData(QString name, int numa, int numb);
  64.  
  65.     QString name() const { return m_Name; }
  66.     int numA() const { return m_NumA; }
  67.     int numB() const { return m_NumB; }
  68.  
  69.     QList<ComplexSubDataA *> cdAList() const ;
  70.     void setCDAList(const QList<ComplexSubDataA *> list);
  71.  
  72.     QList<ComplexSubDataB *> cdBList() const ;
  73.     void setCDBList(const QList<ComplexSubDataB *> list);
  74.  
  75.     QStringList stringList() const { return m_stringList; }
  76.     void setStringList(QStringList stringList) { m_stringList = stringList; }
  77.  
  78. private:
  79.     QString m_Name;
  80.     int m_NumA;
  81.     int m_NumB;
  82.  
  83.     QList<ComplexSubDataA*> m_cdAList;
  84.     QList<ComplexSubDataB*> m_cdBList;
  85.  
  86.     QStringList m_stringList;
  87. };
  88.  
  89. #endif // COMPLEXDATA_H
  90.  
  91. //*************  ComplexData.cpp
  92.  
  93. #include "complexdata.h"
  94.  
  95.  
  96. ComplexData::ComplexData(QObject *parent) : QObject(parent)
  97. {
  98. }
  99.  
  100. ComplexData::ComplexData(QString name, int numa, int numb)
  101. {
  102.     m_Name = name;
  103.     m_NumA = numa;
  104.     m_NumB = numb;
  105. }
  106.  
  107.  
  108. QList<ComplexSubDataA *> ComplexData::cdAList() const
  109. {
  110.     return m_cdAList;
  111. }
  112.  
  113. void ComplexData::setCDAList(const QList<ComplexSubDataA *> list)
  114. {
  115.     m_cdAList = list;
  116. }
  117.  
  118.  
  119. QList<ComplexSubDataB *> ComplexData::cdBList() const
  120. {
  121.     return m_cdBList;
  122. }
  123.  
  124. void ComplexData::setCDBList(const QList<ComplexSubDataB *> list)
  125. {
  126.     m_cdBList = list;
  127. }
  128.  
  129. //*************  main.cpp
  130.  
  131. #include <QGuiApplication>
  132. #include <QQmlApplicationEngine>
  133. #include <QQmlEngine>
  134. #include <QQmlContext>
  135. #include "complexdata.h"
  136.  
  137. int main(int argc, char *argv[])
  138. {
  139.     if (qEnvironmentVariableIsEmpty("QTGLESSTREAM_DISPLAY")) {
  140.         qputenv("QT_QPA_EGLFS_PHYSICAL_WIDTH", QByteArray("213"));
  141.         qputenv("QT_QPA_EGLFS_PHYSICAL_HEIGHT", QByteArray("120"));
  142.  
  143.         QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  144.     }
  145.  
  146.  
  147.     QList<ComplexSubDataA*> cdAList;
  148.     QList<ComplexSubDataB*> cdBList;
  149.  
  150.     ComplexData *lclComplexDataInfo = new ComplexData("ComplexDataTop", 3, 4);
  151.  
  152.     ComplexSubDataA dataA1("1stDataA", 5);
  153.     ComplexSubDataA dataA2("2ndDataA", 7);
  154.     ComplexSubDataA dataA3("3rdDataA", 2);
  155.     cdAList.append(&dataA1);
  156.     cdAList.append(&dataA2);
  157.     cdAList.append(&dataA3);
  158.  
  159.     ComplexSubDataB dataB1("1stDataB", 5);
  160.     ComplexSubDataB dataB2("2ndDataB", 7);
  161.     ComplexSubDataB dataB3("3rdDataB", 2);
  162.     ComplexSubDataB dataB4("4thDataB", 27);
  163.     cdBList.append(&dataB1);
  164.     cdBList.append(&dataB2);
  165.     cdBList.append(&dataB3);
  166.     cdBList.append(&dataB4);
  167.  
  168.     lclComplexDataInfo->setCDAList(cdAList);
  169.     lclComplexDataInfo->setCDBList(cdBList);
  170.  
  171.     QStringList stringInputList;
  172.  
  173.     stringInputList.append("string1");
  174.     stringInputList.append("string2");
  175.     stringInputList.append("string3");
  176.     stringInputList.append("string4");
  177.     stringInputList.append("string5");
  178.  
  179.     lclComplexDataInfo->setStringList(stringInputList);
  180.  
  181.     // qml accessible objects from CanNodeInfo.h
  182.     qmlRegisterUncreatableType<ComplexData>("ComplexDataInfo", 1, 0, "ComplexDataInfo",
  183.        QStringLiteral("ComplexData should not be created in QML."));
  184.     qmlRegisterUncreatableType<ComplexSubDataB>("ComplexSubA", 1, 0, "ComplexSubA",
  185.        QStringLiteral("ComplexSubDataA should not be created in QML."));
  186.     qmlRegisterUncreatableType<ComplexSubDataA>("ComplexSubB", 1, 0, "ComplexSubB",
  187.        QStringLiteral("ComplexSubDataB should not be created in QML."));
  188.  
  189.     QGuiApplication app(argc, argv);
  190.  
  191.     QQmlApplicationEngine engine;
  192.  
  193.     const QUrl url(QStringLiteral("qrc:/main.qml"));
  194.     QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
  195.                      &app, [url](QObject *obj, const QUrl &objUrl) {
  196.         if (!obj && url == objUrl)
  197.             QCoreApplication::exit(-1);
  198.     }, Qt::QueuedConnection);
  199.  
  200.     engine.rootContext()->setContextProperty(QStringLiteral("ComplexDataList"), QVariant::fromValue(lclComplexDataInfo));
  201.  
  202.     engine.load(url);
  203.  
  204.     return app.exec();
  205. }
  206.  
  207. //*************  main.qml
  208.  
  209. import QtQuick 2.15
  210. import QtQuick.Window 2.15
  211. import QtQuick.Controls 2.5
  212. import QtQuick.Layouts 1.3
  213.  
  214. Window {
  215.     width: 640
  216.     height: 480
  217.     visible: true
  218.     title: qsTr("Complex Data Test")
  219.  
  220.     ComboBox {
  221.         id: axisComboBox
  222.         objectName: "objComplexComboBox"
  223.         x: 100
  224.         y: 10
  225.         width: 180
  226.         height: 65
  227.  
  228.         model: ComplexDataList
  229.  
  230.         textRole: "name"
  231.  
  232.         delegate: ItemDelegate {
  233.             id: complexDataSelectorName
  234.  
  235.             Text {
  236.                 id: complexName
  237.                 text: name
  238.             }
  239.         }
  240.  
  241.         Component.onCompleted: currentIndex = 1;
  242.  
  243.         onCurrentIndexChanged: {
  244.             ComplexDataList.model = model.sensorsList;
  245.  
  246.             print("");
  247.             print("");
  248.             print("ComplexComboBox", model)
  249.             print(" currentIndex", currentIndex)
  250.             print("axisComboBox.model", axisComboBox.model);
  251.             print("name", axisComboBox.model.name);
  252.             print("numA", axisComboBox.model.numA);
  253.             print("numB", axisComboBox.model.numB);
  254.  
  255.             print("StringList", axisComboBox.model.stringList)
  256.             print("StringList.length", axisComboBox.model.stringList.length)
  257.  
  258.             print("ComplexAList", axisComboBox.model.cdAList);
  259.             print("ComplexAList.length", axisComboBox.model.cdAList.length);
  260.             print("ComplexAList.numA", axisComboBox.model.cdAList.numA);
  261.  
  262.             print("ComplexBList", axisComboBox.model.cdBList);
  263.             print("ComplexBList.numB", axisComboBox.model.cdBList[0].numB);
  264.          }
  265.     }
  266. }
  267.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement