访问从继承自 QObject 的对象继承的 class 的 Q_Properties
Accessing Q_Properties of a class inherited from an Object that inherited from QObject
我有以下简化设置,我试图访问 inherited class 上的 Q_Properties class继承自 QObject。我可以很好地访问基础 class 的属性,但我无法找到或看到(在调试时)我继承的 class:
的属性
基础Class:
class Vehicle : public QObject
{
Q_OBJECT
Q_PROPERTY(QString model READ getModel WRITE setModel)
public:
explicit Vehicle(QObject *parent = 0);
QString getModel() const;
void setModel(QString model);
virtual QString toString() const;
private:
QString _model;
};
继承Class:
class TransportVehicle : public Vehicle
{
Q_PROPERTY(int Capacity READ getCapacity WRITE setCapacity)
public:
TransportVehicle();
TransportVehicle(int, QString, int);
int getCapacity() const;
void setCapacity(int);
QString toString() const;
private:
int _maxCapacity;
};
以及以下来自通用方法的代码片段,用于访问它在传递给它的列表中找到的任何对象的属性:
int write(QObjectList* list) {
int count = 0;
for(int i = 0; i < list->size(); i++)
{
const QMetaObject *mo = list->at(i)->metaObject();
for(int k = mo->propertyOffset(); k < mo->propertyCount(); k++)
{
const QMetaProperty prop = mo->property(k);
QString name = prop.name();
QString valStr = prop.read(list->at(i)).toString();
QDebug << name << ": " << valStr << endl;
count++;
}
delete mo;
}
return count;
}
它工作正常,除了我的输出会像 'model: toyota' 并且不包括容量。
我能够获得子classes 属性的唯一方法是添加虚拟获取和设置方法以及一个额外的Q_property 到我的基础class ,这似乎根本不对,在我无法访问基础 class.
的正常情况下是不可能的
您没有使用宏 Q_OBJECT
:
class TransportVehicle : public Vehicle
{
Q_OBJECT
^^^^^^^^
Q_PROPERTY(int Capacity READ getCapacity WRITE setCapacity)
public:
TransportVehicle();
TransportVehicle(int, QString, int);
int getCapacity() const;
void setCapacity(int);
QString toString() const;
private:
int _maxCapacity;
};
Vehicle
继承了QObject
,所以TransportVehicle
就得用Q_OBJECT
宏,Q_GADGET
是不继承[=11的时候=] 并想要一个元对象。
直接或间接继承QObject
的每个class都需要Q_OBJECT
宏。您在 TransportVehicle
中没有它,所以您没有为它生成元数据,您被困在为基础 class.
生成的元对象中
我有以下简化设置,我试图访问 inherited class 上的 Q_Properties class继承自 QObject。我可以很好地访问基础 class 的属性,但我无法找到或看到(在调试时)我继承的 class:
的属性基础Class:
class Vehicle : public QObject
{
Q_OBJECT
Q_PROPERTY(QString model READ getModel WRITE setModel)
public:
explicit Vehicle(QObject *parent = 0);
QString getModel() const;
void setModel(QString model);
virtual QString toString() const;
private:
QString _model;
};
继承Class:
class TransportVehicle : public Vehicle
{
Q_PROPERTY(int Capacity READ getCapacity WRITE setCapacity)
public:
TransportVehicle();
TransportVehicle(int, QString, int);
int getCapacity() const;
void setCapacity(int);
QString toString() const;
private:
int _maxCapacity;
};
以及以下来自通用方法的代码片段,用于访问它在传递给它的列表中找到的任何对象的属性:
int write(QObjectList* list) {
int count = 0;
for(int i = 0; i < list->size(); i++)
{
const QMetaObject *mo = list->at(i)->metaObject();
for(int k = mo->propertyOffset(); k < mo->propertyCount(); k++)
{
const QMetaProperty prop = mo->property(k);
QString name = prop.name();
QString valStr = prop.read(list->at(i)).toString();
QDebug << name << ": " << valStr << endl;
count++;
}
delete mo;
}
return count;
}
它工作正常,除了我的输出会像 'model: toyota' 并且不包括容量。
我能够获得子classes 属性的唯一方法是添加虚拟获取和设置方法以及一个额外的Q_property 到我的基础class ,这似乎根本不对,在我无法访问基础 class.
的正常情况下是不可能的您没有使用宏 Q_OBJECT
:
class TransportVehicle : public Vehicle
{
Q_OBJECT
^^^^^^^^
Q_PROPERTY(int Capacity READ getCapacity WRITE setCapacity)
public:
TransportVehicle();
TransportVehicle(int, QString, int);
int getCapacity() const;
void setCapacity(int);
QString toString() const;
private:
int _maxCapacity;
};
Vehicle
继承了QObject
,所以TransportVehicle
就得用Q_OBJECT
宏,Q_GADGET
是不继承[=11的时候=] 并想要一个元对象。
直接或间接继承QObject
的每个class都需要Q_OBJECT
宏。您在 TransportVehicle
中没有它,所以您没有为它生成元数据,您被困在为基础 class.