4

I'm trying to wrap my head around QML plugins and I hope that someone can explain it to me because I seem to be missing something.

So I've gone ahead and created a Qt Quick 2 Extension Plugin. I have a simple source file, I've subclassed the QQmlExtensionPlugin class and registered the new type with qmlRegisterType. I've built this project and it gives me a DLL (I'm on Windows 7, and using QT 5.13). As I understand it, I should now be able to take this DLL and the qmldir file and drop into a new project and I should be able to load the QML from the DLL that is exposed via the qmldir file.

Unfortunately, when I do that, it doesn't work. All the examples I'm seeing online are showing a qml file that does an import of the plugin qml, but every time I try that, the import doesn't work and gives me a "QML module not found" error.

So my question is: How do I actually use the DLL and qmldir files in another project to expose the QML from the DLL to the new project?

1 Answer 1

6

It seems that you're missing the root QML import path.

The default root import path is %QTDIR%/qml, you can simply copy&paste your plugin modules into this folder so that the QML engine can find and load it.

Or a more common practice, using QQmlEngine::addImportPath()

QQmlEngine engine;
engine.addImportPath(qApp->applicationDirPath() + "/qml");

Note that you should always use Module (Namespace) Imports to import your C++ plugin module:

import My.CppModule 1.0

Using Directory Imports to import a C++ plugin module is a Undefined Behavior currently:

import "My/CppModule" // Might fail
Sign up to request clarification or add additional context in comments.

1 Comment

That's all it took, I copied my DLL and qmldir file to the QML import path location and it all worked. Thanks! Now I'm wondering if I can add a different QML import path location so that I don't have to copy DLLs to the QT application folder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.