Wolf900
29th February 2012, 15:14
I've been messing with the Simple DOM Model Example and I want to make everything editable. I have a problem with adding rows to the model.
Specifically, when I use insertAfter function in QDomNode:: parentNode(), the model gets updated only internally. When I collapse and expand the parent, the added node(s) are copies of the last nodes and are inserted in the end.
The only way I am able to get it right is save XML to QString and load it again into QDomModel and QTreeView. Then everything that has been inserted is where it should be. But then all the expanded states are lost! Not very user-friendly...
I have tried adding rows using insertRows function in QDomModel, however a copy of the entire model gets inserted as a child instead of an empty row!
// Get current index
QModelIndex currentTreeIdx = ui->treeView->selectionModel()->currentIndex();
// Get the node corresponding to that index
DomItem *itemRef = static_cast<DomItem*>(currentTreeIdx.internalPointer());
QDomElement newParentTag;
QDomElement newTextTag;
QDomText newText;
// Create tags
newParentTag = model->domDocument.createElement("Parent");
newTextTag = model->domDocument.createElement("Child");
newText = model->domDocument.createTextNode("Child text data");
newTextTag.appendChild(newText);
newSubtitleTag.appendChild(newTextTag);
// Attempt to insert a new tag after the currently selected item
itemRef->node().parentNode().insertAfter(newParentTag, itemRef->node());
Specifically, when I use insertAfter function in QDomNode:: parentNode(), the model gets updated only internally. When I collapse and expand the parent, the added node(s) are copies of the last nodes and are inserted in the end.
The only way I am able to get it right is save XML to QString and load it again into QDomModel and QTreeView. Then everything that has been inserted is where it should be. But then all the expanded states are lost! Not very user-friendly...
I have tried adding rows using insertRows function in QDomModel, however a copy of the entire model gets inserted as a child instead of an empty row!
// Get current index
QModelIndex currentTreeIdx = ui->treeView->selectionModel()->currentIndex();
// Get the node corresponding to that index
DomItem *itemRef = static_cast<DomItem*>(currentTreeIdx.internalPointer());
QDomElement newParentTag;
QDomElement newTextTag;
QDomText newText;
// Create tags
newParentTag = model->domDocument.createElement("Parent");
newTextTag = model->domDocument.createElement("Child");
newText = model->domDocument.createTextNode("Child text data");
newTextTag.appendChild(newText);
newSubtitleTag.appendChild(newTextTag);
// Attempt to insert a new tag after the currently selected item
itemRef->node().parentNode().insertAfter(newParentTag, itemRef->node());