[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
    {
    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 !



  • 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()
    {
    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. :)


Log in to reply
 

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