Important: Please read the Qt Code of Conduct -

Chapter6-plugins - how are QML imports resolved?

  • I don't understand how QML imports are resolved. Looking at app.qml in chapter6-imports (in the Qt examples/declarative/tutorials directory), the first few lines are

    @import QtQuick 1.0

    Item {
    width: 300; height: 200

    PieChart {
        anchors.centerIn: parent
        width: 100; height: 100
        slices: [
            PieSlice { 
                anchors.fill: parent@

    In QtCreator 2.1 both PieChart and PieSlice have wiggly red underlines, which seems to mean 'unknown type,' as you would expect given that there is only the default "import ...." at the head of the file. Nevertheless, the example runs OK.

    The project includes chartsplugin.cpp, which contains this line:

    @Q_EXPORT_PLUGIN2(chartsplugin, ChartsPlugin);@

    which would seem to indicate that to get access to PieChart and PieSlice the qml should include

    @import chartsplugin 1.0 // wrong. why?@

    Could someone throw some light on this?

    And is there a tool which I can run on a plugin to discover the plugin's namespace and exports?


  • I guess this kind of import are resolved at runtime. The qmlviewer look for a qmldir files witch list all plugin used. Then it look for all new type register in all plugin, so you don't need any import.

    In the last code you've posted, this is only right when you don't used the qmlviewer but a qmlview in a personal app. I don't know how that really work but this is really two different approaches.

    You may found more information "here":

  • The example works because the qmldir is in the same directory. It won't work if the qmldir is located elsewhere on the filesystem. (This is probably confusing and the example should be changed.)

    If you want to use this import you suggested:

    import chartsplugin 1.0

    Then you'll need to make a "chartsplugin" directory that contains the qmldir file and the 'lib' subdirectory (which contains the plugin library). Then, if you add the parent directory of "chartsplugin" to the import path - for example, by passing the -I option to QML Viewer - then you'll be able to import the plugin as above.

    The "modules documentation": has more information about modules and the import path.

Log in to reply