How to get the qml object's id in the C++ class which defined for QML?
-
I write a qml type in C++(
Demo
for example):class Demo : public QObject { Q_OBJECT QML_ELEMENT Q_PROPERY(QString name ...) //... };
and then, I use
Demo
inqml
as the following:Demo { id: demo1 name: "dddd" age: 3 //..... }
The question is:
Is there a way to get theid
(a.k.demo1
) ofDemo
in thedemo.cpp
(i.e. inDemo::Demo()
)? -
Is the goal a QObject *, or the string used in the id?
The QObject * is
this
within the constructor.The string version is available via QQmlContext::nameForObject()