I have a QML project that works fine when it's all in the same directory. There's a main.qml
with a main.cpp
containing the usual:
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
There is also my custom component, in pure QML, MyComponent.qml
.
When it is contained within a single project, it works perfectly. But now I want to think about reusability.
I want to have separate main()
s for the demo (what's there now), unit testing and usability testing. I want to be able to run these independently of each other.
I also want to use MyComponent.qml
without any of these main()
s in other projects (that's the whole point!).
I am not too hung up on the structure here, I just want the demo code to be separate from the widget code and to have a reusable widget.
What do I need to do to achieve this?
I've read How to package custom controls in QML? but the answer is light on detail. I can create the appropriate files for an Identified Module, but how do my separate projects actually access and incorporate them?
I tried to arrange things as sub-projects, but didn't get very far. If I create a widget
sub-project as a library project, I have this .pro
file:
TEMPLATE = lib
TARGET = widget-lib
QT += core quick qml
CONFIG += c++11 plugin
DISTFILES += \
MyComponent.qml
The project then fails to build because there's no actual library or entry point or, in fact, any code at all:
LINK : error LNK2001: unresolved external symbol __DllMainCRTStartup@12
debug\widget-lib.dll : fatal error LNK1120: 1 unresolved externals
The only other project type on offer in Qt Creator is a QML/C++ plugin, but since I have no C++ code in my actual component, that doesn't seem applicable.
Adding the MyComponent.qml
file to a demo
sub-project also fails. With this QRC file:
<RCC>
<qresource prefix="/">
<file>main.qml</file>
<file>../widget/MyComponent.qml</file>
</qresource>
</RCC>
...the built demo project can't find MyComponent.qml
because ../widget/
doesn't exist in the build output path.
Adding the widget
sub-project as a library also fails because there's no actual shared library to link against, so demo
can't be built.
I've read Module Definition qmldir Files, but it's all about how to create the file, and has nothing on how to use it.
How do I actually reuse a "reusable QML component"? How do I use the component itself in other projects, or have multiple mini-projects for testing the component itself? And how do I achieve this without just copy-pasting the component QML file into every project that needs it?