I have a custom AbstractItemModel implementation with the following to insert nodes
layoutAboutToBeChanged();
beginInsertRows(createIndex(p_parent->row(), 0, p_parent), start, end);
TreeNode* p_node = new TreeNode(p_parent, p_data);
p_parent->appendChild(start, p_node);
endInsertRows();
layoutChanged();
layoutAboutToBeChanged();
beginInsertRows(createIndex(p_parent->row(), 0, p_parent), start, end);
TreeNode* p_node = new TreeNode(p_parent, p_data);
p_parent->appendChild(start, p_node);
endInsertRows();
layoutChanged();
To copy to clipboard, switch view to plain text mode
And to remove rows:
layoutAboutToBeChanged();
beginRemoveRows(createIndex(p_parent->row(), 0, p_parent), row, row);
p_parent->removeChildren(row, row+1, this);
endRemoveRows();
layoutChanged();
layoutAboutToBeChanged();
beginRemoveRows(createIndex(p_parent->row(), 0, p_parent), row, row);
p_parent->removeChildren(row, row+1, this);
endRemoveRows();
layoutChanged();
To copy to clipboard, switch view to plain text mode
When removeChildren is called, for each node that is removed the following is done:
changePersistentIndex
(createIndex
(p_node
->row
(),
0, p_node
),
QModelIndex());
delete p_node;
changePersistentIndex(createIndex(p_node->row(), 0, p_node), QModelIndex());
delete p_node;
To copy to clipboard, switch view to plain text mode
This works. I can add nodes and remove nodes.
Terminology NOTE: I'm using "nodes" and "rows" interchangeably. Sorry for any confusion.
What doesn't work:
1. If a new row is inserted in front of a selected node. The newly
inserted node becomes selected. [This is not what I expect of want.]
2. If a row is selected and then later deleted, immediate core dump.
3. If mouse over a row that is deleted, immediate core dump.
If I don't delete p_node. Everything runs fine. But obviously that creates a memory leak.
What am I doing wrong?
For reference I'm using QT 5.0.2 on 64 bit linux.
Bookmarks