Reputation: 478
I have got a QQuickView which has loaded a qml file like the following.
Rectangle { width: 100; height: 100 }
Then I am retrieving the root object via QObject *root = view->rootObject()
.
Now I want to get the class name from this object.
The following code results into "QQuickRectangle"
root->metaObject()->className()
But what I want is "Rectangle" just like the typename in the qml file. Any idea?
Edit: I want to build a treeview with the object hirarchie of a qml file like QtCreator.
Upvotes: 2
Views: 3382
Reputation: 1
Yes, there is an answer for a particular case.
You can get QML type name, like Item, Rectangle or MyItem
I needed to get a QML name, be it a Rectangle or the name of a new component MyButton : MouseArea {...} in QML itself via JavaScript
it's enough
let name = object.toString()
let typename = name.substring(0, name.length-15)
You might find it convenient to handle this directly in QML, but there are several options in C++: from evaluate JS, to working with MOC or any classes that represent, for example, a name in the form of QQuickRectangle(0x1a45f97bdd0) where you need to remove 15 digits from the end
Unfortunately, for my case there is no need to duplicate this functionality in C++, that's where it came in handy in QML
Item{
id: lets_enum_children_types
Rectangle{
x: 0
y: 0
//...
}
component MyButton: MouseArea{
//...
}
MyButton{
//...
}
TextEdit{
//...
}
Button{
}
////////////////
function dump(object) {
console.log("dump")
for (const i in object.children){
let name = object.children[i].toString()
let typename = name.substring(0, name.length-15)
console.log( typename )
}
}
Component.onCompleted: dump(lets_enum_children_types)
////////////////
qml: dump
qml: QQuickRectangle
qml: MyButton
qml: QQuickTextEdit
qml: Button_QMLTYPE_26
}
Regarding the fact that the names of custom components exactly match, but the library components do not
Upvotes: 0
Reputation: 49319
There is a pattern there, for qml types implemented in C++ the name would be QQuickSomething
, for qml types implemented in qml the name would be Something_QMLTYPE_X_MAYBEMORESTUFF(objAddress)
.
So you can do some basic string editing depending on the result you get to isolate the actual type name:
QString name = QString(root->metaObject()->className());
if (name.contains("QQuick")) name.remove("QQuick");
else if (name.contains("QMLTYPE")) name.remove(QRegExp("_QMLTYPE_[0-9]*.*"));
// else it might be just a QObject or your on custom type you should handle
Edit: I want to build a treeview with the object hirarchie of a qml file like QtCreator.
Unless you are willing to dig into and use private APIs, it would likely be easier and also more useful to have your own custom model to drive both a view and the actual object tree. Also, QML is quite easy to parse, I'd personally buckle down and write a parses faster than it would take me to get into the existing one, especially if all that is needed is an object tree outline, but YMMV.
Upvotes: 2
Reputation: 12874
QtQuick doesn't provide some special metadata for QML items. It looks that QtQuick uses item types internally only while parsing the source.
The known workaround is objectName
:
Rectangle {
objectName: "Rectangle"
}
and so:
QString className = item->objectName();
Upvotes: 0
Reputation: 881
There is "better" information kept on this (QQmlType
& QQmlMetaType
), but it is not accessible through any public API that I can think of.
Can you explain what you would like to do with it? Maybe there's an alternative.
Upvotes: 2