弄清楚QObject是否是注册的QML类型

M.N*_*Bug 6 qt qml

我正在通过注册许多类型作为QmlComponents

qmlRegisterType<Service>("my.services", 1, 0, "Service");
Run Code Online (Sandbox Code Playgroud)

现在我想在获取qml注册类型的同时遍历对象树.

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);
    }
}  
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我吗?

Mit*_*tch 2

我能想到的最接近的事情(不改变类型本身)是使用qmlEngine()

if (qmlEngine(root)) {
    return;
}
Run Code Online (Sandbox Code Playgroud)

但是,对于 QML 中创建的任何类型,这都将返回 true。

要仅检测您的 C++ 类型,您可以为其类型名称添加前缀(例如QmlService):

if (QString(root->metaObject()->className()).contains("Qml")) {
    return;
}
Run Code Online (Sandbox Code Playgroud)

虽然如果你能做到这一点,我不知道为什么你不只是跟踪你在列表或其他东西中注册的类型并在以后引用它。如果您详细说明您想要实现的目标,也许我们可以提出更好的解决方案。