Delete QML Objects from C++
-
Hello i know how to create and update property's on qml from c++. But how do i delete/destroy the object from the qmlApplicationEngine.
//Creating QQmlApplicationEngine *qmlApplicationEngine = new QQmlApplicationEngine(); qmlApplicationEngine->load(QUrl(QStringLiteral("qrc:/main.qml"))); QQmlComponent component(qmlApplicationEngine, QUrl(QStringLiteral("qrc:/Video.qml"))); QQuickItem *qQuickItem = qobject_cast<QQuickItem*>(component.create()); QQmlEngine::setObjectOwnership(qQuickItem, QQmlEngine::CppOwnership); qQuickItem->setParent(qmlApplicationEngine); qQuickItem->setParentItem(qobject_cast<QQuickWindow*>(qmlApplicationEngine->rootObjects().at(0))->contentItem());
-
@Jens-van-koolwijk Hi, welcome to the Qt forum! You use
QQmlEngine::CppOwnership
, so the QML engine knows that it's your responsibility to delete the object. Just calldelete qQuickItem;
orqQuickItem->deleteLater();
. -
@Jens-van-koolwijk Hi, welcome to the Qt forum! You use
QQmlEngine::CppOwnership
, so the QML engine knows that it's your responsibility to delete the object. Just calldelete qQuickItem;
orqQuickItem->deleteLater();
.@Wieland Hello , thank you for the quick response. Perhaps I have not sufficiently explained my question. If for example, I 've created a video player in the QML file that I play . If I then remove the object while it is playing the audio stays but the visual gets black. If I stop the video first and then remove it seems that not everything cleared according to the QML debugger . what is the correct way . Do i need to make Deconstructor function in QML.
-
@Wieland Hello , thank you for the quick response. Perhaps I have not sufficiently explained my question. If for example, I 've created a video player in the QML file that I play . If I then remove the object while it is playing the audio stays but the visual gets black. If I stop the video first and then remove it seems that not everything cleared according to the QML debugger . what is the correct way . Do i need to make Deconstructor function in QML.
Hmm.. does this happen when you use
deleteLater()
? Because it shouldn't and if so then it's a bug. -
Is creating the object from QML instead of C++ an option? As noted, deleting the C++ object while the QML engine is running may have unexpected results.
If the lifetime of the object needs to be shorter than the lifetime of the parent item, http://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.html covers several options.
-
Is creating the object from QML instead of C++ an option? As noted, deleting the C++ object while the QML engine is running may have unexpected results.
If the lifetime of the object needs to be shorter than the lifetime of the parent item, http://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.html covers several options.
@jeremy_k
Thank you.I will try it out