Create a plugin library of reusable QML files



  • Hello, I'm having a hard time finding any specific information about sharing QML files across multiple projects. Currently we have a Common QML project that just pulls qml files in as resources.

    This works fine when you share the resource, but it requires a path to the resource and doesn't work with the syntax highlighting in qtcreator when modifying qml files.

    Is there a better way to make a shared qml library? I think I found some stuff about making a plugin, but the particular document I was reading was overriding the qttypes. It would be nice to make our own plugin or library though.

    Thanks.



  • Hi,

    http://qt-project.org/doc/qt-5.0/qtqml/qtqml-modules-topic.html should be what you're looking for. In the module specification file (qmldir) you can define which QML files are exposed as types.

    Cheers,
    Chris.



  • ok i've kind of understand whats going on, i found the plugins project, but I have an issue with knowing all of the paths to the files.

    I think this is more of an issue with me not knowing how to set up a project, we want the qml dir in a subdirectory in some common library, and the project that imports it in another directory.



  • To elaborate on my troubles the example .pro file

    @TEMPLATE = lib
    CONFIG += plugin
    QT += qml

    DESTDIR = imports/TimeExample
    TARGET = qmlqtimeexampleplugin

    SOURCES += plugin.cpp

    pluginfiles.files +=
    imports/TimeExample/qmldir
    imports/TimeExample/center.png
    imports/TimeExample/clock.png
    imports/TimeExample/Clock.qml
    imports/TimeExample/hour.png
    imports/TimeExample/minute.png

    qml.files = plugins.qml
    qml.path += $$[QT_INSTALL_EXAMPLES]/qml/plugins
    target.path += $$[QT_INSTALL_EXAMPLES]/qml/plugins/imports/TimeExample
    pluginfiles.path += $$[QT_INSTALL_EXAMPLES]/qml/plugins/imports/TimeExample

    INSTALLS += target qml pluginfiles@

    I'd like to be able to add the import with out having to point to all of the files, or make a common.pri in the folder that will do this. The QML_IMPORT_PATH i'm not entirely sure of what to do with either.

    Is there any good examples that have an entirely separete plugin project. Also if we have projects with c++ and qml can we make plugins that have c++ back ends?



  • I'm not entirely sure what you mean, so maybe my advice won't be useful.

    But, basically: your "QML module" will be installed to a directory (the QML import path). Your application, which imports that module, can reside wherever it likes, it just needs the correct import statement.

    For example, if in your application's QML file you have:
    @
    import com.example.components 1.0
    @

    then the QML engine will look in $QTDIR/qml/com/example/components for the module specification file ("qmldir"). You shouldn't need to include any of the paths to anything, in the .pro/.pri files of the application itself.

    To answer your final question, yes it's absolutely possible to define plugins with C++ defined types - there is a section about that in the documentation. Your plugin cpp should use qmlRegisterType (and related functions) to register the C++-defined types as QML types.

    Cheers,
    Chris.


Log in to reply
 

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