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. Pointer to a Q_GADGET

Pointer to a Q_GADGET

Scheduled Pinned Locked Moved Solved General and Desktop
meta-objectspointerfunctionqobjectqgagdet
3 Posts 2 Posters 685 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 last edited by
    #1

    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!

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Q_GAGDET is not a class, use a template:

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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      R 1 Reply Last reply
      6
      • VRoninV VRonin

        Q_GAGDET is not a class, use a template:

        template <class T>
        void gadgetExplorer(T *ptr)
        {
            const QMetaObject = ptr->staticMetaObject;
            //List properties
        }
        
        R Offline
        R Offline
        raphasauer
        wrote on last edited by
        #3

        @VRonin Solved! Thanks for your time!

        1 Reply Last reply
        0

        • Login

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