Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    [SOLVED]remove a QGraphicsItem from a QGraphicsScene.

    General and Desktop
    3
    7
    15856
    Loading More Posts
    • 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
      issam last edited by

      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 Reply Quote 0
      • M
        mcosta last edited by

        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 Reply Quote 0
        • I
          issam last edited by

          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 Reply Quote 0
          • M
            mcosta last edited by

            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 Reply Quote 0
            • I
              issam last edited by

              yes, this is a pointer to GraphicsScene !

              http://www.iissam.com/

              1 Reply Last reply Reply Quote 0
              • C
                ChrisW67 last edited by

                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 Reply Quote 0
                • I
                  issam last edited by

                  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 Reply Quote 0
                  • First post
                    Last post