Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

figure out if QObject is a registered QML Type

Tags:

qt

qml

i am registering lots of Types as QmlComponents via

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

Now I would like to traverse the object tree while getting qml-registered types ONLY.

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 that ?

like image 317
M.N. Bug Avatar asked Aug 16 '16 10:08

M.N. Bug


1 Answers

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

if (qmlEngine(root)) {
    return;
}

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

To detect just your C++ types, you could give their type name a prefix (e.g. QmlService):

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

Though if you can do this, I'm not sure why you wouldn't just keep track of which types you register in a list or something and refer to it later. If you elaborate a bit about what you're trying to achieve, perhaps we can come up with better solutions.

like image 155
Mitch Avatar answered Nov 15 '22 04:11

Mitch