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. [SOLVED]remove a QGraphicsItem from a QGraphicsScene.
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]remove a QGraphicsItem from a QGraphicsScene.

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 17.6k 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.
  • I Offline
    I Offline
    issam
    wrote on 3 Apr 2013, 00:32 last edited by
    #1

    hi all,

    I have a problem when i try to remove a graphicsItem from a QGraphicsScene instance. The message error is :

    QGraphicsScene::removeItem: item 0x844c398's scene (0x0) is different from this scene (0x8386188)

    Have someone an idea ?

    http://www.iissam.com/

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on 3 Apr 2013, 07:14 last edited by
      #2

      HI,

      can you post your code?
      The error means that you never added the item to the scene.

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • I Offline
        I Offline
        issam
        wrote on 3 Apr 2013, 13:39 last edited by
        #3

        This is a small portion of the code :

        The item class :
        @
        class Place : public QObject, public QGraphicsEllipseItem
        {
        Q_OBJECT
        public:
        enum { Type = UserType + 1 };

        Place (int number);
        ~Place ();
        }
        @

        Add the item :

        @
        switch(scenemode)
        {
        case 0 :
        Place * place;
        place = new Place(places_indexs);
        this->addItem(place);
        place->setPos(mouseEvent->scenePos().x() - place->boundingRect().width() / 2,
        mouseEvent->scenePos().y());
        emit itemInserted(0);
        places_indexs++;

         break;
        case 1:
         break;
        /* ............ */
        

        }
        @

        I use a QMainWindows as the application's main window. The cental area is a TabView and each tab contains a QGraphicsSene !

        http://www.iissam.com/

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on 3 Apr 2013, 17:08 last edited by
          #4

          Hi,

          in the second piece of code, this is a pointer to GraphicsScene?

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          0
          • I Offline
            I Offline
            issam
            wrote on 4 Apr 2013, 23:16 last edited by
            #5

            yes, this is a pointer to GraphicsScene !

            http://www.iissam.com/

            1 Reply Last reply
            0
            • C Offline
              C Offline
              ChrisW67
              wrote on 5 Apr 2013, 05:21 last edited by
              #6

              You are:

              • adding the item to a scene other than the one you think you are;
              • trying to remove it from scene other than the one it was inserted in; or
              • trying to remove an item that was deleted earlier (which removes it from the owning scene and means you are using a dangling pointer).

              It would help if we had some code that related to the removeItem call that fails, and some context for where/when this happens.

              1 Reply Last reply
              0
              • I Offline
                I Offline
                issam
                wrote on 5 Apr 2013, 20:07 last edited by
                #7

                Hi, friend ! I have posted, before the site crash, the code. I have solved the problem.
                The problem is that : in the scene i added three items : pathitem, ellipse and rectangle. Each item had a grahicstextitem children as label. the label is movable and selectable. So, when i selected the three items, the labels are also selected as separated items. thus, the selectedItems() method will return 6 selected items in a list not 3 as i thought. And so when i delete a parent item its children in the list remains without parent, so without scene ;) this is the problem.
                the correct code is :

                @
                /* remove selected item(s) */
                void PNetScene::removeItems()
                {
                if(selectedItems().isEmpty())
                return;

                foreach(QGraphicsItem * item, selectedItems())
                if(item->type() == QGraphicsSimpleTextItem::Type
                || item->type() == Rectangle::Type)
                item->setSelected(false);

                foreach(QGraphicsItem * item, selectedItems()){
                if(item->type() == Arrow::Type)
                {
                Arrow * arrow = qgraphicsitem_cast<Arrow*>(item);
                removeItem(item);
                QGraphicsItem * sourceItem = arrow->getStartItem();
                QGraphicsItem * destItem = arrow->getEndItem();

                if(sourceItem->type() == Place::Type)
                qgraphicsitem_cast<Place*>(sourceItem)->deleteRelation(arrow);
                else if(sourceItem->type() == Transition::Type)
                qgraphicsitem_cast<Transition*>(sourceItem)->deleteRelation(arrow);
                if(destItem->type() == Place::Type)
                qgraphicsitem_cast<Place*>(destItem)->deleteRelation(arrow);
                else if(destItem->type() == Transition::Type)
                qgraphicsitem_cast<Transition*>(destItem)->deleteRelation(arrow);

                delete item;
                }
                }

                foreach(QGraphicsItem * item, selectedItems())
                {
                removeItem(item);
                delete item;
                }

                }
                @

                Thanks for the help. :)

                http://www.iissam.com/

                1 Reply Last reply
                0

                1/7

                3 Apr 2013, 00:32

                • Login

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