title: How do I organise pure QML projects for reusability?
body: <p>I have <a href="https://gitlab.com/detly/MyComponent/tree/44571f968b6174aaee64ad91308b106fddc5cafe" rel="nofollow noreferrer">a QML project</a> that works fine when it's all in the same directory. There's a <code>main.qml</code> with a <code>main.cpp</code> containing the usual:</p>
  
  <pre><code>QQmlApplicationEngine engine;
  engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
  </code></pre>
  
  <p>There is also my custom component, in pure QML, <code>MyComponent.qml</code>.</p>
  
  <p>When it is contained within a single project, it works perfectly. But now I want to think about reusability.</p>
  
  <p>I want to have separate <code>main()</code>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.</p>
  
  <p>I also want to use <code>MyComponent.qml</code> <em>without</em> any of these <code>main()</code>s in other projects (that's the whole point!).</p>
  
  <p>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.</p>
  
  <p>What do I need to do to achieve this?</p>
  
  <p>I've read <a href="https://stackoverflow.com/questions/42597690/how-to-package-custom-controls-in-qml">How to package custom controls in QML?
  </a> but the answer is light on detail. I can create the appropriate files for an <a href="http://doc.qt.io/qt-5/qtqml-modules-identifiedmodules.html" rel="nofollow noreferrer">Identified Module</a>, but how do my separate projects actually access and incorporate them?</p>
  
  <p>I tried to arrange things as sub-projects, but didn't get very far. If I create a <code>widget</code> sub-project as a library project, I have this <code>.pro</code> file:</p>
  
  <pre><code>TEMPLATE = lib
  
  TARGET = widget-lib
  
  QT += core quick qml
  CONFIG += c++11 plugin
  
  DISTFILES += \
      MyComponent.qml
  </code></pre>
  
  <p>The project then fails to build because there's no actual library or entry point or, in fact, any code at all:</p>
  
  <pre><code>LINK : error LNK2001: unresolved external symbol __DllMainCRTStartup@12
  </code></pre>
  
  <p>debug\widget-lib.dll : fatal error LNK1120: 1 unresolved externals</p>
  
  <p>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.</p>
  
  <p>Adding the <code>MyComponent.qml</code> file to a <code>demo</code> sub-project also fails. With this QRC file:</p>
  
  <pre><code>&lt;RCC&gt;
      &lt;qresource prefix="/"&gt;
          &lt;file&gt;main.qml&lt;/file&gt;
          &lt;file&gt;../widget/MyComponent.qml&lt;/file&gt;
      &lt;/qresource&gt;
  &lt;/RCC&gt;
  </code></pre>
  
  <p>...the built demo project can't find <code>MyComponent.qml</code> because <code>../widget/</code> doesn't exist in the build output path.</p>
  
  <p>Adding the <code>widget</code> sub-project as a library also fails because there's no actual shared library to link against, so <code>demo</code> can't be built.</p>
  
  <p>I've read <a href="http://doc.qt.io/qt-5/qtqml-modules-qmldir.html" rel="nofollow noreferrer">Module Definition qmldir Files</a>, but it's all about how to create the file, and has nothing on how to use it.</p>
  
  <p>How do I actually <em>reuse</em> 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?</p>