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