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. Is the documentation correct for QMetaObject Class | Qt 4.8 ?

Is the documentation correct for QMetaObject Class | Qt 4.8 ?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 582 Views 1 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    On this page:
    https://doc.qt.io/archives/qt-4.8/qmetaobject.html#property

    The sample code shows:

    for(int i = metaObject->propertyOffset(); i < metaObject->propertyCount(); ++i)
    

    Is this correct? Because in my simple code that I've created:

    const QMetaObject* cpobjMO(metaObject());
    for( int i=cpobjMO->propertyOffset(); i<cpobjMO->propertyCount(); ++i )
    

    I have already added a property to the class in the constructor using:

    setProperty(crstrMember.toLatin1().data(), crvarData);
    

    Where crstrMember contains "Hello" and crvarData contains "World". When I get to the loop, both propertyOffset and propertyCount return 1, so it will not iterate at all.

    Kind Regards,
    Sy

    KroMignonK 1 Reply Last reply
    0
    • SPlattenS SPlatten

      @KroMignon , I'm making some kind of fundamental mistake because I've just made another change to test and validate:

      clsJSON::clsJSON(QObject* pParent) : QObject(pParent) {
      }
      void clsJSON::insert(const QString& crstrMember, const QVariant& crvarData) {
          if ( crstrMember.isEmpty() == true || crvarData.isValid() != true ) {
              return;
          }
          setProperty(crstrMember.toLatin1().data(), crvarData);
      }
      QString clsJSON::toString() {
          const QMetaObject* cpobjMO(metaObject());
          int intOffset = cpobjMO->propertyOffset()  // returns 1
             ,intCount = cpobjMO->propertyCount();    // returns 1
          const QString cstrTest("Another");
          const QVariant cvarData("test");
          insert(cstrTest, cvarData);
          QVariant varP1(property("HELLO"));       // Works and varP1 contains "WORLD"
          QVariant varP2(property("Another"));    // Also works varP2 contains "test"
          intOffset = cpobjMO->propertyOffset()  // still returns 1
          intCount = cpobjMO->propertyCount();    // still returns 1
          for( int i=cpobjMO->propertyOffset(); i<cpobjMO->propertyCount(); ++i ) {
              ...  
          }
          return ...;
      }
      

      See above results with comments.

      [Edit] See additional to lines where I get the property data inserted and verify it is correct, yet the propertyOffset and propertyCount are both still 1.

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #5

      @SPlatten said in Is the documentation correct for QMetaObject Class | Qt 4.8 ?:

      See above results with comments.

      Oh, sorry but I made the same mistake as you!
      This only works for property which have been declared with Q_PROPERTY, and therefore added in the QMetaObject create by the MOC!!

      What you are looking for is QObject::dynamicPropertyNames() (cf https://doc.qt.io/archives/qt-4.8/qobject.html#dynamicPropertyNames)

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      SPlattenS 1 Reply Last reply
      4
      • SPlattenS SPlatten

        On this page:
        https://doc.qt.io/archives/qt-4.8/qmetaobject.html#property

        The sample code shows:

        for(int i = metaObject->propertyOffset(); i < metaObject->propertyCount(); ++i)
        

        Is this correct? Because in my simple code that I've created:

        const QMetaObject* cpobjMO(metaObject());
        for( int i=cpobjMO->propertyOffset(); i<cpobjMO->propertyCount(); ++i )
        

        I have already added a property to the class in the constructor using:

        setProperty(crstrMember.toLatin1().data(), crvarData);
        

        Where crstrMember contains "Hello" and crvarData contains "World". When I get to the loop, both propertyOffset and propertyCount return 1, so it will not iterate at all.

        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by
        #2

        @SPlatten said in Is the documentation correct for QMetaObject Class | Qt 4.8 ?:

        Where crstrMember contains "Hello" and crvarData contains "World". When I get to the loop, both propertyOffset and propertyCount return 1, so it will not iterate at all.

        Are you sure you are testing on the right object instance?
        Note: propertyOffset = 1, which is normal, property 0 is the name() property of the QObject (cf. https://doc.qt.io/archives/qt-4.8/qmetaobject.html#propertyOffset).

        setProperty("Hello", "world");
        const QMetaObject* cpobjMO(metaObject());
        for( int i=cpobjMO->propertyOffset(); i<cpobjMO->propertyCount(); ++i )
            qDebug() << "Found" << i << "as" << QString::fromLatin1(cpobjMO->property(i).name());
        

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        SPlattenS 2 Replies Last reply
        0
        • KroMignonK KroMignon

          @SPlatten said in Is the documentation correct for QMetaObject Class | Qt 4.8 ?:

          Where crstrMember contains "Hello" and crvarData contains "World". When I get to the loop, both propertyOffset and propertyCount return 1, so it will not iterate at all.

          Are you sure you are testing on the right object instance?
          Note: propertyOffset = 1, which is normal, property 0 is the name() property of the QObject (cf. https://doc.qt.io/archives/qt-4.8/qmetaobject.html#propertyOffset).

          setProperty("Hello", "world");
          const QMetaObject* cpobjMO(metaObject());
          for( int i=cpobjMO->propertyOffset(); i<cpobjMO->propertyCount(); ++i )
              qDebug() << "Found" << i << "as" << QString::fromLatin1(cpobjMO->property(i).name());
          
          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #3

          @KroMignon , yes, my test code is very simple:

          clsJSON objDemo;
          objDemo.insert("HELLO", "WORLD");
          QString strJSON(objDemo.toString());
          qDebug() << strJSON;
          

          The class itself:

          clsJSON::clsJSON(QObject* pParent) : QObject(pParent) {
          }
          void clsJSON::insert(const QString& crstrMember, const QVariant& crvarData) {
              if ( crstrMember.isEmpty() == true || crvarData.isValid() != true ) {
                  return;
              }
              setProperty(crstrMember.toLatin1().data(), crvarData);
          }
          QString clsJSON::toString() const {
              const QMetaObject* cpobjMO(metaObject());
              for( int i=cpobjMO->propertyOffset(); i<cpobjMO->propertyCount(); ++i ) {
                  ...  
              }
              return ...;
          }
          

          toString is not compete yet, but the loop isn't getting iterated at all, I've checked and as posted propertyOffset and propertyCount both equal 1.

          Kind Regards,
          Sy

          1 Reply Last reply
          0
          • KroMignonK KroMignon

            @SPlatten said in Is the documentation correct for QMetaObject Class | Qt 4.8 ?:

            Where crstrMember contains "Hello" and crvarData contains "World". When I get to the loop, both propertyOffset and propertyCount return 1, so it will not iterate at all.

            Are you sure you are testing on the right object instance?
            Note: propertyOffset = 1, which is normal, property 0 is the name() property of the QObject (cf. https://doc.qt.io/archives/qt-4.8/qmetaobject.html#propertyOffset).

            setProperty("Hello", "world");
            const QMetaObject* cpobjMO(metaObject());
            for( int i=cpobjMO->propertyOffset(); i<cpobjMO->propertyCount(); ++i )
                qDebug() << "Found" << i << "as" << QString::fromLatin1(cpobjMO->property(i).name());
            
            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by SPlatten
            #4

            @KroMignon , I'm making some kind of fundamental mistake because I've just made another change to test and validate:

            clsJSON::clsJSON(QObject* pParent) : QObject(pParent) {
            }
            void clsJSON::insert(const QString& crstrMember, const QVariant& crvarData) {
                if ( crstrMember.isEmpty() == true || crvarData.isValid() != true ) {
                    return;
                }
                setProperty(crstrMember.toLatin1().data(), crvarData);
            }
            QString clsJSON::toString() {
                const QMetaObject* cpobjMO(metaObject());
                int intOffset = cpobjMO->propertyOffset()  // returns 1
                   ,intCount = cpobjMO->propertyCount();    // returns 1
                const QString cstrTest("Another");
                const QVariant cvarData("test");
                insert(cstrTest, cvarData);
                QVariant varP1(property("HELLO"));       // Works and varP1 contains "WORLD"
                QVariant varP2(property("Another"));    // Also works varP2 contains "test"
                intOffset = cpobjMO->propertyOffset()  // still returns 1
                intCount = cpobjMO->propertyCount();    // still returns 1
                for( int i=cpobjMO->propertyOffset(); i<cpobjMO->propertyCount(); ++i ) {
                    ...  
                }
                return ...;
            }
            

            See above results with comments.

            [Edit] See additional to lines where I get the property data inserted and verify it is correct, yet the propertyOffset and propertyCount are both still 1.

            Kind Regards,
            Sy

            KroMignonK 1 Reply Last reply
            0
            • SPlattenS SPlatten

              @KroMignon , I'm making some kind of fundamental mistake because I've just made another change to test and validate:

              clsJSON::clsJSON(QObject* pParent) : QObject(pParent) {
              }
              void clsJSON::insert(const QString& crstrMember, const QVariant& crvarData) {
                  if ( crstrMember.isEmpty() == true || crvarData.isValid() != true ) {
                      return;
                  }
                  setProperty(crstrMember.toLatin1().data(), crvarData);
              }
              QString clsJSON::toString() {
                  const QMetaObject* cpobjMO(metaObject());
                  int intOffset = cpobjMO->propertyOffset()  // returns 1
                     ,intCount = cpobjMO->propertyCount();    // returns 1
                  const QString cstrTest("Another");
                  const QVariant cvarData("test");
                  insert(cstrTest, cvarData);
                  QVariant varP1(property("HELLO"));       // Works and varP1 contains "WORLD"
                  QVariant varP2(property("Another"));    // Also works varP2 contains "test"
                  intOffset = cpobjMO->propertyOffset()  // still returns 1
                  intCount = cpobjMO->propertyCount();    // still returns 1
                  for( int i=cpobjMO->propertyOffset(); i<cpobjMO->propertyCount(); ++i ) {
                      ...  
                  }
                  return ...;
              }
              

              See above results with comments.

              [Edit] See additional to lines where I get the property data inserted and verify it is correct, yet the propertyOffset and propertyCount are both still 1.

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #5

              @SPlatten said in Is the documentation correct for QMetaObject Class | Qt 4.8 ?:

              See above results with comments.

              Oh, sorry but I made the same mistake as you!
              This only works for property which have been declared with Q_PROPERTY, and therefore added in the QMetaObject create by the MOC!!

              What you are looking for is QObject::dynamicPropertyNames() (cf https://doc.qt.io/archives/qt-4.8/qobject.html#dynamicPropertyNames)

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              SPlattenS 1 Reply Last reply
              4
              • KroMignonK KroMignon

                @SPlatten said in Is the documentation correct for QMetaObject Class | Qt 4.8 ?:

                See above results with comments.

                Oh, sorry but I made the same mistake as you!
                This only works for property which have been declared with Q_PROPERTY, and therefore added in the QMetaObject create by the MOC!!

                What you are looking for is QObject::dynamicPropertyNames() (cf https://doc.qt.io/archives/qt-4.8/qobject.html#dynamicPropertyNames)

                SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on last edited by
                #6

                @KroMignon , Thank you, I was starting to think it had to be something very fundamental like that...I think some notes in the documentation would be useful, since the functions are called property and setProperty.

                Kind Regards,
                Sy

                1 Reply Last reply
                0

                • Login

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