无法加载 QML 插件:无法保护模块,因为它从未注册过

Unable to load QML plugin: cannot protect module as it was never registered

本文关键字:注册 因为 模块 QML 加载 插件 保护      更新时间:2023-10-16

我无法从C++加载 qml 文件:

QQmlComponent component(&engine, QUrl("qrc:/qml/Singletons.qml"));

在此文件的顶部,我们有

import My.Module 1.0

错误是 (component.errorString()(:

plugin cannot be loaded for module "My.Module": Cannot protect module My.Module 1 as it was never registered

My.Module是一个仅包含 qml 的插件。它已使用QQmlEngine::importPlugin成功加载。qml 组件位于 qrc 中并被编译。我不在插件本身中注册任何类型。这适用于Qt 5.14.1,但不适用于Qt 5.15.0

原因是我没有注册任何类型,在这种情况下,事实证明您需要调用qmlRegisterModule

void MyModulePlugin::registerTypes(const char *uri)
{
Q_ASSERT(uri == QLatin1String("My.Module"));
qmlRegisterModule(uri, 1, 0);
}