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
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

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

    Quote Originally Posted by Davor View Post
    That is exactly what I thought... until I started coding.

    I don't understand the index() method: the arguments over-determine the subject. Whats the meaning of row, column AND parent. Either row/column OR parent is more than enough to devise the index. I don't understand the logic.
    Imagine a treeview like so:
    #root
    #-first tree
    #---first tree subitem
    #-second tree
    #---second tree subitem

    So 'first tree' and 'second tree' will require a parent of 'root', whilst the subitems will request the first/second tree as parents as appropriate. Once you have that, THEN you can use the row and column. Without the parent, the row and column are meaningless.

  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 fatjuicymole View Post
    Imagine a treeview like so:
    #root
    #-first tree
    #---first tree subitem
    #-second tree
    #---second tree subitem

    So 'first tree' and 'second tree' will require a parent of 'root', whilst the subitems will request the first/second tree as parents as appropriate. Once you have that, THEN you can use the row and column. Without the parent, the row and column are meaningless.
    Ok, so what should I return then from index() if parent is invalid? QModelIndex()?

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

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

    Here is something interesting: from QAbstractItemModel Class Reference for “parent()”:

    […] A common convention used in models that expose tree data structures is that only items in the first column have children. For that case, when reimplementing this function in a subclass the column of the returned QModelIndex would be 0.[...]

    The meaning of “conventions” is left open: which implications has it on the model? Same with “would be”: is it an option or a necessity to make it 0?


    In the end, I really feel like wasting time. In the meantime I could have written 50 classic trees, so to speak. The Qt Model/View documentation doesn't say anything about Model-View interaction. It just says what you should implement for certain thing to occur, and let the magic work, and if magic fails, then one's only option is to delve into the sourcecode and try to figure out the internal workings.

    Up till now, I tried many things. They outcomes/debuggings all lack logic to me. And in the end, I don't even see why one would need pointers to make trees. It seems an unnecessary complication. Why? Because the underlying Model/View structure is represented in tables in the first place, and because trees are representable as graphs (with constraints), and the two structures (one could probably argue) are completely isomorphic, given the constraints. (There are books written about this, mainly for the practical purpose of translating the (old) hierarchical databases into the relational ones.)

    I'll just use the TreeItem and TreeModel for my implementation, of simply the QTreeWidget..

    Thanks for your help anyway.

  4. #4
    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?

    As I understand it, you have

    Table(row, column) is the parent of Table(row, column+1)

    It would seem to me that all you need to do is iterate through your table, adding items to the tree, in pseudo code this would be:

    For row
    insert parent

    For column
    insert child
    next column

    next row

    the number of rows and colums can be obtained from your tableModel:

    TableModel->row count();
    TableModel->columncount();

    the methods for inserting nodes are included in the QTreeWidgetItem class.

  5. #5
    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
    As I understand it, you have

    Table(row, column) is the parent of Table(row, column+1)

    It would seem to me that all you need to do is iterate through your table, adding items to the tree, in pseudo code this would be:

    For row
    insert parent

    For column
    insert child
    next column

    next row

    the number of rows and colums can be obtained from your tableModel:

    TableModel->row count();
    TableModel->columncount();

    the methods for inserting nodes are included in the QTreeWidgetItem class.
    Indeed, it’s funny considering the fact that that the model/view was made for easy programming. Or maybe it’s just my bad luck having this problem...

    PS: wysota, you have no idea how much I would love to share just a fraction of your insight in this model/view thing! Thanks for trying to solve it.

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

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

    Quote Originally Posted by Davor View Post
    Ok, so what should I return then from index() if parent is invalid? QModelIndex()?
    Yes, the same as you would if row or column was invalid.

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.