Results 1 to 7 of 7

Thread: QGraphicsScene::items() Segmentation Fault

  1. #1
    Join Date
    Feb 2012
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QGraphicsScene::items() Segmentation Fault

    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:

    Qt Code:
    1. void MainWindow::updateMapView() {
    2. scene->addRect(QRectF(0, 0, 10, 10), scenePen, sceneBrush);
    3. QList<QGraphicsItem*> items = scene->items();
    4. for (int i = 0; i < items.size(); i++) {
    5. scene->addItem(items[i]);
    6. }
    7. ui.map_view->setScene(scene);
    8. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by pmwalk; 23rd February 2012 at 00:34.

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsScene::items() Segmentation Fault

    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)?

  3. #3
    Join Date
    Feb 2012
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsScene::items() Segmentation Fault

    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!

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,256
    Thanks
    239
    Thanked 674 Times in 664 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QGraphicsScene::items() Segmentation Fault

    Quote Originally Posted by pmwalk View Post
    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:

    Qt Code:
    1. void MainWindow::updateMapView() {
    2. scene->addRect(QRectF(0, 0, 10, 10), scenePen, sceneBrush);
    3. QList<QGraphicsItem*> items = scene->items();
    4. for (int i = 0; i < items.size(); i++) {
    5. scene->addItem(items[i]);
    6. }
    7. ui.map_view->setScene(scene);
    8. }
    To copy to clipboard, switch view to plain text mode 

    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.

  5. #5
    Join Date
    Feb 2012
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsScene::items() Segmentation Fault

    Quote Originally Posted by d_stranz View Post
    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.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,256
    Thanks
    239
    Thanked 674 Times in 664 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QGraphicsScene::items() Segmentation Fault

    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.

  7. #7
    Join Date
    Feb 2012
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsScene::items() Segmentation Fault

    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.

Similar Threads

  1. segmentation fault!!
    By Yayati.Ekbote in forum Qt Programming
    Replies: 4
    Last Post: 24th March 2010, 16:10
  2. segmentation fault
    By navid in forum Qt Programming
    Replies: 3
    Last Post: 20th December 2009, 12:40
  3. Segmentation fault
    By impeteperry in forum Qt Programming
    Replies: 5
    Last Post: 29th December 2008, 19:59
  4. Segmentation Fault
    By merry in forum General Programming
    Replies: 4
    Last Post: 12th March 2007, 05:08
  5. QGraphicsScene segmentation fault
    By maxpower in forum Qt Programming
    Replies: 2
    Last Post: 20th November 2006, 19:28

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.