Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. reflection capabilities

reflection capabilities

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 4.3k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dit8
    wrote on last edited by
    #1

    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..

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        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.html

        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 = metaproperty.name();
            QVariant value = object->property(name);
            ...
        }
        

        Just as note :)

        D 1 Reply Last reply
        2
        • mrjjM mrjj

          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.html

          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 = metaproperty.name();
              QVariant value = object->property(name);
              ...
          }
          

          Just as note :)

          D Offline
          D Offline
          dit8
          wrote on last edited by
          #4

          great that should do the trick!
          (now I just need to get to know properties better to understand the implications of using it)

          mrjjM 1 Reply Last reply
          0
          • D dit8

            great that should do the trick!
            (now I just need to get to know properties better to understand the implications of using it)

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @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.

            kshegunovK 1 Reply Last reply
            1
            • mrjjM mrjj

              @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.

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              @mrjj said in reflection capabilities:

              Also the class get married to Qt

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

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved