PDA

View Full Version : Deleting and re-creating a QGraphicsScene



dohzer
15th August 2010, 07:36
I've got a widget with a QGraphicsView and a QGraphicsScene, shown below.
I want to delete all items from the scene and create new ones, so I'm trying to delete the scene and create a new one.

myGraphicsWidget Constructor:

myGraphicsWidget::myGraphicsWidget(QWidget *parent)
: QWidget(parent)
{
//Create a new scene
myScene = new QGraphicsScene(this);
myScene->setSceneRect(0, 0, 300.0, 300.0 );

//Creates items in scene myScene
populateScene(myScene);

myView = new QGraphicsView(myScene);
myView->setRenderHints(QPainter::Antialiasing);

QVBoxLayout *myLayout = new QVBoxLayout;
myLayout->addWidget(myView);
setLayout(myLayout);
}

newScene:

void myGraphicsWidget::newScene(void)
{
//Delete previous scene
delete myScene;

//Create a new scene
myScene = new QGraphicsScene(this);

//Creates items in scene myScene
populateScene(myScene);

//Code here to update the widget/view???? <-- Need code here
}

When I try to run newScene(), I get a blank Widget.
Is there some code I need to insert at the end of newScene() to get it to update in the QGraphicsView and widget, or is this technique of deleting, creating and repopulating the scene not going to work?
Should I be deleting the individual items from the scene instead of deleting the whole scene?

tbscope
15th August 2010, 07:41
You also need to set the scene in the view.
You do this in the constructor of your widget via myView = new QGraphicsView(myScene);

I guess, I didn't check though, that there is a setScene(...) function in QGraphicsView

dohzer
15th August 2010, 08:24
Yeah, I actually just realised before checking for a reply. It worked when I tried! :D
Sorry for all these silly questions... I really need to look harder before asking, but sometimes I just never see the answers no matter how hard I look. :(

It's not a 'bad programming practice' to delete and recreate a scene like that, is it?

tbscope
15th August 2010, 08:48
It's not a 'bad programming practice' to delete and recreate a scene like that, is it?

It depends. What do you mean with changing items?
If you only want to replace a couple of items in your scene, or change some properties, then the overhead of deleting the scene and repopulating it is very high.

The QGraphicsView classes are based on the model/view design pattern. This means the data is separate from the actual painting. This is nice, since you don't have to do the paint updating yourself, you just change your data. Custom items do need to implement painting routines though (which is normal). But this also means you can extend the scene class for example and add more control features to it. Since the scene contains a list of all your items, you can use and extend the scene to do specific operations. For example: set the pen color of all lines to red.

This of course means you need to implement some functions like setLineColor(color) which iterates the items in the scene, looks if an item is a line (for example) and then sets the color.

Urthas
16th August 2010, 19:42
It's not a 'bad programming practice' to delete and recreate a scene like that, is it?

Like tbscope said, it depends. However, just out of curiosity, did you happen to notice the clear() slot in QGraphicsScene? Alternately, you could roll your own using items(), which returns a list of all items on the scene for you to iterate through.

dohzer
17th August 2010, 04:16
Like tbscope said, it depends. However, just out of curiosity, did you happen to notice the clear() slot in QGraphicsScene? Alternately, you could roll your own using items(), which returns a list of all items on the scene for you to iterate through.
I want to get rid of all items and create new ones, and the clear() function won't delete the items from memory, will it?
I was thinking of using items() like you say, but there's not much point if I'm simply deleting all the items, is there?

aamer4yu
17th August 2010, 06:50
I want to get rid of all items and create new ones, and the clear() function won't delete the items from memory, will it?

From the docs -

void QGraphicsScene::clear () [slot]

Removes and deletes all items from the scene, but otherwise leaves the state of the scene unchanged.

dohzer
18th August 2010, 16:52
I'm really going to have to learn to read the documentation more thoroughly. :(
I could swear I was reading a thread (I believe it was from another Qt forum... no idea of the link) and they said that clear() didn't delete the items that were in the scene from program memory; it just removed them from the scene. Perhaps they were talking about a previous version of Qt, but more likely I just read it wrong.