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

Viewed 1194

I am unable to load a qml file from C++:

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

At the top of this file we have

import My.Module 1.0

The error is (component.errorString()):

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

My.Module is a plugin containing qml only. It has been successfully loaded using QQmlEngine::importPlugin. The qml components are in a qrc and are compiled. I don't register any types in the plugin itself. This worked for Qt 5.14.1 but doesn't work for Qt 5.15.0

1 Answers

The reason is that I didn't register any types, in this case it turns out you need to call qmlRegisterModule:

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