Qt World Summit: Register Today!

How to delete a QDeclarativeItem in cpp?

  • -Hello ,

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

    QDeclarativeContext *itemContext = new QDeclarativeContext(engine.rootContext());
    QDeclarativeComponent component(&engine);
    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.

  • Maybe using item->deleteLater()?

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

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

  • 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?

  • 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?

  • 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?

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

Log in to reply