PDA

View Full Version : Adding new items to model (MVC)



Urvin
2nd November 2010, 08:21
Hello!
I've created my own model, that bases on Blanchet-Summerfield methon from all-known book.
So, I have an Node object. Every Node has a pointer to parent node and a list of child node. Model has the "main Root Node", which parent == 0 ad child are the needed model's nodes.
The "main Root Node" doesn't appear in view, only its nodes:

RootNode - invisiple
--Node1
----Node1.1
----Node1.2
--Node2
--Node3

At first time I fill this model by data using beginResetModel and endResetModel functions.
But there are cases, when I want to add a few nodes into the model, but don't want to reset it whole. Additionaly, I use a proxy sorting model.

Reading Qt help I see that I should use beginInsertRows and endInsertRows functions. The problem is that functions have a QModelIndex parameter - parent node.
I don't use QModelIndex at all!
To know node class from QModelIndex presented I use this construction, as written in the book:

tUMClientsNode *lNode = static_cast<tUMClientsNode*>(aParent.internalPointer());
tUMClientsNode - my node class, aParent - QModelIndex.

What should I do to get QModelIndex from my Node? How to use beginInsertRows correctly?

To add a single item I use similar code now:

void tUMClientsModel::AddNewClient(tUMDCClientInfo &aNewClient, bool aUseBeginInsert)
{
if (aNewClient.fClientParentID == 0)
fRootNode->fChildrenList.append(new tUMClientsNode(fRootNode, aNewClient));
else
for (int i = 0; i < fRootNode->fChildrenList.count(); i++)
if (fRootNode->fChildrenList[i]->fClientInfo.fClientID == aNewClient.fClientParentID)
{
fRootNode->fChildrenList[i]->fChildrenList.append(
new tUMClientsNode(fRootNode->fChildrenList[i], aNewClient)
);
break;
}
}

Thank you!

Urvin
7th November 2010, 07:54
Here's full code of my model: http://rapidshare.com/files/429357472/tree_model_urvin.zip
Please! :crying: