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. GraphicsScene::removeItem
Forum Updated to NodeBB v4.3 + New Features

GraphicsScene::removeItem

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 4 Posters 5.9k Views 1 Watching
  • 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.
  • LimerL Offline
    LimerL Offline
    Limer
    wrote on last edited by Limer
    #1

    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();
    
    jsulmJ joeQJ 2 Replies Last reply
    0
    • LimerL Limer

      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();
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Limer

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • LimerL Limer

        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();
        
        joeQJ Offline
        joeQJ Offline
        joeQ
        wrote on last edited by
        #3

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

        Just do it!

        1 Reply Last reply
        1
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1

          • Login

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