Search code examples
c++qtqmlqtquick2qt-quick

QQmlComponent from module item


I want to create instance of QQmlComponent from QML script text instead of from a file, something like:

QQmlComponent comp(engine, "import QtQml 2.0; Item {}");

which would be the exact analogue of:

import QtQml 2.0

Component {
    Item {}
}

How to do this?


Solution

  • You must use void QQmlComponent::setData(const QByteArray &data, const QUrl &url):

    QQmlComponent component(&engine);
    component.setData("import QtQuick 2.7\n; Item {}", QUrl());
    component.create();