3

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?

detly
  • 29,332
  • 18
  • 93
  • 152
  • If I understand well your question you just want to *package* of QML widgets. I don't know how to do that, but do you really need a specific lib ? I don't see the purpose of your organization. What is the other project ? The other `main.cpp` ? – ymoreau Mar 01 '18 at 09:29
  • 1
    Maybe this is what you are a looking for : http://doc.qt.io/qt-5/qtqml-modules-qmldir.html – ymoreau Mar 01 '18 at 09:51
  • @ymoreau At this point, I don't have another one, but I'd like to have unit tests and usability tests as well, which both require separate `main()`s. I also intend to use the control in real applications, of which there will be many. – detly Mar 01 '18 at 19:19
  • I read the section on `qmldir` files, I wrote one, but... now what? It doesn't do anything for the projects that depend upon the control. – detly Mar 01 '18 at 19:20
  • It *almost* looks like [*Other Project > Qt Quick UI Prototype*](http://doc.qt.io/qtcreator/quick-projects.html) is what I need here but `*drumroll*` it cannot be created as a subproject! – detly Mar 01 '18 at 22:17
  • I guess it does not make sense for QMake to create a subproject which is just a set of QML files. Can't you just create your QRC for this package and include it in the different projects you need ? Why create a subproject for this ? – ymoreau Mar 02 '18 at 14:12
  • @ymoreau Why do it for C++ code? – detly Mar 02 '18 at 14:14
  • @ymoreau I mean, it's possible, sure. But copy-pasting source files separately into the tree of every project that need them is prone to things getting out of sync and becoming hard to track. I had expected that QML could be packaged and shared like many other scripting languages, but if it's not designed for it, it is what it is. – detly Mar 02 '18 at 14:17
  • C++ code must be compiled so it can be convenient to only link to binaries of a lib. QML on the other hand, you just need one import path to use it directly. And QML has some ways to be packaged, just not as a subproject, it does not mean you need to copy-paste any file. We pointed out some options here in the comments, have you tried anything ? – ymoreau Mar 07 '18 at 09:38
  • @ymoreau Good point re C++. But I've read all the resources you've suggested, I just don't understand how they allow me to share a single QML component between multiple projects. I wrote a `qmldir` file... how does eg. the unit test project use this file? I can edit a project's QRC file to point to the component's directory, but the resulting binary still errors out because it can't find the QML file. How does it fit together? – detly Mar 07 '18 at 10:02
  • (I am definitely dreading the day when I actually *do* need a QML component backed by some C++ code, because that just seems completely off the map for Qt Creator or QMake. But that's getting O/T.) – detly Mar 07 '18 at 10:04
  • To spell it out, [here's an example](https://gitlab.com/detly/MyComponent) of a super simple QML project. How do I turn this into something that (a) has separate unit test, usability test and demo projects and (b) can be used by another application, in a totally separate project from this one? – detly Mar 14 '18 at 00:30
  • @ymoreau I've changed the question a bit to make it more about what I want to achieve. The subproject structure etc. was where the docs led me, but not necessarily the best approach. – detly Mar 14 '18 at 01:34
  • Sorry, I don't have any answer to give there. I thought that including a simple QRC could do the trick, but maybe this does not work with sibling-directories and sub-projects. I don't have any other idea right now. – ymoreau Mar 14 '18 at 09:37
  • @ymoreau No worries, I'll keep experimenting. Maybe I need to re-read the QRC docs and try that again. – detly Mar 14 '18 at 10:28
  • @detly I have a similar problem. Did you find any solution for this? – Qutab Qazi Aug 07 '20 at 10:16
  • @QutabQazi unfortunately not, the project I was using it for was cut short so I never had to find a solution. – detly Aug 07 '20 at 11:34

0 Answers0