Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to delete a QDeclarativeItem in cpp?

How to delete a QDeclarativeItem in cpp?

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 4 Posters 4.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.
  • P Offline
    P Offline
    pp42168
    wrote on last edited by
    #1

    -Hello ,

    in my app, i create some QDeclarativeItem objects in cpp, using the code below

    @
    QDeclarativeContext *itemContext = new QDeclarativeContext(engine.rootContext());
    QDeclarativeComponent component(&engine);
    component.loadUrl(QUrl::fromLocalFile(source));
    QDeclarativeItem *item = qobject_cast<QDeclarativeItem *>(component.create(itemContext));
    @

    then I add this item into my GraphicsView to show with other some GraphicsItems, this item is created dynamically, and i need to remove it from the GraphicsView as well.

    the question is that HOW CAN I DELETE THIS QDeclarativeItem??

    when I remove the item from the GraohicsViews' scene and delete the item , i'll have an runtime error.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tobias.hunger
      wrote on last edited by
      #2

      Maybe using item->deleteLater()?

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pp42168
        wrote on last edited by
        #3

        Do I need to delete this anyway?
        or QML will delete the object when the app is closing?

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tobias.hunger
          wrote on last edited by
          #4

          I do not know whether you need to delete the object or not. That is hard to decide based on the small snippet of code you provided.

          When the application has closed the OS makes sure all memory is reclaimed.

          1 Reply Last reply
          0
          • B Offline
            B Offline
            blam
            wrote on last edited by
            #5

            If the item is removed from a QGraphicsScene using QGraphicsScene::removeItem(), the ownership of the item is transferred back to the caller, so deleting this item should not cause an error. Does deleting the item have some other side effect within your code that is causing the error?

            1 Reply Last reply
            0
            • P Offline
              P Offline
              pp42168
              wrote on last edited by
              #6

              It is ok if I remove an ordinary QGraphicsItem, but here the item is a QDeclarativeItem, it is created by "component.create()" function, does it means the item's ownership is belong to QDeclarativeComponent or QDeclarativeEngine?

              Is it because the QDeclarativeEngine is still evaluating the QDeclarativeItem after I removed it from the scene?

              how to stop QDeclarativeEngine from evaluating a QDeclarativeItem?

              1 Reply Last reply
              0
              • B Offline
                B Offline
                blam
                wrote on last edited by
                #7

                It sounds like a bug if the item cannot be deleted cleanly. What is the runtime error? Do you have an example that reproduces the error?

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  srikanth_trulyit
                  wrote on last edited by
                  #8

                  You should take a look at the way QDeclarative hanldes dynamic items at http://doc.qt.nokia.com/4.7/qdeclarativedynamicobjects.html

                  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