Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Access QVariant's QMetaObject in QML



  • 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.


  • Moderators

    @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?



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