Create new Element on demand
-
Hello!
Can i create new elements,such as Buttons,or element in List on demand? For example,if i receive a new data-i add new QML ListElement in my view?Thanks.
-
Yes, you can use Loader element or instantiate QML objects from JS/ C++.
-
Can you post some example? Or link to it?
-
From QML you can use the Qt.createQmlObject() method:
@object Qt::createQmlObject(string qml, object parent, string filepath)@
Like for example:
@var newObject = Qt.createQmlObject('import QtQuick 2.0; Rectangle {color: "red"; width: 20; height: 20}', parentItem, "dynamicSnippet1");@
From C++:
@QQmlEngine *engine = new QQmlEngine;
QQmlComponent component(engine, QUrl::fromLocalFile("main.qml"));
QObject *myObject = component.create();@ -
QML "Loader":http://qt-project.org/doc/qt-4.8/qml-loader.html element.
"Dynamic object creation":http://qt-project.org/doc/qt-4.8/qml-qt.html#dynamic-object-creation in QML.
-
Thanks for you answer!