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

问题描述 投票:0回答:1

我无法从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

c++ qt qml qt5
1个回答
0
投票

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

void MyModulePlugin::registerTypes(const char *uri)
{
    Q_ASSERT(uri == QLatin1String("My.Module"));
    qmlRegisterModule(uri, 1, 0);
}
© www.soinside.com 2019 - 2024. All rights reserved.