Using a Loader for a string of QML

Posted by Robbert on Stack Overflow See other posts from Stack Overflow or by Robbert
Published on 2014-06-07T16:09:51Z Indexed on 2014/06/07 21:25 UTC
Read the original article Hit count: 385

Filed under:
|
|

In Qt 5.3 I've been using the Loader QML element for loading screens. Now I'm trying to load a string of QML dynamically. Qt.createQmlObject enables me to do so, but I can't seem to get the Loader element to play along.

Seems like Loader only takes a URL (QUrl) or component (QQmlComponent), but Qt.createQmlObject creates an object (QObject).

I'm new to QML, but from my understanding components are reusable elements, similar to classes, and objects are the instances of those classes. I thus can't seem to wrap my head around why Loader wouldn't work with objects.

How can I show a loading screen while (asynchronously) parsing and initializing a string of QML?

Example QML code:

Item {
    Rectangle {id: content}

    Loader {id: loader}

    Component.onCompleted: {
        var obj = Qt.createQmlObject('import QtQuick 2.3; Rectangle {}', content);

        loader.source = obj; // Throws error.
    }
}

© Stack Overflow or respective owner

Related posts about qt

Related posts about asynchronous