GraphicsScene::removeItem



  • This is source code in QT 5.9.

    /*!
        Removes the item \a item and all its children from the scene.  The
        ownership of \a item is passed on to the caller (i.e.,
        QGraphicsScene will no longer delete \a item when destroyed).
    
        \sa addItem()
    */
    void QGraphicsScene::removeItem(QGraphicsItem *item)
    {
        // ### Refactoring: This function shares much functionality with _q_removeItemLater()
        Q_D(QGraphicsScene);
        if (!item) {
            qWarning("QGraphicsScene::removeItem: cannot remove 0-item");
            return;
        }
        if (item->scene() != this) {
            qWarning("QGraphicsScene::removeItem: item %p's scene (%p)"
                     " is different from this scene (%p)",
                     item, item->scene(), this);
            return;
        }
    
        // Notify the item that it's scene is changing to 0, allowing the item to
        // react.
        const QVariant newSceneVariant(item->itemChange(QGraphicsItem::ItemSceneChange,
                                                        QVariant::fromValue<QGraphicsScene *>(0)));
        QGraphicsScene *targetScene = qvariant_cast<QGraphicsScene *>(newSceneVariant);
        if (targetScene != 0 && targetScene != this) {
            targetScene->addItem(item);
            return;
        }
    
        d->removeItemHelper(item);
    
        // Deliver post-change notification
        item->itemChange(QGraphicsItem::ItemSceneHasChanged, newSceneVariant);
    
        d->updateInputMethodSensitivityInViews();
    }
    

    Two problems:

    One: removeItem() won't free the item's memory, It's right? And if I want to delete an item(it's mean remove the item from the scene and free it's memory), I should write the code like removeItem(item); delete item;,right?

    Two:

    Can someone add more detailed comments in the code below:

    const QVariant newSceneVariant(item->itemChange(QGraphicsItem::ItemSceneChange,
                                                        QVariant::fromValue<QGraphicsScene *>(0)));
        QGraphicsScene *targetScene = qvariant_cast<QGraphicsScene *>(newSceneVariant);
        if (targetScene != 0 && targetScene != this) {
            targetScene->addItem(item);
            return;
        }
    
        d->removeItemHelper(item);
    
        // Deliver post-change notification
        item->itemChange(QGraphicsItem::ItemSceneHasChanged, newSceneVariant);
    
        d->updateInputMethodSensitivityInViews();
    

  • Moderators

    @Limer

    One: see http://doc.qt.io/qt-5/qgraphicsscene.html#removeItem
    Two: what code is this and what comments do you need?



  • @Limer Hi, friend.

    About first,You are right. Qt help manual

    void QGraphicsScene::removeItem(QGraphicsItem *item) Removes the item item and all its children from the scene. The ownership of item is passed on to the caller (i.e., QGraphicsScene will no longer delete item when destroyed).

    And about scene->clear(), Qt help manual:

    Removes and deletes all items from the scene, but otherwise leaves the state of the scene unchanged.

    You can also to see QListWidgetItem::takeItem to understand it well.

    QListWidgetItem *QListWidget::takeItem(int row) Removes and returns the item from the given row in the list widget; otherwise returns 0. Items removed from a list widget will not be managed by Qt, and will need to be deleted manually.

    About second. I also don't know.


  • Lifetime Qt Champion

    Hi,

    1. The documentation already says it: The ownership of item is passed on to the caller (i.e., QGraphicsScene will no longer delete item when destroyed)

    2. Again, read the documentation of the method: This virtual function is called by QGraphicsItem to notify custom items that some part of the item's state changes. By reimplementing this function, you can react to a change, and in some cases (depending on change), adjustments can be made. change is the parameter of the item that is changing. value is the new value; the type of the value depends on change.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.