[SOLVED] Make shared library with QML files

  • Is this possible?

    I have managed to make library with C++ classes and to import/use them in application, however I cannot make QML files visible to application.
    Can someone give me example or tutorial how to do that?

    All i have found is this so far:
    But I always stuck somewhere.
    Latest I tried is to create separated Qt Quick 2 Extension Plugin, but again I cannot provide QML files to application.

    I don't want to reimplement all my elements in C++, I would like to use them in QML if possible.

    This is how my file structure looks like:

    Application Project (EXE):

    Shared Library (DLL):
    -Button.qml (included in QRC file)

    In main.cpp when I execute this code (main method): Q_INIT_RESOURCE(Files);
    I get this error: undefined reference to `qInitResources_Files()'

    In application project I have added:
    @LIBS += -L/path_to_dll/ -lmy_dll_file
    INCLUDEPATH += /path_to_headers/@

    If I exclude Q_INIT_RESOURCE(Files); from main.cpp, then it compiles without errors but at runtime I am not able to see QML file.
    For example:
    @QFile* tmp = new QFile(":/component/Button.qml");
    if (tmp->exists())
    qDebug("PROBLEM"); //all the time@

  • Thanks, I somehow missed that.
    Now I managed to import QML files from Qt Quick 2 Extension Plugin to my application.
    Still, I cannot make it under library (DLL) file. In other words, I don't want to expose .qml files to users.

  • Use a QRC file for the "internal" QML-document-defined-types. In the module specification qmldir file, don't list those types which you don't want exposed to users.

  • Thanks.
    I have been trying that, however I cannot load files from QRC file (which is in DLL file).

    Any tips how should I load?

  • I have tried to load resource in library code, and Button.qml is not visible (file.exists() returns false).
    I really don't know what is the problem.

    In application if I create QRC file and add QML file it is visible without issues.
    If I apply same steps in shared library, QML file is not visible (neither to shared library or application, like resource doesn't exist). I have checked, in .pro file I have added RESOURCES += Files.qrc in library.

  • I have found solution (I am not sure is it right one, but at least it sees now files).

    In library I have added this file in .pro file:
    @SOURCES += path/to/qrc_Files.cpp@
    and included in library.h file this (it works also if you include directly in application):
    @#include "path/to/qrc_Files.cpp"@
    For me, it was located in build directory (don't know why is not added automatically in QtCreator).

    -Now I can run Q_INIT_RESOURCE(Files)- (it turns out that I don't need this) and now QML files are visible to both shared library and application :)

  • I wanted to add that using this solution may occur problem if you try to add more than 1 library to project.
    Problem is something about multiple declaration of functions (this is because some function names are exactly same in both libraries).
    I solved this using namespace in project that I am including libraries.

    This is example:
    @#include "myFirstLib.h"
    namespace whateverYouWant
    #include "mySecondLib.h"

    //now I can access to files located in both libraries@

  • Or if you want to have something like a shared folder of QML components between multiple binaries:
    - where qml_component folder is used by both apps.

    Do something like:
    create a resource file of the qml_component dir - say qml_components.qrc
    add that ressource file to each of the 2 apps : App1 and App2 - in their .pro file :
    @RESOURCES += <path_to_qrc>/qml_components.qrc@

    When importing in qml you do something like:
    @import "qrc://qml_components/myButton.qml"@

    This should work and the qml_components can now be shared between as many projects you want - included in the resources of each application.

  • You seem to have figured out how this works. Did you manage to get the components from the module displayed in the Qt Quick Designer? Somehow Qt Creator find my modules but it does not show them in the Designer.

    Here is my original post about this topic:

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.