Is the documentation correct for QMetaObject Class | Qt 4.8 ?
-
On this page:
https://doc.qt.io/archives/qt-4.8/qmetaobject.html#propertyThe 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.
-
@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.
@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 withQ_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) -
On this page:
https://doc.qt.io/archives/qt-4.8/qmetaobject.html#propertyThe 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.
@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()); -
@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());@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.
-
@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());@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.
-
@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.
@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 withQ_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) -
@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 withQ_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)@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.