Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 2

Qt-interest Archive, September 2007
Best way to clear a QGraphicsScene


Message 1 in thread

Hi all,

I have been looking for the best way to remove all QGraphicsItems from a QGraphicsScene. At the moment the simplest way seems to just reinstantiate it (myscene = new QGraphicsScene()), but somehow this does not seem like a "Good Way"(TM) to do things.

What would people here on the list recommend?

--
 [ signature omitted ] 

Message 2 in thread

Hi,
Christopher Rasch-Olsen Raa schrieb:
> Hi all,
>
> I have been looking for the best way to remove all QGraphicsItems from a QGraphicsScene. At the moment the simplest way seems to just reinstantiate it (myscene = new QGraphicsScene()), but somehow this does not seem like a "Good Way"(TM) to do things.
>   
You also need to  delete the scene, which cleans your graphic items.
> What would people here on the list recommend?
>
>   
This is what I use to clear a scene:
        // graphicsScene.clear()
        QList<QGraphicsItem *> list = gScene.items();
        QList<QGraphicsItem *>::Iterator it = list.begin();
        for ( ; it != list.end(); ++it )
        {
                if ( *it )
                {
                        gScene.removeItem(*it);
                        delete *it;
                }
        }

As you can see, this basically does the same what a "delete scene" would
do, except deleting the scene itself.
I haven't done a speed test, but  I do not think  either version has a
noticeable performance advantage.


 Jochen

--
 [ signature omitted ]