As in JavaScript, you can use typeof
:
QtObject {
id: c
property real x
property string y
property int z
Component.onCompleted: print(typeof c.x, typeof c.y, typeof c.z)
}
This will print qml: number string number
.
Note that there is no difference here between x
and z
even though they are not of the same type, that's because JavaScript knows of only 1 number type, every number is 64-bit floating point*.
If you want to know how types are actually stored by the Qt engine, you would have to do as you mentionned in your question, use QMetaObject.
For that you can expose a c++ type as a qml singleton, and expose an invokable method in it returning the typename of an object's property :
#ifndef METAOBJECTHELPER_H
#define METAOBJECTHELPER_H
#include <QMetaObject>
#include <QObject>
#include <QQmlProperty>
class MetaObjectHelper : public QObject {
Q_OBJECT
public:
using QObject::QObject;
Q_INVOKABLE QString typeName(QObject* object, const QString& property) const
{
QQmlProperty qmlProperty(object, property);
QMetaProperty metaProperty = qmlProperty.property();
return metaProperty.typeName();
}
};
#endif // METAOBJECTHELPER_H
Doing this in QML :
print(MetaObjectHelper.typeName(c, "x"), MetaObjectHelper.typeName(c, "y"), MetaObjectHelper.typeName(c, "z"));
Will then print qml: double QString int
* : more nuanced details here