Solved reflection capabilities
-
i've got a class that has several QStrings as data member:
eg.
class myConst{
const QString first = "FIRST";
const QString second = "SECOND";
QList<QString > myConstList;
public:
myConst(){
myConstList << first << second ; ////wish I wouldn't have to do that
}
}I wish that the "myConstList" will have all the data members of the class without being have to add them in the constructor.
Is there a way in QT to do something of that sort? (some sort of reflection of the class?)
I've tried going through the site for that and didn't find something like that.. -
Hi and welcome to devnet,
Right out of the box ? Currently, no, you can't. Here's a little interesting article.
-
Hi
It should be mentioned if your
QStrings data member were Qt properties you would be able
to enumerate them.
http://doc.qt.io/qt-5/properties.htmlQObject *object = ... const QMetaObject *metaobject = object->metaObject(); int count = metaobject->propertyCount(); for (int i=0; i<count; ++i) { QMetaProperty metaproperty = metaobject->property(i); const char *name = metaproperty.name(); QVariant value = object->property(name); ... }
Just as note :)
-
great that should do the trick!
(now I just need to get to know properties better to understand the implications of using it) -
@dit8 said in reflection capabilities:
implications
One that is important is that properties works using a tool called moc.exe so its not pure c++ anymore.
Also the class get married to Qt as it depends on the metasystem.
But for that you get easy way to save members ( more dynamically) . So the extend of the implications is based on
your actual use case. -
@mrjj said in reflection capabilities:
Also the class get married to Qt
With members of type
QString
that marriage is iron-cast.