I thought I could remove any item after it leaves outside the scene with the code below, but that is not the case. After trying different implementations I think I should try another approach. Some QGraphicsItems actually start outside of the boundingRect so I was wondering if there is a way to remove and delete GraphicsItems after they pass a certain coordinate point.
void Scene::advance()
{
QList <QGraphicsItem *> itemsToRemove;
foreach( QGraphicsItem * item, this->items())
{
if( !this->sceneRect().intersects(item->boundingRect()))
{
// The item is no longer in the scene rect, get ready to delete it
itemsToRemove.append(item);
}
}
foreach( QGraphicsItem * item, itemsToRemove )
{
this->removeItem(item);
delete(item);
}
QGraphicsScene::advance();
}
The problem is in this line: -
if( !this->sceneRect().intersects(item->boundingRect()))
This is comparing the scene's rect, which is in scene coordinates, with the item's bounding rect, which is in the item's local coordinate system.
You need to convert one of them so that you're comparing within the same coordinate system.
QRectF itemSceneBoundingRect = item->mapRectToScene(item->boundingRect());
if( !this->sceneRect().intersects(itemSceneBoundingRect)
{
// remove the item.
}