Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Delete QML Objects from C++
QtWS25 Last Chance

Delete QML Objects from C++

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 5.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jens van koolwijk
    wrote on last edited by Jens van koolwijk
    #1

    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());
    
    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      @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 call delete qQuickItem; or qQuickItem->deleteLater();.

      J 1 Reply Last reply
      0
      • ? A Former User

        @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 call delete qQuickItem; or qQuickItem->deleteLater();.

        J Offline
        J Offline
        Jens van koolwijk
        wrote on last edited by
        #3

        @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.

        ? 1 Reply Last reply
        0
        • J Jens van koolwijk

          @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.

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          Hmm.. does this happen when you use deleteLater()? Because it shouldn't and if so then it's a bug.

          1 Reply Last reply
          0
          • jeremy_kJ Offline
            jeremy_kJ Offline
            jeremy_k
            wrote on last edited by
            #5

            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.

            Asking a question about code? http://eel.is/iso-c++/testcase/

            J 1 Reply Last reply
            0
            • jeremy_kJ jeremy_k

              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.

              J Offline
              J Offline
              Jens van koolwijk
              wrote on last edited by Jens van koolwijk
              #6

              @jeremy_k
              Thank you.I will try it out

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved