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

Pointer to a Q_GADGET



  • Hello everyone. I'm trying to develop a function that receives a Q_GADGET as input and lists its properties. I can't figure out, however, how to actually input a Q_GADGET in the function. I was hoping to work with pointers (something like a pointer to a Q_GADGET), but all my attempts so far have failed. The first snippet is from a Q_GAGDET. The second snippet contains the main.

    #ifndef EXAMPLE_H
    #define EXAMPLE_H
    #include <QObject>
    
    struct Player {
        Q_GADGET
    
    public:
        enum class CarType {
            NO_CAR = 0,
            SLOW_CAR,
            FAST_CAR,
            HYPER_CAR
        }; Q_ENUM(CarType)
    
        Q_PROPERTY(Player player READ getPlayer)
        Q_PROPERTY(float m_speed READ getSpeed)
        Q_PROPERTY(CarType m_carType READ getCarType)
    
        Player getPlayer() { return *this;}
        float getSpeed() {return m_speed;}
        CarType getCarType() { return m_carType;}
    
    #endif
    

    Main:

    #include <QCoreApplication>
    #include <iostream>
    #include <QMetaObject>
    #include "example.h"
    #include <QDebug>
    #include <QMetaProperty>
    
    //Should list properties of a Q_GAGDET
    //As example only, I know Q_GAGDET is
    // a macro in this wont run
    void gadgetExplorer(Q_GAGDET *ptr)
    {
        const QMetaObject = ptr->staticMetaObject;
        //List properties
    }
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        qRegisterMetaType<Player>();
        Player p1;
        p1.m_speed = 30;
        Player *pointerPlayer = &p1;
        //Doing something like this to list properties from Player
        //Using a pointer as an input
        gadgetExplorer(pointerPlayer);
        return a.exec();
    }
    

    I know this could be achieved using a MetaObject as an input, however I'm really curious to know if it's possible using pointers.

    Thanks for your time!



  • Q_GAGDET is not a class, use a template:

    template <class T>
    void gadgetExplorer(T *ptr)
    {
        const QMetaObject = ptr->staticMetaObject;
        //List properties
    }
    


  • @VRonin Solved! Thanks for your time!