问题
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?
回答1:
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!
回答2:
Go here: http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-definetypes.html, scroll to bottom and look at heading "Defining Visual Items with the QtQuick Module"
That gives you idea how it should be done.
This link provides examples how it should be done (thought it is written for Qt 4.7, but it shouldn't be much different from Qt 5): http://developer.nokia.com/Community/Wiki/Creating_a_custom_QML_element_with_Qt
NOTE: Don't forget to use (inherit) QQuickItem class instead of QDeclarativeItem.
来源:https://stackoverflow.com/questions/14092319/create-qml-element-in-c