[QML] List all variables like in a loop or something else
-
hi, is there a way in QML to list all variables I've created in an Item like in a loop or something else? I have a Source File, where i have only variables and i have to have there in a Database, but i don't want to write variable by variable.
example:
Item { property double number1: 1.2 property var newDate = new Date() property int number2 = 6 }
Thanks for help
-
@freaksdd
set the objectName property of your Items you want to save (can even be the same for all).
Then get the root object (via the engine). And then find all children and use their metaObject() to traverse the properties.QML:
Item { objectName: "item_to_save" .... }
C++:
QQuickView view; QObject* rootItem = view.rootObject(); if ( QObject* item= rootItem->findChildren<QObject*>("item_to_save") ) { const QMetaObject * mo = item->metaObject(); for( int i = mo->propertyOffset(); i < mo->propertyCount(); ++i ) { QMetaProperty prop = mo->property(i) ; QString name = QString::fromLatin1(prop.name()); QVariant value = prop.read(item); ... } }
Should do it, but haven't tested it.
-
A pure QML way would be as follows:
Item { id: item property double number1: 1.2 property var newDate : new Date() property int number2 : 6 Component.onCompleted: { for(var property in item) console.log(property) } }
But note that this lists all the properties including the properties inherited by that Item. So in case if you want to filter your properties you can add some unique prefix to your properties and check. Something like:
if(property.substring(0, 4)==="str_") ...
3/3