[Interest] (Q)Plugins and QML/Quick

Ulf Hermann ulf.hermann at qt.io
Tue Sep 14 11:02:32 CEST 2021


> Maybe I missed something, but this seems more like a solution to manage 
> QML resources and modules that you know about at compile time.

The QML import path allows you to combine modules compiled separately in 
the context of different projects.

> I'm talking about a runtime problem. Lemme try to explain with some 
> simplified code. Allow me to leave a link to some markdown and syntax 
> highlighted stuff:
> 
> https://gist.github.com/ruilvo/43365e67f6d3ea472531738fd5a52793

Do not start with something as low level as a plugin. Think of your code 
as logical units, defining different parts of your UI. Then create a QML 
module for each. The plugins are automatically generated. You don't have 
to care about them. Using "import Some.Module" in QML will load them as 
necessary.

You only need to make sure the QML engine knows about the relevant 
import paths. If you use the default directory layout as shown in the 
blog post, the default import path will be enough.

Otherwise you have to call QQmlEngine::addImportPath(). This is likely 
what you have to do if you want to combine modules from different projects.

You say that "import mytype" is not possible. However, there must be 
some starting point in your application. You need to have code that 
specifies a first plugin to load by file name. This can instead be 
phrased as a module URI. If you insist on not having an entry point 
main.qml, you can dynamically generate a two-line QML document and load 
it via QQmlComponent::setData():

// given moduleName and elementName:
QQmlEngine engine;
engine.addImportPath( ... );
QQmlComponent component(&engine);
const QString qml = QStringLiteral("import %1\n%2{}").arg(moduleName, 
elementName)
component.setData(qml.toUtf8(), QUrl());
QObject *myQmlObject = component.create();

best regards,
Ulf


More information about the Interest mailing list