Can not import components



  • Greetings All,

    I am new in QML. I am creating an application gui. Given is my directory structure

    (I am using XML to depict my directory structure. Elements with .* are files and without extension are directories)
    @
    <root>
    </application.pro>
    </resources.qrc>
    <qml>
    <gui>
    <main>
    </main.qml>
    </anotherscreen.qml>
    </main>
    <components>
    </Button.qml>
    </qmldir>
    </components>
    </gui>
    </qml>
    </root>
    @
    My resources.qrc looks like this

    @
    <!DOCTYPE RCC><RCC version="1.0">
    <qresource prefix="/guiresources">
    <file>qml/gui/components/qmldir</file>
    <!-- Screens -->
    <file alias="main.qml">qml/gui/main/main.qml</file>
    <file alias="anotherscreen.qml">qml/gui/main/anotherscreen.qml</file>
    </qresources>
    @
    In my main.qml I am importing my components as
    import "components" as Components <--- This gives me error (In the QT Quick editor) that can not find this file / directory

    and If I import my components in main.qml as
    import "../components" as Components <---- I can see no error

    But when I run my application it says

    qrc:/guiresources/main.qml:2:1: "../components": no such directory

    I access all my resources as qrc:/guiresources/<resource_name>

    Any idea whats going wrong ?

    Thanks in advance.
    Farrukh Arshad.


  • Moderators

    QML has a useful/ nasty (depends on context...) feature, that once you start using QRC, all children are loaded with QRC too. So, if you load your main.qml with QRC, all (really all) other paths in your files HAVE TO be QRC-based. Sad but true.

    There are a few ways for getting round this. You can clear the context before loading (if you are using Loader, Repeater elements). This requires going into C++, extracting QDeclarativeContext and clearing the baseUrl. An example from my code:
    @
    QDeclarativeContext *context = declarativeView->engine()->contextForObject(object); // Object here is my Loader element
    context->setBaseUrl(QUrl::fromLocalFile(""));
    @

    You can also use standard, relative paths while developing, but convert them into QRC when deploying. It's doable, but requires some work. I've created a Qt app that does that, took me about 3 days, but works like a charm. I might release the code in the future, but currently it's closed.

    Other option is - use resources more selectively, remembering to include all "children" of a file loaded by QRC inside this resource.

    Might have forgotten about some more things, I'll come back when I recall them. All in all, this is - IMO - one of the drawbacks of QML. I am thinking about proposing a change to this behaviour, but am not sure if it has chances of being picked up. I'm also not aware how much work would that take on Qt side, but there is a chance that this is an easy fix. We'll see. Currently, I'm too busy to get into it.



  • bq. QML has a useful/ nasty (depends on context…) feature, that once you start using QRC, all children are loaded with QRC too. So, if you load your main.qml with QRC, all (really all) other paths in your files HAVE TO be QRC-based. Sad but true.

    I don't have such a behavior. I have a bit complicated source tree with a decent use of relative paths and have no problems with loading it into C++ via resources. I modify nothing and use it as is.
    one of deep imports in my project: @import ".."
    import "../../.."@

    farrukh.arshad, try to remove prefix from your QRC file and use full path when loading it from C++("qrc:/qml/gui/main/main.qml")



  • hi this is balamurali

    I'm just a beginner in Qt creator .
    now working on speedometer recreation.
    my qml code is

    import QtQuick 2.3
    import QtQuick.Window 2.2
    import QtMobility.publishsubscribe 1.1
    import "../Components"

    Item {
    id: root
    property int viewid: -1
    property int analogindex: -1
    property int topindex: -1
    property int bottomindex: -1

    signal clicked()
    signal options()
    
    function toFixed(num,count) {
        var s = num.toFixed(count).toString();
        return s;
    }
    
    MouseHandler {
        id: mouseHandler
        anchors.fill: parent
        onSingleTap: root.clicked()
        onLongTap: root.reset()
        onDoubleTap: root.options()
    }
    
    function update() {
        analogindex = settings.getProperty("speed_analog",0)
        topindex =    settings.getProperty("speed_top",3)
        bottomindex = settings.getProperty("speed_bottom",4)
    }
    
    Component.onCompleted: update()
    
    property list<QtObject> values: [
        ValueSpaceSubscriber { id: speedcur; path: "/server/speed/current"     },
        ValueSpaceSubscriber { id: speedmin; path: "/server/speed/min"         },
        ValueSpaceSubscriber { id: speedmax; path: "/server/speed/max"         },
        ValueSpaceSubscriber { id: speedavg; path: "/server/speed/average"     },
        ValueSpaceSubscriber { id: distance; path: "/server/location/distance" },
        ValueSpaceSubscriber { id: monitor;  path: "/server/monitor/distance"  }
    ]
    property double analogvalue: values[analogindex].value? values[analogindex].value : -180
    property double topvalue:    values[topindex].value?    values[topindex].value    : 0
    property double bottomvalue: values[bottomindex].value? values[bottomindex].value : 0
    
    
    function reset() {
        console.log("speedometer.reset()")
        var cmd = { "class": "speed", "method": "reset", "args": [] }
        client.sendCommand(cmd);
    
        cmd = { "class": "location", "method": "reset", "args": [] }
        client.sendCommand(cmd);
    }
    property int count: 5
    onCountChanged: setCount(count)
    function setCount(c) {
        console.log("speedometer.setCount(",c,")")
        var cmd = { "class": "speed", "method": "setcount", "args": [ c ] }
        client.sendCommand(cmd);
    }
    property real hysteresis: 25.0
    onHysteresisChanged: setHysteresis(hysteresis)
    function setHysteresis(h) {
        console.log("speedometer.setHysteresis(",h,")")
        var cmd = { "class": "location", "method": "sethysteresis", "args": [ h ] }
        client.sendCommand(cmd);
    }
    
    Image {
        //source: if (analog.value < 10) "speed10.svg"; else "speed200.svg"
        source: (analogvalue && (analogvalue>10))? "../Images/speed200.svg" : "../Images/speed10.svg"
        width: parent.width
        height: parent.height
    }
    
    Rectangle {
        y: parent.height * 0.75
        height: parent.height * 0.16
        color: activePalette.dark
        width: parent.width/4
        anchors.horizontalCenter: parent.horizontalCenter
        Text {
            //id: top
            anchors.top: parent.top
            anchors.right: parent.right
            anchors.margins: 2
            //text: root.toFixed(top.value,1)
            text: root.toFixed(topvalue,1)
            color: "white"
            font.bold: true; font.pixelSize: parent.height/3
            style: Text.Raised; styleColor: "black"
        }
        Text {
            //id: bottom
            anchors.bottom: parent.bottom
            anchors.right: parent.right
            anchors.margins: 2
            //text: root.toFixed(bottom.value,1)
            text: root.toFixed(bottomvalue,1)
            color: "white"
            font.bold: true; font.pixelSize: parent.height/3
            style: Text.Raised; styleColor: "black"
        }
    }
    
    Image {
        source: "../Images/speedneedle.svg"
        width: parent.width
        height: parent.height
        transform: Rotation {
            id: needle
            origin.x: width/2
            origin.y: height/2
            //angle: if (analog.value < 10) analog.value/10*360 -180; else analog.value/200*360 -180
            angle: analogvalue<10? analogvalue/10*360 -180: analogvalue/200*360 -180
            Behavior on angle {
                SpringAnimation {
                    spring: 1.4
                    damping: .15
                }
            }
        }
    }
    

    }

    and I'm getting error as

    QQmlApplicationEngine failed to load component
    qrc:/main.qml:4 "../Components": no such directory

    kindly help me

    Thanks in Advance



  • @Balamurali said:

    hi this is balamurali

    I'm just a beginner in Qt creator .
    now working on speedometer recreation.
    my qml code is

    import QtQuick 2.3
    import QtQuick.Window 2.2
    import QtMobility.publishsubscribe 1.1
    import "../Components"

    Item {
    id: root
    property int viewid: -1
    property int analogindex: -1
    property int topindex: -1
    property int bottomindex: -1

    signal clicked()
    signal options()
    
    function toFixed(num,count) {
        var s = num.toFixed(count).toString();
        return s;
    }
    
    MouseHandler {
        id: mouseHandler
        anchors.fill: parent
        onSingleTap: root.clicked()
        onLongTap: root.reset()
        onDoubleTap: root.options()
    }
    
    function update() {
        analogindex = settings.getProperty("speed_analog",0)
        topindex =    settings.getProperty("speed_top",3)
        bottomindex = settings.getProperty("speed_bottom",4)
    }
    
    Component.onCompleted: update()
    
    property list<QtObject> values: [
        ValueSpaceSubscriber { id: speedcur; path: "/server/speed/current"     },
        ValueSpaceSubscriber { id: speedmin; path: "/server/speed/min"         },
        ValueSpaceSubscriber { id: speedmax; path: "/server/speed/max"         },
        ValueSpaceSubscriber { id: speedavg; path: "/server/speed/average"     },
        ValueSpaceSubscriber { id: distance; path: "/server/location/distance" },
        ValueSpaceSubscriber { id: monitor;  path: "/server/monitor/distance"  }
    ]
    property double analogvalue: values[analogindex].value? values[analogindex].value : -180
    property double topvalue:    values[topindex].value?    values[topindex].value    : 0
    property double bottomvalue: values[bottomindex].value? values[bottomindex].value : 0
    
    
    function reset() {
        console.log("speedometer.reset()")
        var cmd = { "class": "speed", "method": "reset", "args": [] }
        client.sendCommand(cmd);
    
        cmd = { "class": "location", "method": "reset", "args": [] }
        client.sendCommand(cmd);
    }
    property int count: 5
    onCountChanged: setCount(count)
    function setCount(c) {
        console.log("speedometer.setCount(",c,")")
        var cmd = { "class": "speed", "method": "setcount", "args": [ c ] }
        client.sendCommand(cmd);
    }
    property real hysteresis: 25.0
    onHysteresisChanged: setHysteresis(hysteresis)
    function setHysteresis(h) {
        console.log("speedometer.setHysteresis(",h,")")
        var cmd = { "class": "location", "method": "sethysteresis", "args": [ h ] }
        client.sendCommand(cmd);
    }
    
    Image {
        //source: if (analog.value < 10) "speed10.svg"; else "speed200.svg"
        source: (analogvalue && (analogvalue>10))? "../Images/speed200.svg" : "../Images/speed10.svg"
        width: parent.width
        height: parent.height
    }
    
    Rectangle {
        y: parent.height * 0.75
        height: parent.height * 0.16
        color: activePalette.dark
        width: parent.width/4
        anchors.horizontalCenter: parent.horizontalCenter
        Text {
            //id: top
            anchors.top: parent.top
            anchors.right: parent.right
            anchors.margins: 2
            //text: root.toFixed(top.value,1)
            text: root.toFixed(topvalue,1)
            color: "white"
            font.bold: true; font.pixelSize: parent.height/3
            style: Text.Raised; styleColor: "black"
        }
        Text {
            //id: bottom
            anchors.bottom: parent.bottom
            anchors.right: parent.right
            anchors.margins: 2
            //text: root.toFixed(bottom.value,1)
            text: root.toFixed(bottomvalue,1)
            color: "white"
            font.bold: true; font.pixelSize: parent.height/3
            style: Text.Raised; styleColor: "black"
        }
    }
    
    Image {
        source: "../Images/speedneedle.svg"
        width: parent.width
        height: parent.height
        transform: Rotation {
            id: needle
            origin.x: width/2
            origin.y: height/2
            //angle: if (analog.value < 10) analog.value/10*360 -180; else analog.value/200*360 -180
            angle: analogvalue<10? analogvalue/10*360 -180: analogvalue/200*360 -180
            Behavior on angle {
                SpringAnimation {
                    spring: 1.4
                    damping: .15
                }
            }
        }
    }
    

    }

    and I'm getting error as

    QQmlApplicationEngine failed to load component
    qrc:/main.qml:4 "../Components": no such directory

    kindly help me

    Thanks in Advance


Log in to reply
 

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