ThefrenchSpeedruns
ThefrenchSpeedruns

Reputation: 113

Qt can't inherit properties

I'm having troubles with properties.

So I have some Classes (UML)

CNode
   |--CNode2D
         |--CSprite2D

CNode has these following Properties:

Q_OBJECT
Q_PROPERTY(QString name READ getName WRITE setName)
Q_PROPERTY(bool visible READ getVisible WRITE setVisible)
Q_PROPERTY(bool enabled READ getEnabled WRITE setEnabled)

CNode2D has those:

Q_OBJECT
Q_PROPERTY(float Position_X READ getPosX WRITE setPosX)
Q_PROPERTY(float Position_Y READ getPosY WRITE setPosY)
Q_PROPERTY(float height READ getHeight WRITE setHeight)

CSprite2D has none...

If I try to print out the Properties in the constructor of CSprite2D

for(int i = metaObject()->propertyOffset(); i < metaObject()->propertyCount(); ++i)
{
      qDebug() << metaObject()->property(i).name() << metaObject()->property(i).read(this);
}

I only get outputted the properties of CNode2D

Position_X QVariant(float, 7.47779e+21)
Position_Y QVariant(float, 4.58827e-41)
height QVariant(float, 0)

Which leads me to think that CNode2D doesn't inherit of the properties of CNode...

Only CNode inherits from QObject.

If I make inherit the other two Classes from QObject, I get thrown compilation errors at me.

I'm new to Qt, so I might have forgotten something important, in this case, sorry.

EDIT: HERE ARE THE CLASS DEFINITIONS (I removed the includes):

    class CNode: public QObject
{
public:
    CNode();
    QString getName();
    void setName(QString nameI);
    bool getVisible ();
    void setVisible (bool i);
    void setEnabled(bool i);
    bool getEnabled();

public slots:
    void nameChanged();
    void visibleChanged();
    void enabledChanged();

public: //properties

    Q_OBJECT
    Q_PROPERTY(QString name READ getName WRITE setName)
    Q_PROPERTY(bool visible READ getVisible WRITE setVisible)
    Q_PROPERTY(bool enabled READ getEnabled WRITE setEnabled)

protected:
    QTreeWidgetItem* nodeListItem;
    QString name;
    bool enabled;
    bool visible; //if the node has to get rendered
};



    class CNode2D: public CNode
{
public:
    CNode2D();
    virtual ~CNode2D();
    void setPosX(float i);
    float getPosX();
    float getPosY();
    void  setPosY(float i);
    float getHeight();
    void  setHeight(float i);

public slots:
    void posXChanged();
    void posYChanged();
    void heightChanged();

public:
    Q_OBJECT
    Q_PROPERTY(float Position_X READ getPosX WRITE setPosX)
    Q_PROPERTY(float Position_Y READ getPosY WRITE setPosY)
    Q_PROPERTY(float height READ getHeight WRITE setHeight)

protected:
    float height;
    float posX;
    float posY;
};


class CSprite2D: public CNode2D
{
public:
    CSprite2D(int number, Ui::MainWindow *i);
    ~CSprite2D();
    int getPosX();
    int getPosY();
    void compile();
private:
    Ui::MainWindow *ui;
    int number_sprite;
    QString spriteLink;
    void drawSprite();
};

Upvotes: 2

Views: 873

Answers (1)

transistor
transistor

Reputation: 669

Just tried some code, as I was curious:

You have to add another loop for accessing also the superclass' properties like so:

for(int i = metaObject()->superClass()->propertyOffset(); i < metaObject()->superClass()->propertyCount(); ++i)
{
      qDebug() << metaObject()->superClass()->property(i).name() << metaObject()->superClass()->property(i).read(this);
}

Just calling metaObject() accesses only the derived instance and not the parents.

Upvotes: 4

Related Questions