I'm trying to understand Qt's reflection capabilities by testing its QMetaObject class. As a minimum example, I'm trying to see if I can find the name of a class using using QMetaObject. However, upon running the programme, I get a compile time error "undefined reference to 'vtable for ReflectedClass'". I can remove the compile time error by removing the Q_OBJECT macro however then when printing out the class name I get "QObject" which is the parent class and NOT the class Ive defined. I was told I must add in Q_OBJECT to get the class of the child class. Can anyone help me address the error and help me to simply get a class name using QMetaObject. Here is the code:
#include <QCoreApplication>
#include <iostream>
#include <QString>
#include <QObject>
#include <QMetaObject>
#include <QMetaProperty>
#include <QDebug>
using namespace std;
class ReflectedClass : public QObject {
Q_OBJECT
public:
int getFieldA(){
return fieldA;
}
bool getFieldB(){
return fieldB;
}
QString getFieldC(){
return fieldC;
}
void setFieldA(int i){
fieldA = i;
}
void setFieldB(bool b){
fieldB = b;
}
void setFieldC(QString s){
fieldC = s;
}
private:
int fieldA;
bool fieldB;
QString fieldC;
};
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
ReflectedClass *a = new ReflectedClass();
a->setFieldA(3);
a->setFieldB(true);
a->setFieldC("Hello");
const QMetaObject *mo = a->metaObject();
qDebug() << mo->className();
return app.exec();
}
Aucun commentaire:
Enregistrer un commentaire