title: Best way to access a cpp structure in QML
body: <p>I need to pass structures between cpp and QML. If i use <strong>property</strong> i should create an individual set and get functions, My structure contains minimum 5 members so i felt it's not good to use set and get for all those members.
  Following is an example of what i am trying to do :</p>
  
  <p><strong>MyClass.h</strong></p>
  
  <pre><code>#include &lt;QObject&gt;
  #include &lt;QDebug&gt;
  using namespace std;
  
  struct MyStruct {
  Q_GADGET
  int m_val;
  QString m_name1;
  QString m_name2;
  QString m_name3;
  QString m_name4;
  Q_PROPERTY(int val MEMBER m_val)
  Q_PROPERTY(QString name1 MEMBER m_name1)
  Q_PROPERTY(QString name2 MEMBER m_name2)
  Q_PROPERTY(QString name3 MEMBER m_name3)
  Q_PROPERTY(QString name4 MEMBER m_name4)
  };
  
  class MyClass:public QObject
      {
          Q_OBJECT
      Q_PROPERTY(MyStruct mystr READ getMyStruct
                  WRITE setMyStruct NOTIFY myStructChanged)
  
  public:
      explicit MyClass(QObject *parent = nullptr);
      MyStruct strObj;
  
       // Edit: changed get function
       MyStruct getMyStruct() const
       {
           return strObj;
       }
  
  // Edit: Added set function
       void setMyStruct(myStruct val)
          {
              mystr = val;
              emit myStructChanged();
          }
  
  signals:
  void myStructChanged();
  
  }
  </code></pre>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include &lt;QGuiApplication&gt;
  #include &lt;QQmlApplicationEngine&gt;
  #include &lt;QQmlContext&gt;
  #include &lt;QDebug&gt;
  #include &lt;QObject&gt;
  
  #include "MyClass.h"
  
  int main(int argc, char *argv[])
  {
      QGuiApplication app(argc, argv);
      QQmlApplicationEngine engine;
  
      MyClass classObj;
  
      engine.rootContext()-&gt;setContextProperty("classObj",&amp;classObj);
  
      engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
  
      if (engine.rootObjects().isEmpty())
          return -1;
  
      return app.exec();
  }
  </code></pre>
  
  <p><strong>Main.qml</strong></p>
  
  <pre><code>import QtQuick 2.6
  import QtQuick.Controls 2.2
  import QtQuick.Window 2.3
  
  ApplicationWindow {
  
      id: applicationWindow
  
      visible: true
      width: 600
      height: 400
      title: qsTr("My App")
  
      MainForm{
          id : mainform
  
          Component.onCompleted: {
          console.log("name===="+classObj.mystr.name1)
  
          //EDIT added more code to explain the use case.
          classObj.myStr.name1 = "abc"  //Calls setter 
          classObj.mystr.name2 = "ans" // Calls setter 
          }
      }
  }
  </code></pre>
  
  <p>If i print just <code>(classObj.myVariant)</code> i am getting <strong>QVariant(MyStruct)</strong> but when i tried to access any parameter like <code>classObj.myVariant.name1</code> i am getting "<strong>undefined</strong>" and also how to set a variant from QML? </p>
  
  <p><strong>[UPDATE]</strong> - Should also add MyStruct to <strong>Q_DECLARE_METATYPE</strong> as below: <strong>Q_DECLARE_METATYPE(MyStruct)</strong></p>