Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Access QVariant's QMetaObject in QML
QtWS25 Last Chance

Access QVariant's QMetaObject in QML

Scheduled Pinned Locked Moved Unsolved General and Desktop
qmlqvariantqvariantlist qlqmetaobject
3 Posts 2 Posters 1.2k 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.
  • R Offline
    R Offline
    raphasauer
    wrote on 17 Jul 2021, 13:11 last edited by
    #1

    Hello everyone. Is it possible to access a QVariant's QMetaObject in QML? I have a QVariantList that I load in my QML like this:

    engine.rootContext()->setContextProperty("myList", list);
    

    I can interate through each QVariant like this:

    function startupFunction() {
    
                for(var i = 0; i < variantList.length; i++)
                {
                    console.log(variantList[i]);
                }
    
            }
    

    In C++, it's easy to access this meta object, it can be done like this:

    var.value<Player>().staticMetaObject;
    

    How could I do it on QML?

    Here's my code:

    Main:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <example.h>
    #include <QQmlContext>
    
    int main(int argc, char *argv[])
    {
    #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    #endif
    
        QGuiApplication app(argc, argv);
    
        qRegisterMetaType<Player>();
        qRegisterMetaType<P2D>();
        qRegisterMetaType<P2D*>();
    
        Player p1;
        p1.setSpeed(15);
        p1.setCarType(Player::CarType::NO_CAR);
    
        P2D* ptr = new P2D();
    
        ptr->setX(10);
    
        ptr->setY(25);
    
        p1.setP2D(ptr);
    
    
        Player p2;
        p2.setSpeed(150);
        p2.setCarType(Player::CarType::FAST_CAR);
    
        P2D* ptr2 = new P2D();
    
        ptr->setX(50);
    
        ptr->setY(60);
    
        p2.setP2D(ptr2);
    
    
        QVariantList list;
    
        QVariant var;
    
        var.setValue(p1);
    
        list.append(var);
    
        var.setValue(p2);
    
        list.append(var);
    
    
    
    
    
       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("myList", list);
        engine.load(url);
    
        return app.exec();
    }
    

    QML:

    import QtQuick 2.12
    import QtQuick.Controls 2.5
    import QtQuick.Window 2.12
    import Qt.labs.qmlmodels 1.0
    
    
    Window {
        width: 400
        height: 400
        visible: true
    
    
    
        TableView {
            id: tv
            anchors.fill: parent
            columnSpacing: 1
            rowSpacing: 1
            boundsBehavior: Flickable.StopAtBounds
    
            property var variantList: myList
    
            function startupFunction() {
    
                for(var i = 0; i < variantList.length; i++)
                {
                    console.log(variantList[i]);
                }
    
            }
    
            Component.onCompleted: startupFunction();
    
    
    
    
            model: TableModel {
                TableModelColumn { display: "Type" }
                TableModelColumn { display: "Speed" }
                TableModelColumn { display: "Ammunition" }
                TableModelColumn { display: "Activity" }
                TableModelColumn { display: "Coordinates" }
    
                // Each row is one type of fruit that can be ordered
                rows: [
                    {
                        // Each property is one cell/column.
                        Type: l[0],
                        Speed: l[1],
                        Ammuntion: l[2],
                        Activity: l[3],
                        Coordinates: l[4]
                    },
                ]
            }
            delegate:  TextInput {
                text: model.display
                padding: 12
                selectByMouse: true
    
                onAccepted: model.display = text
    
                Rectangle {
                    anchors.fill: parent
                    color: "#efefef"
                    z: -1
                }
            }
        }
    }
    

    Objects:

    #ifndef EXAMPLE_H
    #define EXAMPLE_H
    #include <QObject>
    #include <QVariant>
    #include <QDebug>
    
    struct P2D
    {
            Q_GADGET
            Q_PROPERTY(P2D p2d READ getP2D )
            Q_PROPERTY(float m_x READ getX WRITE setX)
            Q_PROPERTY(float m_y READ getY WRITE setY)
    
    
    
        public:
            float m_x;
            float m_y;
            P2D getP2D() {return *this;}
            float getX() {return this->m_x;}
            void setX(float x) {this->m_x = x;}
            float getY() {return this->m_y;}
            void setY(float y) {this->m_y = y;}
    
        }; Q_DECLARE_METATYPE(P2D)
    
    
    
    
    
    
    struct Player {
    
        Q_GADGET
    
    
        Q_PROPERTY(Player player READ getPlayer)
        Q_PROPERTY(int m_speed READ getSpeed WRITE setSpeed)
        Q_PROPERTY(CarType m_carType READ getCarType)
        Q_PROPERTY(P2D* p2d READ getP2D WRITE setP2D )
    
    public:
        enum class CarType {
            NO_CAR = 0,
            SLOW_CAR,
            FAST_CAR,
            HYPER_CAR
        }; Q_ENUM(CarType)
    
        CarType m_carType;
        int m_speed;
        P2D *p2d;
    
        Player getPlayer() { return *this;}
        int getSpeed() {return this->m_speed;}
        void setSpeed(int speed) {this->m_speed = speed;}
        CarType getCarType() { return m_carType;}
        void setCarType(CarType car) {this->m_carType = car;}
        P2D* getP2D() {return this->p2d;}
    
        void setP2D(P2D *ptr)
        {
    
            this->p2d = ptr;
        }
    
    }; Q_DECLARE_METATYPE(Player)
    #endif
    

    Thanks for your time.

    JKSHJ 1 Reply Last reply 17 Jul 2021, 15:17
    0
    • R raphasauer
      17 Jul 2021, 13:11

      Hello everyone. Is it possible to access a QVariant's QMetaObject in QML? I have a QVariantList that I load in my QML like this:

      engine.rootContext()->setContextProperty("myList", list);
      

      I can interate through each QVariant like this:

      function startupFunction() {
      
                  for(var i = 0; i < variantList.length; i++)
                  {
                      console.log(variantList[i]);
                  }
      
              }
      

      In C++, it's easy to access this meta object, it can be done like this:

      var.value<Player>().staticMetaObject;
      

      How could I do it on QML?

      Here's my code:

      Main:

      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include <example.h>
      #include <QQmlContext>
      
      int main(int argc, char *argv[])
      {
      #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
      #endif
      
          QGuiApplication app(argc, argv);
      
          qRegisterMetaType<Player>();
          qRegisterMetaType<P2D>();
          qRegisterMetaType<P2D*>();
      
          Player p1;
          p1.setSpeed(15);
          p1.setCarType(Player::CarType::NO_CAR);
      
          P2D* ptr = new P2D();
      
          ptr->setX(10);
      
          ptr->setY(25);
      
          p1.setP2D(ptr);
      
      
          Player p2;
          p2.setSpeed(150);
          p2.setCarType(Player::CarType::FAST_CAR);
      
          P2D* ptr2 = new P2D();
      
          ptr->setX(50);
      
          ptr->setY(60);
      
          p2.setP2D(ptr2);
      
      
          QVariantList list;
      
          QVariant var;
      
          var.setValue(p1);
      
          list.append(var);
      
          var.setValue(p2);
      
          list.append(var);
      
      
      
      
      
         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("myList", list);
          engine.load(url);
      
          return app.exec();
      }
      

      QML:

      import QtQuick 2.12
      import QtQuick.Controls 2.5
      import QtQuick.Window 2.12
      import Qt.labs.qmlmodels 1.0
      
      
      Window {
          width: 400
          height: 400
          visible: true
      
      
      
          TableView {
              id: tv
              anchors.fill: parent
              columnSpacing: 1
              rowSpacing: 1
              boundsBehavior: Flickable.StopAtBounds
      
              property var variantList: myList
      
              function startupFunction() {
      
                  for(var i = 0; i < variantList.length; i++)
                  {
                      console.log(variantList[i]);
                  }
      
              }
      
              Component.onCompleted: startupFunction();
      
      
      
      
              model: TableModel {
                  TableModelColumn { display: "Type" }
                  TableModelColumn { display: "Speed" }
                  TableModelColumn { display: "Ammunition" }
                  TableModelColumn { display: "Activity" }
                  TableModelColumn { display: "Coordinates" }
      
                  // Each row is one type of fruit that can be ordered
                  rows: [
                      {
                          // Each property is one cell/column.
                          Type: l[0],
                          Speed: l[1],
                          Ammuntion: l[2],
                          Activity: l[3],
                          Coordinates: l[4]
                      },
                  ]
              }
              delegate:  TextInput {
                  text: model.display
                  padding: 12
                  selectByMouse: true
      
                  onAccepted: model.display = text
      
                  Rectangle {
                      anchors.fill: parent
                      color: "#efefef"
                      z: -1
                  }
              }
          }
      }
      

      Objects:

      #ifndef EXAMPLE_H
      #define EXAMPLE_H
      #include <QObject>
      #include <QVariant>
      #include <QDebug>
      
      struct P2D
      {
              Q_GADGET
              Q_PROPERTY(P2D p2d READ getP2D )
              Q_PROPERTY(float m_x READ getX WRITE setX)
              Q_PROPERTY(float m_y READ getY WRITE setY)
      
      
      
          public:
              float m_x;
              float m_y;
              P2D getP2D() {return *this;}
              float getX() {return this->m_x;}
              void setX(float x) {this->m_x = x;}
              float getY() {return this->m_y;}
              void setY(float y) {this->m_y = y;}
      
          }; Q_DECLARE_METATYPE(P2D)
      
      
      
      
      
      
      struct Player {
      
          Q_GADGET
      
      
          Q_PROPERTY(Player player READ getPlayer)
          Q_PROPERTY(int m_speed READ getSpeed WRITE setSpeed)
          Q_PROPERTY(CarType m_carType READ getCarType)
          Q_PROPERTY(P2D* p2d READ getP2D WRITE setP2D )
      
      public:
          enum class CarType {
              NO_CAR = 0,
              SLOW_CAR,
              FAST_CAR,
              HYPER_CAR
          }; Q_ENUM(CarType)
      
          CarType m_carType;
          int m_speed;
          P2D *p2d;
      
          Player getPlayer() { return *this;}
          int getSpeed() {return this->m_speed;}
          void setSpeed(int speed) {this->m_speed = speed;}
          CarType getCarType() { return m_carType;}
          void setCarType(CarType car) {this->m_carType = car;}
          P2D* getP2D() {return this->p2d;}
      
          void setP2D(P2D *ptr)
          {
      
              this->p2d = ptr;
          }
      
      }; Q_DECLARE_METATYPE(Player)
      #endif
      

      Thanks for your time.

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on 17 Jul 2021, 15:17 last edited by
      #2

      @raphasauer said in Access QVariant's QMetaObject in QML:

      Is it possible to access a QVariant's QMetaObject in QML?

      No.

      But you can probably access its properties. What is your goal for wanting to access the QMetaObject in QML?

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      R 1 Reply Last reply 17 Jul 2021, 16:14
      0
      • JKSHJ JKSH
        17 Jul 2021, 15:17

        @raphasauer said in Access QVariant's QMetaObject in QML:

        Is it possible to access a QVariant's QMetaObject in QML?

        No.

        But you can probably access its properties. What is your goal for wanting to access the QMetaObject in QML?

        R Offline
        R Offline
        raphasauer
        wrote on 17 Jul 2021, 16:14 last edited by
        #3

        @JKSH Thanks for the reply. I was actually hoping to list the QMetaObject's properties on QML, in a dynamic way. Is it possible?

        1 Reply Last reply
        0

        1/3

        17 Jul 2021, 13:11

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved