Results 1 to 7 of 7

Thread: Clearing GraphisScene causes SIGSEGV

  1. #1
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Angry Clearing GraphisScene causes SIGSEGV

    Hi,

    I have a custom QGraphicsscene like this
    Qt Code:
    1. class DisjointSetsScene : public QGraphicsScene
    2. {
    3. Q_OBJECT
    4. public:
    5.  
    6. //...
    7.  
    8. DisjointSetsScene(QObject *parent, /* ... */);
    9. //...
    10.  
    11. signals:
    12. void signalNodeClicked(Node *node);
    13. // ...
    14. };
    To copy to clipboard, switch view to plain text mode 

    with some constructor
    Qt Code:
    1. DisjointSetsScene::DisjointSetsScene(QObject *parent, /* ... */)
    2. : QGraphicsScene(parent)
    To copy to clipboard, switch view to plain text mode 

    I'm adding some custom graphics items and lines like this
    Qt Code:
    1. NodeItem *nodeItem = new NodeItem();
    2. nodeItem->setRect(startCoords.x(), startCoords.y(), 30, 30);
    3. nodeItem->setBrush(nodeBrush);
    4. nodeItem->setPen(nodePen);
    5. //...
    6. addLine(startCoords.x() + 15, startCoords.y() + 15, endCoords.x() + 15, endCoords.y() + 15, arrowPen);
    7. //...
    8. addItem(nodeItem);
    To copy to clipboard, switch view to plain text mode 

    Now, at some point (when a node is clicked), i need to repaint the whole scene. So i want to clear it first, however, this thing
    Qt Code:
    1. qDeleteAll(items());
    To copy to clipboard, switch view to plain text mode 
    gives me SIGSEGV. Any suggestions?

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Clearing GraphisScene causes SIGSEGV

    why do you want to delete everything just because you want to repaint? crazy.

    And if you want to delete the items, then you will probably need to remove() them from the scene first!
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Clearing GraphisScene causes SIGSEGV

    Hi,
    thank you for your answer.

    As for deleting everything -> i didn't express myself clearly enough. I need to move nearly all the objects and delete some of them. Therefore its a lot easier to "rebuild" the whole scene with new objects. I'm aware of the overhead, but optimalization is not an issue now.

    Searching web i found this function (slot) http://doc.qt.digia.com/qt/qgraphicsscene.html#clear
    The problem, however, remains the same.

    To be more specific, I have a forest graph which i'm trying to display and manipulate. I have a "add new tree to the forest" slot connected to a button which triggers this action:
    Qt Code:
    1. forest->append(new Node(NULL, "new root node"));
    2. scene->resetScene();
    To copy to clipboard, switch view to plain text mode 
    This works fine, the scene is changed correctly.

    Further, I have a signal being emited from each "NodeItem" on the scene whenever it's clicked (the "node" passed in the signal is a pointer into the data structure representing my forest):
    Qt Code:
    1. void NodeItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
    2. {
    3. emit signalNodeClicked(node);
    4. }
    To copy to clipboard, switch view to plain text mode 

    The signal is passed to the main window, where a method is called based on the current tool selected
    Qt Code:
    1. void MainWindow::slotNodeClicked(Node *node)
    2. {
    3. currentTool->nodeClicked(node);
    4. }
    To copy to clipboard, switch view to plain text mode 
    and this action is processed:
    Qt Code:
    1. void AddNodeTool::nodeClicked(Node *node)
    2. {
    3. Node *newNode = new Node(node, "child");
    4. node->children.append(newNode);
    5. scene->resetScene(); //this calls the clear() method and causes segmentation fault
    6. }
    To copy to clipboard, switch view to plain text mode 

    My suggestion was that it can be problem that the object that emits the original signal (instance of NodeItem, which is a subclass of QGraphicsEllipseItem) gets deleted in the clear() method...however, when I commented the call to clear(), SIGSEGV moved just one line further to
    Qt Code:
    1. foreach(Node* tree, *forest)
    2. {
    3. coords = addNodeItemToScene(tree, coords);
    4. coords.setY(0);
    5. }
    To copy to clipboard, switch view to plain text mode 

    Finally, when I comment the problematic scene->resetScene(); line mentioned above and call it later via the slot in the first code snippet in this message (which btw also adds new tree to the forest), all works fine and the scene is rebuild correctly with new nodes added.....
    Last edited by Lord_Navro; 24th November 2012 at 16:09.

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Clearing GraphisScene causes SIGSEGV

    I'm not going to try and decipher 50% story and 50% code. Check your destructors (incidentally you haven't shown any of that code...).

    read my sig.
    Last edited by amleto; 24th November 2012 at 16:23.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. #5
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Clearing GraphisScene causes SIGSEGV

    This is my complete project: https://github.com/LordNavro/DisjointSets I dont have destructors as I dont need any by now. Thanks for any suggestion.

  6. #6
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Clearing GraphisScene causes SIGSEGV

    I just took a quick peek at your code. In createTools(void) you are using an unitialized pointer to the scene. See if changing the the order of the statements in the mainwindow constructor helps:
    Qt Code:
    1. forest = new QList<Node *>;
    2.  
    3. /* createActions();
    4.   createMenus();
    5.   createToolBars();
    6.   createTools();
    7. */
    8. QBrush brush(Qt::green);
    9. QPen pen(Qt::yellow);
    10. scene = new DisjointSetsScene(this, forest, DisjointSetsScene::TREE, pen, brush, pen);
    11. connect(scene, SIGNAL(signalNodeClicked(Node*)), this, SLOT(slotNodeClicked(Node*)));
    12. view = new QGraphicsView(scene, this);
    13.  
    14. createActions();
    15. createMenus();
    16. createToolBars();
    17. createTools();
    18.  
    19. this->setCentralWidget(view);
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to norobro for this useful post:

    Lord_Navro (24th November 2012)

  8. #7
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Clearing GraphisScene causes SIGSEGV

    I'm so stupid. I'm so stupid. I'm so stupid. I'm so stupid.

    God bless you, norobro.

Similar Threads

  1. Sigsegv
    By babygal in forum Newbie
    Replies: 3
    Last Post: 2nd November 2010, 05:29
  2. QTextEdit SIGSEGV
    By Flayer in forum Qt Programming
    Replies: 4
    Last Post: 14th January 2010, 22:52
  3. sigsegv ?
    By mero in forum Qt Programming
    Replies: 1
    Last Post: 28th November 2009, 18:01
  4. QSqlQueryModel::setQuery - SIGSEGV
    By onamatic in forum Qt Programming
    Replies: 14
    Last Post: 27th January 2009, 10:26
  5. Program crashes (SIGSEGV)
    By Voldemort in forum Qt Programming
    Replies: 47
    Last Post: 21st May 2007, 20:09

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.