Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML component not recognized when loading from local file

QML component not recognized when loading from local file

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmlqqmlengineqtquick2
11 Posts 3 Posters 9.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nwoki
    wrote on 23 Sept 2016, 16:25 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      p3c0
      Moderators
      wrote on 23 Sept 2016, 16:32 last edited by
      #2

      @nwoki Can you check if one of the following way works for you?
      https://forum.qt.io/topic/57604/qrc-main-qml-9-gui-delegates-uepeopleitemdelegate-qml-no-such-directory-error/2

      157

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nwoki
        wrote on 23 Sept 2016, 16:44 last edited by
        #3

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

        1 Reply Last reply
        0
        • P Offline
          P Offline
          p3c0
          Moderators
          wrote on 23 Sept 2016, 17:02 last edited by
          #4

          @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("."))
          }
          

          157

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nwoki
            wrote on 23 Sept 2016, 18:34 last edited by
            #5

            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?

            1 Reply Last reply
            0
            • P Offline
              P Offline
              p3c0
              Moderators
              wrote on 24 Sept 2016, 04:49 last edited by
              #6

              @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 ?

              157

              1 Reply Last reply
              0
              • N Offline
                N Offline
                nwoki
                wrote on 25 Sept 2016, 22:02 last edited by
                #7

                @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)

                J P 2 Replies Last reply 26 Sept 2016, 01:26
                0
                • N nwoki
                  25 Sept 2016, 22:02

                  @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)

                  J Offline
                  J Offline
                  JosephMills
                  wrote on 26 Sept 2016, 01:26 last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • N nwoki
                    25 Sept 2016, 22:02

                    @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)

                    P Offline
                    P Offline
                    p3c0
                    Moderators
                    wrote on 26 Sept 2016, 05:18 last edited by
                    #9

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

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

                    157

                    1 Reply Last reply
                    1
                    • N Offline
                      N Offline
                      nwoki
                      wrote on 26 Sept 2016, 08:42 last edited by
                      #10

                      @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 :)

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        p3c0
                        Moderators
                        wrote on 26 Sept 2016, 10:05 last edited by
                        #11

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

                        157

                        1 Reply Last reply
                        1
                        • B Burak Hancerli referenced this topic on 8 May 2023, 09:05

                        4/11

                        23 Sept 2016, 17:02

                        7 unread
                        • Login

                        • Login or register to search.
                        4 out of 11
                        • First post
                          4/11
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved