Create QML Element in C++?

joekr picture joekr · Dec 30, 2012 · Viewed 9.2k times · Source

I have the following problem:

I load an initial interface into a QQuickView using a .qml file. I then want to add QML types like an Image or Text to the interface using C++.

I know I can manipulate existing elements from C++ but how can I create new types and add them?

Answer

user975326 picture user975326 · Jul 4, 2013

Unfortunately the docs are a bit out-of-date (uses Qt4 api), but If you read section Loading QML Components from C++ here: https://doc.qt.io/qt-4.8/qtbinding.html

Then you should have something like (with Qt5 api):

QQuickView view;
view.setSource(QUrl::fromLocalFile("MyView.qml"));
QQmlComponent component(view.engine()
        , QUrl::fromLocalFile("MyItem.qml"));
QObject *object = component.create();

This gives you a QObject from a .qml file, but what's missing is how to add this to the view. In qml, items won't get drawn unless they are parented to the view. One way of doing this is to add the item to the root context, like so:

QQmlProperty::write(object, "parent"
                    , QVariant::fromValue<QObject*>(view.rootObject()));

Also, note (again from the above link): "You should always use QObject::setProperty(), QDeclarativeProperty or QMetaProperty::write() to change a QML property value, to ensure the QML engine is made aware of the property change".

Next, we need to set the ownership of the item, otherwise the JavaScript garbage handler can delete your item and you can seg fault randomly.

QQmlEngine::setObjectOwnership(object, QQmlEngine::CppOwnership);

Finally, you need to remember to delete the object "object". Since it's a QObject you should use:

object->deleteLater();

Hope that helps someone!