C Younger Brother
C Younger Brother

Reputation: 1

What dos the QVariant(QString, “”) of property mean in The Property System?

The QObject subclass has a function tha it return the object of QMetaObject. The function is metaObject(). I use this method to get it:

    MyObject *myObject_1=new MyObject;
   const QMetaObject *metaobject=myObject_1->metaObject();

When you use this code it will print something like this.

Object.h

#ifndef MYOBJECT_H
#define MYOBJECT_H

#include <QObject>

class MyObject:public QObject
{
   Q_OBJECT
public:
   MyObject(QObject *parent=0):QObject(parent){

   }
};

#endif // MYOBJECT_H

Main.cpp

#include "myobject.h"
#include <QApplication>
#include <QMetaObject>
#include <QMetaProperty>
#include <QDebug>

int main(int argc, char *argv[])
{
   MyObject *myObject_1=new MyObject;
   const QMetaObject *metaobject=myObject_1->metaObject();
   for(int i=0;i<metaobject->propertyCount();i++){
       QMetaProperty metaproperty=metaobject->property(i);
       qDebug()<<myObject_1->property(metaproperty.name());
   }

   return 0;
}

Print the result:

enter image description here

When you use this code it will print something like this.

Object.h

#ifndef MYOBJECT_H
#define MYOBJECT_H

#include <QObject>

class MyObject:public QObject
{
   Q_OBJECT
   Q_PROPERTY(Priority priority READ priority WRITE setPriority)
public:
   enum Priority{One,Two,Three};
   Q_ENUM(Priority)
   MyObject(QObject *parent=0):QObject(parent){

   }
   void setPriority(Priority priority){
       m_priority=priority;
   }

   Priority priority()const{
       return m_priority;
   }
private:
   Priority m_priority;
};

#endif // MYOBJECT_H

Main.cpp

#include "myobject.h"
#include <QApplication>
#include <QMetaObject>
#include <QMetaProperty>
#include <QDebug>

int main(int argc, char *argv[])
{
   MyObject *myObject_1=new MyObject;
   myObject_1->setProperty("priority","Two");
   const QMetaObject *metaobject=myObject_1->metaObject();
   for(int i=0;i<metaobject->propertyCount();i++){
       QMetaProperty metaproperty=metaobject->property(i);
       qDebug()<<myObject_1->property(metaproperty.name());
   }

   return 0;
}

Print the result

enter image description here

The question is why myObject_1 has a property that is QVariant(QString,“”)? What dos the QVariant(QString, “”) of property mean in The Property System ?

View the breakpoint:

enter image description here

Upvotes: 0

Views: 180

Answers (1)

rafix07
rafix07

Reputation: 20936

You have printed the property from base class - QObject. Use propertyOffset to show only properties for your derived class:

  //                          \/ 
  for(int i=metaobject->propertyOffset();i<metaobject->propertyCount();i++){
       QMetaProperty metaproperty=metaobject->property(i);
       qDebug()<<myObject_1->property(metaproperty.name());
   }

This property QVariant(QString,“”) is object name of base class.

Upvotes: 2

Related Questions