PDA

View Full Version : Flat data source for QTreeView/Model



mawt
17th October 2009, 13:30
Greetings

I've made serveral attempts but im failing big time. I last tried this serveral months ago, only to simply give up and work on some library code.

I want a make a persistant TreeView widget across a network conection. To do that, I need to use a 'flat source' (ie: QTableView data), to provide an initial update, and successive edits/drag + drop operations. The easiest data source is a flat source because its serializable and diff'able.

I've tried field delegates that alter a foreign source and an sql backend that doesn't fire change events etc.

My problem is that I can't seem to find a "one model fits all" backend to both QTreeModel and QTableModels. I've been able to successfully build a tree from a flat source using pointer and non-pointer methods, but both mean I create an in memory tree stucture, which is hard to update/alter.

My data is something along the lines of:



QVector<QVariant*> headerData;
headerData << new QVariant(tr("Display"));
headerData << new QVariant(tr("Name"));
headerData << new QVariant(tr("Parent"));
rootItem = new TreeItem(headerData);

QVector<QVariant *> rootNodeData;
rootNodeData << new QVariant(tr("This is the root node"));
rootNodeData << new QVariant(tr("ROOT"));
rootNodeData << new QVariant(tr("."));

rootItem->insertChildren(rootItem->childCount(), 1, rootNodeData);
rootNode = rootItem->child(0);


Some sample data



TreeData * r1 = new TreeData();
r1->setString("Display", "Display txt");
r1->setString("Name", "Parent1");
r1->setString("Parent", "ROOT");

TreeData * r2 = new TreeData();
r2->setString("Display", "this is parent 2");
r2->setString("Name", "Parent2");
r2->setString("Parent", "ROOT");

TreeData * c1 = new TreeData();
c1->setString("Display", "First child");
c1->setString("Name", "Child1");
c1->setString("Parent", "Parent1");

TreeData * c2 = new TreeData();
c2->setString("Display", "This is the child of child1");
c2->setString("Name", "Child2");
c2->setString("Parent", "Child1");

QVector<TreeData*> treeDataVec;
treeDataVec << r1;
treeDataVec << r2;
treeDataVec << c1;
treeDataVec << c2;


Recursively populate children by "Parent" <-> "Name"



foreach (TreeData * td, treeData)
{
QString parentName = QString(td->getString("Parent").c_str());

TreeItem * currentRoot = recursiveFindByName(parentName, rootNode);
if (!currentRoot)
currentRoot = rootNode;

QString thisNodeName = currentRoot->data(1).toString();;

QVector<QVariant*> data;
data << new QVariant(QString(td->getString("Display").c_str()));
data << new QVariant(QString(td->getString("Name").c_str()));
data << new QVariant(QString(td->getString("Parent").c_str()));

currentRoot->insertChildren(currentRoot->childCount(), 1, data);
}


But this isn't very drag and drop friendly. What I really need is to use a model proxy. Can someone provide an example of this? Does it reduce down to a flat source that I can simply assign to a QTableView and alter in realtime, with the updates reflected in the Tree heirarchy?

Thanks

mawt

wysota
18th October 2009, 16:20
Aren't you duplicating what QStandardItemModel can already do?