0
votes

I have subclassed both a Qgraphicsscene and a Qgraphicsitem, it seems it works but trying to remove the items by subclass recognition don't works. This delete the items:

void debugSceneItemscuatrobis()
{
    QList<QGraphicsItem *> allitems = items();
        foreach(auto item, allitems) {
            removeItem(item);
        }
    }

But this doesn't, it recognizes there are items but doesn't remove them, tryed different posseibilities but couldn't make it works.

void debugSceneItemscuatrotris()
{
    QList<QGraphicsItem *> allitems = items();
        foreach(auto item, allitems) {
        if(item->type() == chord::Type) {
            removeItem(item);
            delete item;
         }
        }
    }

This is how the items were added by the qgraphicsitem subclass:

void chord::addchord(QPointF sp)
{
    scene()->addLine(sp.x(), sp.y(), sp.x()+10, sp.y()+10);
        QList<int> midics = {10, 30, 40};
      for(int i = 0; i < midics.length(); i++)
          {
        QGraphicsSimpleTextItem *item = new QGraphicsSimpleTextItem("n");
        item->setFont(QFont("omheads", 20));
        item->setPos(sp.x(), sp.y()+midics[i]);
        scene()->addItem(item);
        coso.append(item);
      }
}

Sorry, I'm very newbie and no programmer, those are my first subclasses. Someone knows how it could be approached? Thanks. :-)

1

1 Answers

0
votes

Without seeing more of your code I'm only guessing. But that guess would be that when you remove an item of type chord you are still able to see the various QGraphicsItems that were added to the scene in chord::addchord. If so it's probably due to the lack of any parent/child relationship between the chord and those items: from the documentation for QGraphicsScene::removeItem(item)...

Removes the item item and all its children from the scene.

Try creating the parent/child relationship explicitly by changing your chord:addchord implementation to...

void chord::addchord (QPointF sp)
{
    auto *line = scene()->addLine(sp.x(), sp.y(), sp.x() + 10, sp.y() + 10);
    line->setParentItem(this);
    QList<int> midics = { 10, 30, 40 };
    for (int i = 0; i < midics.length(); i++)
    {
        QGraphicsSimpleTextItem *item = new QGraphicsSimpleTextItem("n", this);
        item->setFont(QFont("omheads", 20));
        item->setPos(sp.x(), sp.y() + midics[i]);
        scene()->addItem(item);
        coso.append(item);
    }
}

It might not solve all of the issues but should (hopefully) head you in the right direction.