reflection capabilities

  • i've got a class that has several QStrings as data member:
    class myConst{
    const QString first = "FIRST";
    const QString second = "SECOND";
    QList<QString > myConstList;
    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..

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Right out of the box ? Currently, no, you can't. Here's a little interesting article.

  • Qt Champions 2017

    It should be mentioned if your
    QStrings data member were Qt properties you would be able
    to enumerate them.

    QObject *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 =;
        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)

  • Qt Champions 2017

    @dit8 said in reflection capabilities:


    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.

  • Qt Champions 2016

    @mrjj said in reflection capabilities:

    Also the class get married to Qt

    With members of type QString that marriage is iron-cast.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.