Problem with custom component used in external QML



  • Hello Everybody,

    I want to create a system using QML, where you can load QML from a file from anywhere on the machine.
    It works fine, until I reference a custom QML component from said file.

    Example

    CustomComp.qml

    import QtQuick 2.0
    
    Rectangle {
        property int customValue: 0
    }
    

    externalQMLFile.qml

    import QtQuick 2.0
    
    CustomComp {
        customValue: 4711
    }
    

    Using this component (located in the qml.qrc) inside e.g. the main.qml works fine as does loading an external QML.
    If I do it like in the example (custom components used in external file) I get this error:

    QQmlComponent: Component is not ready
    

    Is there a way to get this accomplished? It's kinda important to this whole project.

    Best wishes,
    Andreas


  • Moderators

    @Honkmueller said in Problem with custom component used in external QML:

    externalQMLFile.qml

    import QtQuick 2.0
    
    CustomComp {
        customValue: 4711
    }
    

    this can't be your full QML code using your custom component...
    How do you include your custom component type?!
    And also how do you register it (if you do at all)?



  • Try putting the property outside of the rectangle

    Example CustomComp.qml

    import QtQuick 2.0
    
    property string rectColor
    
    Rectangle {
        color: rectColor
    }
    
    

    example otherFile.qml

    import QtQuick2.0
    
    CustomComp {
        anchors.fill: parent
        rectColor: "blue"
    }
    


  • @raven-worx isn't CustomComp already registered? Or is this just the case within QMLs inside the project/qml.qrc file? Is there a way to include the CutomComp.qml into the externalQMLFile.qml?



  • How shall the qml engine know where your CustomComp qml file is located?

    So take a look for QML import path to add additional directories where the qml engine looks for qml files.
    ( http://doc.qt.io/qt-5/qtqml-syntax-imports.html )

    regards
    Karl-Heinz Reichel



  • @karlheinzreichel Like I said: The CustomComp.qml is in the qml.qrc file/directory and works fine in any other QML file in the project. So the engine seems to know, where to find it. It just doesn't work, if I use the CustomComp in a qml that I load like this:

    QQmlComponent component(Main::AppMain::engine, QUrl::fromLocalFile(filePath));
    

    Is that a "same folder" issue?

    regards Andreas



  • Could you show me the contents of filePath ?

    btw: one more question : did you build your app as shadow build?

    regards
    Karl-Heinz



  • @karlheinzreichel I tried around with import statements and it turned out, that I just had to add this on top of the externalQMLFile.qml:

    import "qrc:/"
    

    That's it.

    Thank you guys, you got me on the right track.

    Best regards,
    Andreas


Log in to reply
 

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