PDA

View Full Version : trouble making simple DOM model example editable



Mamra
26th September 2009, 14:01
I've been trying for some time now to make the "simple DOM model" example editable but most of my efforts failed. I managed to achieve some functionality but still can’t get exactly what I need. Other editable model examples wont help:(.
Is there someone that worked with this example in the past and transformed it into an editable model successfully? If there is, I’m dying to see his implementation or receive some guidance from him.
Thanks anyway.

wysota
26th September 2009, 16:22
What's wrong exactly?

Mamra
26th September 2009, 16:53
My problem right now is how to insert rows(children).I was able to remove them and set data on them but the insertion functionality wont work.
Nothing happens. I'm not sure what I'm supposed to do, each time I need to insert I create a new instand of the DomItem class and I insert a pointer to it in the last position of the QHash provided by the childs parent.
Is there something more that should be done?
I must be missing something:confused:.
I've worked with the model/view concept before and I never had such problems. Never had to use DomDocument ofcourse:o.

wysota
26th September 2009, 23:49
As far as I remember the nodes are only a part of representation of the dom tree. They don't actually touch the DOM tree in any way, they are just constructed from the tree. So if you want to add new tags to the tree, you need to both create the nodes and insert appropriate tags in the tree.

Mamra
28th September 2009, 10:30
This is how I try to add one new child.
In fact all I need is to append it.


QDomNode node;
DomItem *item = new DomItem(node, childItems.count()+1, this);
domNode.appendChild(node);
childItems.insert(childItems.count()+1 ,item);

First I create a new null node.
Then a new DomItem object(the class that represents data upon the node).
Finaly I append the node to the fathers QDomNodeList and the new DomItem pointer to the fathers QHash.

After this is executed I get the following message:
QTreeView:Internal representetion of the tree corrupted.Reseting

Any ideas...?

wysota
28th September 2009, 11:17
What about emitting proper signals rom the model?

Mamra
1st October 2009, 14:40
I'm not sure which signals you mean...layoutAboutToBeChanged() or rowsAboutToBeInserted() perhaps?
I think beginInsertRows() will take care of those things

Anyway, this is how my insert mechanism looks like
Is this what you have in mind?


bool DomModel::insertRows(int position, int rows, const QModelIndex& parent)
{
DomItem *parentItem;

if (!parent.isValid())
parentItem = rootItem;
else
parentItem = static_cast<DomItem*>(parent.internalPointer());

//only need to append one child
beginInsertRows(parent, position, position);

if(!parentItem->insertChild())
return false;

endInsertRows();

return true;
}

bool DomItem::insertChild()
{
int rows = childItems.count();

QDomNode node;
DomItem *item = new DomItem(node, rows+1, this);
domNode.appendChild(node);
childItems.insert(rows+1, item);

return true;
}


I keep getting the same message(which was written wrong in my last post:o)

QTreeView::rowsInserted internal representation of the model has been corrupted, resetting.

It looks like bad indexing occurs...but I'm not sure why that happens.