PDA

View Full Version : QGraphicsScene::items() Segmentation Fault



pmwalk
22nd February 2012, 19:03
I cannot find a way to get the item lists from a QGraphicsScene. When trying the items() method, the program always crashes with a segfault.

Here is the bit of code where the problem occurs:



void MainWindow::updateMapView() {
scene->addRect(QRectF(0, 0, 10, 10), scenePen, sceneBrush);
QList<QGraphicsItem*> items = scene->items();
for (int i = 0; i < items.size(); i++) {
scene->addItem(items[i]);
}
ui.map_view->setScene(scene);
}


If I remove the middle four lines (and just add the QRectF and set the scene), the code runs fine and the rectangle is displayed. Once I try and get the items, I get the fault.

Any hints? I'm at a bit of a loss. Thanks.

Edit: This also occurs for some other functions too, including QGraphicsScene::width() and height(). Not addItem(), or any of the other add functions though.

Spitfire
23rd February 2012, 13:16
What OS does it happen on?

I would be guessing invalid scene pointer but you're saying that you're able to addRect() on it.

Cna you create compilable example of the issue (without using UI files)?

pmwalk
23rd February 2012, 20:04
I figured out the problem. It's not reflected in the example I put up here, but in my code I was getting the items from one scene and adding it to the main scene. It some instances, that first scene wasn't getting initialized before the method was called, which caused the error. So it was an invalid pointer.

Thanks for the tip!

d_stranz
26th February 2012, 21:32
I cannot find a way to get the item lists from a QGraphicsScene. When trying the items() method, the program always crashes with a segfault.

Here is the bit of code where the problem occurs:



void MainWindow::updateMapView() {
scene->addRect(QRectF(0, 0, 10, 10), scenePen, sceneBrush);
QList<QGraphicsItem*> items = scene->items();
for (int i = 0; i < items.size(); i++) {
scene->addItem(items[i]);
}
ui.map_view->setScene(scene);
}


If I remove the middle four lines (and just add the QRectF and set the scene), the code runs fine and the rectangle is displayed. Once I try and get the items, I get the fault.

Any hints? I'm at a bit of a loss. Thanks.

Edit: This also occurs for some other functions too, including QGraphicsScene::width() and height(). Not addItem(), or any of the other add functions though.

Your code doesn't make any sense. In line 3 you are retrieving the list of items from the scene, then in your loop, you are adding them back into the same scene. This basically does nothing, since the scene already *owns* the items you are re-adding.

pmwalk
26th February 2012, 21:42
Your code doesn't make any sense. In line 3 you are retrieving the list of items from the scene, then in your loop, you are adding them back into the same scene. This basically does nothing, since the scene already *owns* the items you are re-adding.

Sorry about the mixup, the "scene" object in Line 3 should be "scene2"... it is a different scene. When i was abridging the code to post I made the typo. In any event, I've fixed the problem- it was due to the first scene not being properly initialized.

d_stranz
26th February 2012, 23:27
OK, that makes a little more sense. You do understand that by retrieving from scene1 and adding to scene2, you are transferring ownership of the items, right?

I don't know what the rest of your code looks like, but if you have already set the scene in your "map_view" (perhaps in the main window constructor), it isn't necessary to do it each time you update. If the view is already holding a pointer to the scene, the scene will automatically update the view when the content changes.

pmwalk
26th February 2012, 23:41
Yea I realize now the original way of doing this was a little convoluted, haha. That's a good point though, thanks. I'm only adding each item once at initialization now, and in the updateMapView() function just manipulating them as/if needed.