QML component not recognized when loading from local file



  • Hi all. When loading a qml file (main.qml) from a custom directory (NOT via resource file. The qml files are not to be integrated in the application) I can't load other components in the same directory.

    The QML folder is:

    /folder
      |
      - main.qml
      - ActivationArea.qml
    

    I can load the main.qml correctly but main.qml can't load the ActivationArea component.

    MAIN.QML

    import QtQuick 2.7
    import ".";
    
    Item {
        anchors.fill: parent;
    
        ActivationArea {
            id: topLeft;
            areaSide: currentExercise.parameter("area_width").value;
    
            x: 0;
            y: 0;
        }
    
        ActivationArea {
            id: center;
            areaSide: currentExercise.parameter("area_width").value;
    
    
            anchors {
                centerIn: parent;
            }
        }
    }
    
    

    ACTIVATIONAREA.QML

    import QtQuick 2.7
    
    Item {
        id: root;
    
        property int areaSide: 0;
        property string baseColour: "red";
        property string activeColour: "pink";
    
    
        Rectangle {
            id: bg;
            color: root.baseColour;
    
            width: root.areaSide;
            height: root.areaSide;
    
            Behavior on color {
                NumberAnimation {
                    duration: 100;
                }
            }
        }
    

    The error code given is: ActivationArea is not a type. Any ideas on how to load my items/components?

    P.S : the baseUrl is set to the directory of the root of the qml files.


  • Moderators



  • main.qml has import "." at line 2. The "." imports the current directory, so yeah, already tried that


  • Moderators

    @nwoki Can you check if the following code prints the same path where main.qml is present? Add this code in main.qml.

    Component.onCompleted: {
        console.log(Qt.resolvedUrl("."))
    }
    


  • i get the following output.

    qml: MY RESOLVE -> file:///
    

    So it's not reading the root dir of my qml file. How can I set it properly?


  • Moderators

    @nwoki I tried to create the same scenario and it worked. From where are you loading these QML files ? Can you post the whole project structure? Are there any plugins involved ?



  • @p3c0 The QML files are in a folder on the desktop whilst my program is run from a different folder. The result i'm trying to achieve is that of being able to load a qml program (any folder with a bunch of QML files in it starting from a "main.qml" file) from any folder.

    Yes, I use plugins but they're custom QML plugins (qqmlcomponent). Do they somehow interfere with the loading of the qml files on my desktop folder? (I use a seperate QQmlEngine for these components)



  • The result i'm trying to achieve is that of being able to load a qml program (any folder with a bunch of QML files in it starting from a "main.qml" file) from any folder.

    Use a Loader and set the source to a main area of that said extra folder

    example:

    /home/nwoki/mainApp/ This is the folder that contains your main app. that app that is run when main.cpp inits the engine or view or whatever. Add a Loader here and get the url (for other folder )and store in c++ sa a QString or whatever

    then there is this folder
    /home/nwoki/Videos/QmlVideosFolder/

    Just map the Loader to Load from there.


  • Moderators

    @nwoki Can you try without setting the baseUrl and in the following manner:

    QQuickView view;
    view.setSource(QUrl::fromLocalFile(QStringLiteral("/home/folder/main.qml")));
    


  • @p3c0 said in QML component not recognized when loading from local file:

    @nwoki Can you try without setting the baseUrl and in the following manner:

    QQuickView view;
    view.setSource(QUrl::fromLocalFile(QStringLiteral("/home/folder/main.qml")));
    

    This was the fix. More precisely:

    QUrl::fromLocalFile
    

    I was loading the non qrc files without the "QUrl::fromLocalFile". Using this solved the qml resolve problems and I'm now able to load the extra QML components from the specified dir.

    Thanks guys :)


  • Moderators

    @nwoki Didn't anticipate that :D
    Congratulations and Happy Coding :)


Log in to reply
 

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