Results 1 to 2 of 2

Thread: Flat data source for QTreeView/Model

  1. #1
    Join Date
    Oct 2009
    Posts
    1
    Qt products
    Qt4 Qt/Embedded

    Default Flat data source for QTreeView/Model

    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:

    Qt Code:
    1. QVector<QVariant*> headerData;
    2. headerData << new QVariant(tr("Display"));
    3. headerData << new QVariant(tr("Name"));
    4. headerData << new QVariant(tr("Parent"));
    5. rootItem = new TreeItem(headerData);
    6.  
    7. QVector<QVariant *> rootNodeData;
    8. rootNodeData << new QVariant(tr("This is the root node"));
    9. rootNodeData << new QVariant(tr("ROOT"));
    10. rootNodeData << new QVariant(tr("."));
    11.  
    12. rootItem->insertChildren(rootItem->childCount(), 1, rootNodeData);
    13. rootNode = rootItem->child(0);
    To copy to clipboard, switch view to plain text mode 

    Some sample data

    Qt Code:
    1. TreeData * r1 = new TreeData();
    2. r1->setString("Display", "Display txt");
    3. r1->setString("Name", "Parent1");
    4. r1->setString("Parent", "ROOT");
    5.  
    6. TreeData * r2 = new TreeData();
    7. r2->setString("Display", "this is parent 2");
    8. r2->setString("Name", "Parent2");
    9. r2->setString("Parent", "ROOT");
    10.  
    11. TreeData * c1 = new TreeData();
    12. c1->setString("Display", "First child");
    13. c1->setString("Name", "Child1");
    14. c1->setString("Parent", "Parent1");
    15.  
    16. TreeData * c2 = new TreeData();
    17. c2->setString("Display", "This is the child of child1");
    18. c2->setString("Name", "Child2");
    19. c2->setString("Parent", "Child1");
    20.  
    21. QVector<TreeData*> treeDataVec;
    22. treeDataVec << r1;
    23. treeDataVec << r2;
    24. treeDataVec << c1;
    25. treeDataVec << c2;
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. foreach (TreeData * td, treeData)
    2. {
    3. QString parentName = QString(td->getString("Parent").c_str());
    4.  
    5. TreeItem * currentRoot = recursiveFindByName(parentName, rootNode);
    6. if (!currentRoot)
    7. currentRoot = rootNode;
    8.  
    9. QString thisNodeName = currentRoot->data(1).toString();;
    10.  
    11. QVector<QVariant*> data;
    12. data << new QVariant(QString(td->getString("Display").c_str()));
    13. data << new QVariant(QString(td->getString("Name").c_str()));
    14. data << new QVariant(QString(td->getString("Parent").c_str()));
    15.  
    16. currentRoot->insertChildren(currentRoot->childCount(), 1, data);
    17. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Flat data source for QTreeView/Model

    Aren't you duplicating what QStandardItemModel can already do?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. data rate transfer is decreasing in TCP connection
    By navi1084 in forum Qt Programming
    Replies: 3
    Last Post: 19th June 2009, 16:15
  2. Best way to display lots of data fast
    By New2QT in forum Newbie
    Replies: 4
    Last Post: 16th October 2008, 22:46
  3. remote ODBC data source
    By mchara in forum Qt Programming
    Replies: 7
    Last Post: 10th December 2007, 10:39
  4. Connecting to an ODBC Data Source
    By kikapu in forum Newbie
    Replies: 9
    Last Post: 25th June 2007, 12:18
  5. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.