[SOLVED]remove a QGraphicsItem from a QGraphicsScene.

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

  • HI,

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

  • This is a small portion of the code :

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

    Place (int number);
    ~Place ();

    Add the item :

    case 0 :
    Place * place;
    place = new Place(places_indexs);
    place->setPos(mouseEvent->scenePos().x() - place->boundingRect().width() / 2,
    emit itemInserted(0);

    case 1:
    /* ............ */


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

  • Hi,

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

  • yes, this is a pointer to GraphicsScene !

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

  • 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()

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

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

    if(sourceItem->type() == Place::Type)
    else if(sourceItem->type() == Transition::Type)
    if(destItem->type() == Place::Type)
    else if(destItem->type() == Transition::Type)

    delete item;

    foreach(QGraphicsItem * item, selectedItems())
    delete item;


    Thanks for the help. :)

Log in to reply

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