Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create QML Element in C++?

Tags:

qt

qt5

qml

qtquick2

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?

like image 461
joekr Avatar asked Dec 30 '12 16:12

joekr


1 Answers

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!

like image 131
user975326 Avatar answered Nov 23 '22 22:11

user975326