Find out if a QObject is a registered QML type

I register many types as QmlComponents through

qmlRegisterType<Service>("my.services", 1, 0, "Service"); 

Now I would like to traverse the tree of objects, getting only registered qml types.

 void Service::traverse(QString &path, QObject *root) { if( <!root is registered qml type> ) { //<-- this piece im missing return; } if(!path.isEmpty()) { path.append('.'); }; path.append(root->metaObject()->className()); qDebug() << path; foreach(QObject *o, root->children()) { traverse(path, o); } } 

Can anyone help me with this?

+5
source share
1 answer

The closest thing I can think of (without changing the types themselves) is to use qmlEngine() :

 if (qmlEngine(root)) { return; } 

However, this will return true for any type created in QML.

To define only your C ++ types, you can specify their prefix type name (for example, QmlService ):

 if (QString(root->metaObject()->className()).contains("Qml")) { return; } 

Although, if you can do this, I’m not sure why you simply won’t keep track of which types you are registering in the list or something else, and refer to it later. If you clarify a little what you are trying to achieve, perhaps we can find the best solutions.

+1
source

All Articles