4

When I use Qt.createComponent to create component dynamically, the stutas is always Component.error but I cannot understand the reason.

I used it like that:

Rectangle{
    function loadTest(){
        function finishCreation() {
            if (component.status === Component.Ready) {
                console.log("ready")
            } else if (component.status === Component.Error) {
                // Error Handling
                console.log("Error loading component:", component.errorString());
            }
        }

        var component = Qt.createComponent("MyPage.qml");
        console.log(component.status)
        console.log("Error loading component:", component.errorString());
        component.statusChanged.connect(finishCreation);

        if (component.status === Component.Ready) {
            var button = component.createObject(container);
            console.log("ready")
        }
    }

    Component.onCompleted: {
        console.log("Completed Running!")
        loadTest()
    }
}

If the MyPage.qml does not exist in the qrc file, the error is

qrc:/MyPage.qml:-1 File not found"

If I set the full path of MyPage.qml, I get a Network error.

When I add the SeriesSelectionPage.qml file to the resource file, it works. But it shouldn't be dynamic?

I just want to find a QML file and load it dynamically when the application executes so that the application can load different QML according to user operations.

Anyone knows how to do that? I'm going crazy.

BaCaRoZzo
  • 7,502
  • 6
  • 51
  • 82
behtgod
  • 251
  • 3
  • 15

1 Answers1

5

Qt.createComponent() takes a url as its first argument. From the url documentation:

The url type refers to a resource locator (like a file name, for example). It can be either absolute, e.g. "http://qt-project.org", or relative, e.g. "pics/logo.png". A relative URL is resolved relative to the URL of the containing component.

So, as long as you're using relative URLs from within a file that is loaded from a QRC file, you'll need to use the qrc scheme:

var component = Qt.createComponent("qrc:/MyPage.qml");
BaCaRoZzo
  • 7,502
  • 6
  • 51
  • 82
Mitch
  • 23,716
  • 9
  • 83
  • 122
  • It will be better just add the QML file into resources and use "qrc:/" protocol in URL. – Max Go Sep 15 '15 at 18:34
  • 2
    I was under the impression that OP wanted the end user to be able to choose a QML file... guess I misread. – Mitch Sep 15 '15 at 18:36
  • Yes, you are right. looks like your first version of answer is more correct :) Sorry – Max Go Sep 15 '15 at 18:40
  • OP wrote "When I add the "SeriesSelectionPage.qml" to qrc file. It works." – Max Go Sep 15 '15 at 18:41
  • 1
    No, I think you're right and I just read the question incorrectly! Haha. :) – Mitch Sep 15 '15 at 18:41
  • @N1ghtLight Jeez...I think it's the first interpretation, i.e. loading a file on the basis of user interactions. As a matter of fact, we can be all wrong. :D – BaCaRoZzo Sep 15 '15 at 19:14
  • 2
    Thank you all. I'm so sorry that my English is too weak to express clearly. – behtgod Sep 16 '15 at 03:06
  • What url could I use if I want to create Qt provided component (in my case a MessageDialog)? I clearly dont want to hardcode /opt/Qt5/blabla into my code. – feedc0de Jul 04 '17 at 12:28
  • @DanielBrunner you have to wrap the Qt component in your own QML file. – Mitch Jul 04 '17 at 14:30