Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] Make shared library with QML files
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Make shared library with QML files

Scheduled Pinned Locked Moved QML and Qt Quick
10 Posts 5 Posters 15.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DRAX
    wrote on last edited by
    #1

    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:
    "http://qt-project.org/forums/viewthread/19941":http://qt-project.org/forums/viewthread/19941
    "http://qt-project.org/forums/viewthread/25988":http://qt-project.org/forums/viewthread/25988
    "http://qt-project.org/forums/viewthread/24264":http://qt-project.org/forums/viewthread/24264
    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):
    -main.cpp

    Shared Library (DLL):
    -library.h
    -Files.qrc
    -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("OK");
    else
    qDebug("PROBLEM"); //all the time@

    1 Reply Last reply
    0
    • podsvirovP Offline
      podsvirovP Offline
      podsvirov
      wrote on last edited by
      #2

      Have you read "it":http://qt-project.org/doc/qt-5.0/qtqml/qtqml-modules-topic.html?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DRAX
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          chrisadams
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DRAX
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            • D Offline
              D Offline
              DRAX
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                DRAX
                wrote on last edited by
                #7

                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 :)

                1 Reply Last reply
                1
                • D Offline
                  D Offline
                  DRAX
                  wrote on last edited by
                  #8

                  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@

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    asergiu
                    wrote on last edited by
                    #9

                    Or if you want to have something like a shared folder of QML components between multiple binaries:
                    App1
                    App2
                    qml_components
                    - 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.

                    1 Reply Last reply
                    0
                    • strahlexS Offline
                      strahlexS Offline
                      strahlex
                      wrote on last edited by
                      #10

                      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:
                      http://qt-project.org/forums/viewthread/41611/

                      Feel free to check out my website machinekoder.com
                      and my pet projects Intellicute and QtQuickVcp

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved