Component setData for loading a bunch of qml files from memory.

  • I want to load qml files from memory using Component.setData .

    The syntax is:
    void QQmlComponent::setData(const QByteArray & data, const QUrl & url) [slot]

    What i would like to know if it is possible to load alot of components to memory and work from there, given the QUrl as their name+relative_location.

    execute like ./qmltest tutorial1.qml

    my current example returns this error:

    QQmlComponent: Component is not ready
    "file:///tmp/hello.qml:4:1: NiButton is not a type"

    my current example follows:

    #include <QtCore>
    #include <QtQuick/QtQuick>
    #include <QtQml/qqml.h>
    #include <QDebug>
    #include <Qt/qapplication.h>

    QQuickItem * createComponent(QQmlEngine * in_engine, QQmlContext * in_context, QByteArray in_data, QUrl in_url=QUrl(), QQuickItem * in_parent=0) {
    QQmlComponent *component = new QQmlComponent (in_engine);
    component->setData(in_data, in_url);
    QQuickItem *rect = qobject_cast<QQuickItem *> ( component->create(in_context) );

    if (!component->errors().isEmpty()) {
        foreach (const QQmlError &error, component->errors())
        qWarning() << error.toString();
        return 0;
    if (in_parent)
        rect->setParentItem(qobject_cast<QQuickItem *>(in_parent));
    return rect;


    int main(int argc, char ** argv)
    QApplication app(argc, argv);
    QQuickView *qxView = new QQuickView();

    QString pluginImportPath;
    QQmlEngine* engine = qxView->engine();
    if (pluginImportPath.isEmpty()) {
        QDir cur = QDir::current();;
        pluginImportPath = cur.absolutePath();

    Q(pluginImportPath );

    // QQmlContext * tmp_context = new QQmlContext( qxView->rootContext());
    QQmlContext * tmp_context = qxView->rootContext();

    app.arguments().size() ? qxView->setSource(app.arguments()[1]) : qxView->setSource(QUrl("hello.qml"));
    QUrl url1 = QUrl::fromLocalFile&#40;pluginImportPath+"/"+"hello.qml"&#41;;
    QByteArray data1("import QtQuick 2.0\n import \".\"\n Rectangle{ width:200; height:200; color:\"red\";\nNiButton{}}");
    QUrl url2 = QUrl::fromLocalFile&#40;pluginImportPath+"/"+"NiButton.qml"&#41;;
    QByteArray data2("import QtQuick 2.0\n Rectangle{ width:200; height:250; color:\"green\";}");
    QQuickItem * parent1 = app.arguments().size() ? qxView->rootObject() : 0 ;
    QQuickItem * item2   = createComponent(engine,tmp_context,data2,url2);
    QQuickItem * item1   = createComponent(engine,tmp_context,data1,url1,parent1);
    QObject::connect(qxView->engine(), SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));
    return app.exec();


    And a qml file to serve as qml root called tutorial1.qml:
    import QtQuick 2.0

    Rectangle {
    id: page
    width: 320; height: 480
    color: "lightgray"
    Text {
    id: helloText
    text: "Hello world!"
    y: 30
    anchors.horizontalCenter: page.horizontalCenter
    font.pointSize: 24; font.bold: true

  • Problem is with NiButton. Why do you create it dynamically as Item2 since you declaring it in item1?
    This two files (hello.qml and NiButton.qml) are in the same directory but i think that "import "."" part is problem. Try "import ""+pluginImportPath+""\n". I suppose that import "." is not reference to hello.qml file directory.

  • Thanks for the reply.
    Could you try if it works for you then.
    I made all combinations of paths(executable dir, etc) and couldn't get it to work.

  • Could You provide tarball of above code with examplary qml files ?

Log in to reply

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