Simple DOM Model - making it editable - inserting rows
-
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()removed.insertAfter(newParentTag, itemRef->node());
@I've tried to inform the model by calling
@emit dataChanged(idx, idx);@in DomModel : QAbstractItemModel when QModelIndex idx is updated. It seems it does nothing.
I've even tried to call it for each index in the model.
Basically, to add to this, what I'm trying to achieve is combine "Editable Tree Model" and "Simple Dom Model" examples. So I'm having the latter as a base and I've copied the edit functions over there, changed the flags, setData, etc.
-
It seems I have a problem in the insertChildren function. As it is now, it replaces the selected row with an empty row.
@
bool DomItem::insertChildren(int position, int count, int columns){
if (position < 0 || position > childItems.size()){
return false;
}for (int row = 0; row < count; row++){ DomItem *item = new DomItem(*(new QDomNode()), row, this); // this doesn't seem to be correct... childItems.insert(position, item); } return true;
}
@ -
I've used
@item->node().toElement() @method that has the necessary "set" functions to modify the table. Refer to "my answer on stackoverflow":http://stackoverflow.com/questions/7998843/qt-qtreeview-editable-dom-model/8054115#8054115
-
I've dug up some old code, I think it's what you need for removing but I might be wrong. Most of the things were taken from the standard Qt4 class examples.
DomModel is a subclass of QAbstractItemModel
DomItem is a separate class containing QDomNode@
DomModel::deleteIndex(const QModelIndex &index){
DomItem item = static_cast<DomItem>(index.internalPointer());
DomItem parent_item = static_cast<DomItem>(index.parent().internalPointer());
QDomNode node = item->node();
QDomNode parent_node = parent_item->node();
parent_node.toElement().removeChild(node);
return true;
}@
and it's called by
@
QModelIndex index = ui->treeView->currentIndex(); // treeView is a subclass of QTreeView
model->deleteIndex(index); // DomModel *model;
@And inserting is done using the methods (currentTreeIdx is a variable of QModelIndex type)
@
DomItem itemRef = static_cast<DomItem>(currentTreeIdx.internalPointer()); // reference itemnewMainTag = model->domDocument.createElement("Main"); newTextTag = model->domDocument.createElement("Text"); newText = model->domDocument.createTextNode("***New Text Data***"); // Add text data itself newTextTag.appendChild(newText); newMainTag.appendChild(newTextTag); itemRef->node().toElement().appendChild(newMainTag);
@
I remember inserting was still buggy, I couldn't keep the cursor in the same place after inserting.