Qt5 QML module is not installed

前端 未结 5 2038
温柔的废话
温柔的废话 2020-12-24 13:50

I\'m confused about modules in Qt QML. I\'ve read all the docs, but it doesn\'t make clear some basic ideas.

I understand that i can put a bunch of QML files into a

5条回答
  •  情歌与酒
    2020-12-24 14:34

    In my case (I have all QML files in qrc resources) worked to add qmldir to resources also and call method addImportPath("qrc:/") of QQmlApplicationEngine.

    My main.cpp looks like:

    QQmlApplicationEngine engine;
    engine.addImportPath("qrc:/");
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    

    Important parts of my .pro file looks like:

    RESOURCES += qml.qrc \
        MyModule/mymodule.qrc
    
    QML_IMPORT_PATH += $$PWD
    

    My qmldir:

    module MyModule
    
    MyItem          2.0 MyItem20.qml
    MyItem          2.1 MyItem21.qml
    

    My qrc:

    
        
            MyItem20.qml
            MyItem21.qml
            qmldir
        
    
    

    And finally my main.qml:

    import QtQuick 2.5
    import QtQuick.Window 2.2
    
    import MyModule 2.0
    
    Window {
        visible: true
        width: 640
        height: 480
    
        MyItem {
            anchors.fill: parent
        }
    }
    

    QtCreator is happy (not underlining components and imports) and module is loaded. Hope this helps.

提交回复
热议问题