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. Allocate memory for a Q_GADGET inside another Q_GADGET
QtWS25 Last Chance

Allocate memory for a Q_GADGET inside another Q_GADGET

Scheduled Pinned Locked Moved Unsolved General and Desktop
qmetaobjectpointerqgagdet
2 Posts 1 Posters 277 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 16 Jul 2021, 13:09 last edited by
    #1

    Hello everyone. Let's say I have a void *pointer to a Q_GADGET, and inside this Q_GADGET I have a pointer to another Q_GADGET. All of them are registered in the meta-object system. Is it possible, using QMetaObjects and the original void* pointer to allocate memory to this other Q_GADGET? Example code follows:

    #ifndef EXAMPLE_H
    #define EXAMPLE_H
    #include <QObject>
    
    //Nested Q_GADGET
    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)
    
        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;}
    
    
    public:
        float m_x;
        float m_y;
    }; Q_DECLARE_METATYPE(P2D)
    
    //Main Q_GADGET
    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(int m_speed READ getSpeed WRITE setSpeed)
        Q_PROPERTY(CarType m_carType READ getCarType)
        Q_PROPERTY(P2D* p2d READ getP2D )
    
    
        Player getPlayer() { return *this;}
        int getSpeed() {return this->m_speed;}
        void setSpeed(int speed) {this->m_speed = speed;}
        CarType getCarType() { return m_carType;}
        P2D* getP2D() {return this->p2d;}
    
    
    public:
        CarType m_carType;
        int m_speed;
        P2D *p2d;
    
    }; Q_DECLARE_METATYPE(Player)
    
    #endif
    

    Main:

    #include <QCoreApplication>
    #include <iostream>
    #include <QMetaObject>
    #include "example.h"
    #include <QDebug>
    #include <QMetaProperty>
    #include <QPointer>
    
    
    void gadgetExplorer(void *ptr, const char* s)
    {
        //Receives Player and access its meta-object
        int typeId = QMetaType::type(s);
        const QMetaObject *mo = QMetaType::metaObjectForType(typeId);
    
        qInfo() << "Q_GADGET: " << mo->className();
        //QProperty 3 refers to Q_GADGET P2D
        QMetaProperty mp = mo->property(3);
        qInfo() << "Q_PROPERTY: " << mp.name();
        
        //With this given info, is it possible to allocate memory to p2d?
        
    }
    
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        qRegisterMetaType<Player>();
        qRegisterMetaType<P2D>();
    
        Player p1;
        p1.setSpeed(30);
    
        gadgetExplorer(&p1, "Player");
    
    
        return a.exec();
    }
    

    I think this is doable creating a method to allocate memory inside the Player Q_GADGET. However, is there other way to do it?

    Thanks for your time.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      raphasauer
      wrote on 16 Jul 2021, 13:26 last edited by
      #2

      One more example:

      #include <QCoreApplication>
      #include <iostream>
      #include <QMetaObject>
      #include "example.h"
      #include <QDebug>
      #include <QMetaProperty>
      #include <QPointer>
      
      
      void gadgetExplorer(void *ptr, const char* s)
      {
          //Receives Player and access its meta-object
          int typeId = QMetaType::type(s);
          const QMetaObject *mo = QMetaType::metaObjectForType(typeId);    
          qInfo() << "Q_GADGET: " << mo->className();
          QMetaProperty mp;
          for(int i = 1; i < mo->propertyCount(); i++)
          {
              mp = mo->property(i);
              qInfo() << "Q_PROPERTY: " << mp.typeName() << "Value: " << mp.readOnGadget(ptr);
              if(int(mp.type()) == 1024)
                  gadgetExplorer(ptr, mp.typeName());
          }
      
      }
      
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
          qRegisterMetaType<Player>();
          qRegisterMetaType<P2D>();
          qRegisterMetaType<P2D*>();
      
          Player p1;
          p1.setSpeed(30);
          p1.p2d = new P2D();
          p1.p2d->setX(10.0);
          p1.p2d->setY(25.0);
          gadgetExplorer(&p1, "Player");
      
      
          return a.exec();
      }
      

      gadgetExplorer() works fine for the first Q_GADGET, but for the second one the read property doesn't work. Is it possible to access the second's Q_GADGET pointer?

      1 Reply Last reply
      0

      1/2

      16 Jul 2021, 13:09

      • Login

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