Repository navigation
Take the node id from the library when deleting a node - #556
Merged
Merged
Conversation
The undo commands read the node id from the JSON that saveNode() returns. The default saveNode() returns an empty object. toInt() of a missing key is 0, and 0 is a valid node id (InvalidNodeId is the maximum value). So nothing reported the error. A UI delete called deleteNode(0) and left the selected node in place. Undo and paste then used a null graphics object. The contract that the JSON must hold the id was never written down. Every example and TestGraphModel override saveNode(), so the default path never ran. The library now sets "id" on every saveNode() result. It also skips a node or connection that the model did not restore, instead of using a null graphics object. The docs say that undo of a delete needs saveNode() and loadNode(). With the defaults the node is deleted correctly, undo does not restore it and nothing crashes. The tests in the next commit fail on the code before this commit. With the default saveNode(), delete calls deleteNode(0). Undo of a delete and paste crash with SIGSEGV. Fixes #490
Cover DeleteCommand, CreateCommand, CopyCommand and PasteCommand with a model that implements saveNode() and loadNode(). The test model starts its node ids at 5 and the tests delete a middle node, so that a wrong id 0 cannot hide a bug. A subclass records the ids that reach deleteNode(). Add cases for a model that keeps the default saveNode() and loadNode() (issue 490). They pass with the previous commit. Without it, delete calls deleteNode(0), and undo of a delete and paste crash with SIGSEGV.
3 of 6 tasks
The undo commands read node ids with QJsonValue::toInt(). For a number above the int range, toInt() returns its default value. A node id of 2^31 or more became 0 or InvalidNodeId. This is the same failure as in the missing-id case: delete called deleteNode(0), and undo and paste lost the node. Ids are written as qint64, and NodeId is unsigned. Add jsonValueToUInt32() and use it in the undo commands and in fromJson() for connection ids. The test model now writes and reads ids as 64-bit values too. The new tests use a model whose ids start at 3000000000. They fail without this change.
paceholder
force-pushed
the
fix-delete-node-id
branch
from
November 23, 2026 10:07
3da2586 to
0742233
Compare
3 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Type of change
Description
Deleting a node in the UI called
deleteNode(0)when the model kept the defaultsaveNode(). The undo commands took the node id from the JSON thatsaveNode()returns. The default returns{}, so the id was missing and became 0. The wrong node was deleted, undo could not restore the node, and paste crashed.The commits are in this order, and each one builds and passes on its own:
Take the node id from the library, not from saveNode. The library writes"id"itself and checks for missing graphics objects on insert. The docs now say whatsaveNode()andloadNode()are for: without them undo of a delete cannot restore the node.Test delete, undo and paste of nodes through the undo stack. New fileTestSerializedItemCommands.cpp: 9 coverage cases and 3 regression cases. Against the old source, the paste case crashes (SIGSEGV) and the delete case fails with{0} == {6}.Read node ids from JSON as 64-bit numbers.QJsonValue::toInt()returns 0 for a number above the int range, so an id of 2^31 or more was lost in the same way. The two new tests use ids from 3000000000 and fail on the source of commit 2 with{0} == {3000000001}. It is separate so it can be dropped alone.saveNode()andloadNode()stay as the undo hook in 3.x. The redesign is tracked in #554. The sametoInt()problem in scene load and group restore is fixed in the follow-up PR.Testing
Breaking changes?
No. A model that already writes
"id"insaveNode()still works.Related issue
Fixes #490
Related: #554