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:
forest->append(new Node(NULL, "new root node"));
scene->resetScene();
forest->append(new Node(NULL, "new root node"));
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):
{
emit signalNodeClicked(node);
}
void NodeItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
emit signalNodeClicked(node);
}
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
void MainWindow::slotNodeClicked(Node *node)
{
currentTool->nodeClicked(node);
}
void MainWindow::slotNodeClicked(Node *node)
{
currentTool->nodeClicked(node);
}
To copy to clipboard, switch view to plain text mode
and this action is processed:
void AddNodeTool::nodeClicked(Node *node)
{
Node *newNode = new Node(node, "child");
node->children.append(newNode);
scene->resetScene(); //this calls the clear() method and causes segmentation fault
}
void AddNodeTool::nodeClicked(Node *node)
{
Node *newNode = new Node(node, "child");
node->children.append(newNode);
scene->resetScene(); //this calls the clear() method and causes segmentation fault
}
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
foreach(Node* tree, *forest)
{
coords = addNodeItemToScene(tree, coords);
coords.setY(0);
}
foreach(Node* tree, *forest)
{
coords = addNodeItemToScene(tree, coords);
coords.setY(0);
}
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.....
Bookmarks