Results 1 to 20 of 34

Thread: Map table to tree through model/view possible?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Map table to tree through model/view possible?

    Ah, good point.

    You certainly end up with a table and tree, both referencing the data held in the table and both having 4 rows (in this case) but yes, each table row becomes a tree column 0 element.

    My take on this was as stated in the title - map a table to a tree - I don't know whether the parent child relationship between table row elements is actually that important. It makes a great intellectual exercise though, well solved!
    Last edited by JD2000; 4th December 2009 at 10:18.

  2. #2
    Join Date
    Dec 2009
    Posts
    24
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Map table to tree through model/view possible?

    Quote Originally Posted by JD2000 View Post
    Ah, good point.

    You certainly end up with a table and tree, both referencing the data held in the table and both having 4 rows (in this case) but yes, each table row becomes a tree column 0 element.

    My take on this was as stated in the title - map a table to a tree - I don't know whether the parent child relationship between table row elements is actually that important. It makes a great intellectual exercise though, well solved!
    On the importance: currently, Qt framework doesn't allow any direct mapping between one table and a hierarchical structure. That's unfortunate, at least because both structures are isomorphic. Here a practical example: Suppose you gather data which is hierarchically representable, store it into a relational database (see my other post, here for reference: Kaufmann's Joe Celko's Trees and Hierarchies in SQL for Smarties) and which you want to reproduce/visualize. From such databases you only get tables... no trees.

    The whole problem isn't solved yet: you still have redundant trees: take the following table of primary keys forming the edges between two nodes (i.e. two other tables):
    11
    12
    23

    Both 1 and 2 from the second row refer tot the same PK from the first column, so they should form one tree with two children.

    Tree should look something like this:
    1
    |||1
    |||2
    2
    |||3

    Such tables are simple to generate with relational databases. What is needed is some way to translate them into trees. Wysota has made the first step. I will not bother him for the second, but just note this as an answer to your question.

    And as for how to make it.... unfortunately it is not that easy again. The most important is the fact that the parent is uniquely identified by the preceding (primary key) fields (and not by the column and it's PK, as it would seem from the relational model).

    Here is an example on how to populate the tree:

    for each row
    ---for each column
    ------check column = primary
    ------PK = readPKfrommodel(row,column)
    ------append PK into PathIDVector (which is the parent's ID)
    ------If PathIDVector does not exist yet --> make a treeitem(PathIDVector) which is the child of treeitem(PathIDVector_less_one)

    Currently I have this with TreeItem/TreeModel, and it's no pretty code. If I find time, I'll try to implement it in wysota's code, and post it here.

  3. #3
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Map table to tree through model/view possible?

    Tables and trees are not isomorphic, that is the problem.

    A tree holds what are essentially tables (their elements are indexed by [row,column]) in some sort of

    hierarchy. If you collapse a tree, you end up with a table.

    In order to impose such a hierarchy on a table, the table / table elements need to store their

    row/column data as usual plus references to their parents. Then when you rebuild the tree check this

    reference to find out which node to attach the table element to.

    Alternatively for example in cases like yours where there is a defined structure, the parent reference

    is not needed because it can be calculated by the model being used:


    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char **argv) {
    4. QApplication app(argc, argv);
    5.  
    6. int maxrows = 4;
    7. int maxcols = 4;
    8.  
    9. QStandardItemModel tableModel(maxrows, maxcols);
    10. for (int row = 0; row < maxrows; ++row) {
    11. for (int column = 0; column < maxcols; ++column) {
    12. QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
    13. tableModel.setItem(row, column, item);
    14. }
    15. }
    16.  
    17. QTableView tablev;
    18. tablev.setModel(&tableModel);
    19. tablev.show();
    20.  
    21. QStandardItemModel treeModel;
    22. QStandardItem *parentItem = treeModel.invisibleRootItem();
    23.  
    24. for (int row = 0; row < maxrows; ++row) {
    25. for (int col = 0; col < maxcols; ++col) {
    26. // grab the data out of the table
    27. QStandardItem *item = new QStandardItem(tableModel.data(tableModel.index(row, col, QModelIndex()), Qt::DisplayRole).toString());
    28. parentItem->appendRow(item);
    29. parentItem = item;
    30. }
    31. parentItem = treeModel.invisibleRootItem();
    32. }
    33.  
    34. QTreeView treev;
    35. treev.setModel(&treeModel);
    36. treev.show();
    37.  
    38. return app.exec();
    39.  
    40. }
    To copy to clipboard, switch view to plain text mode 

    The problem here is that after creation, the tree and table are independant of each other.

    I suppose that they could be kept in sync by implementing their respective data changed signals/slots.

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

    Default Re: Map table to tree through model/view possible?

    Quote Originally Posted by JD2000 View Post
    I suppose that they could be kept in sync by implementing their respective data changed signals/slots.
    Oh, I would really like to see you do that... Especially the part where items are added or removed.
    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.


  5. #5
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Map table to tree through model/view possible?

    I had not given it much thought to be honest, presumably in that case you would use the rows/columns inserted or removed signals. I was actually trying to respond to the
    On the importance: currently, Qt framework doesn't allow any direct mapping between one table and a hierarchical structure. That's unfortunate, at least because both structures are isomorphic. Here a practical example .........
    comment. I think it is a bit unfair to expect QT to provide a mapping between a 2D table and a 3D tree framework, infact it is impossible without some extra information relating to the structure of the tree.

    I note that you are no longer a guru, congratulations on your enlightenment Wysota!

Similar Threads

  1. Postgresql QSqlRelationalTableModel empty table
    By RolandHughes in forum Qt Programming
    Replies: 0
    Last Post: 12th November 2008, 17:18
  2. Replies: 3
    Last Post: 5th October 2008, 23:41
  3. Questions regarding setting up a Qt SQL Model/View
    By Methedrine in forum Qt Programming
    Replies: 3
    Last Post: 26th November 2007, 09:26
  4. Replies: 4
    Last Post: 11th September 2006, 14:13
  5. creating table plugin
    By mgurbuz in forum Qt Programming
    Replies: 3
    Last Post: 28th April 2006, 13:50

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
  •  
Qt is a trademark of The Qt Company.