Results 1 to 7 of 7

Thread: Building Tree view from Table view which is already build.

  1. #1
    Join Date
    Jan 2013
    Location
    Bangalore
    Posts
    49
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Building Tree view from Table view which is already build.

    Hi,

    I have already build table view (by customizing QAbstractItemModel & QTableView).
    My table view looks as below


    Qt Code:
    1. Level Name Rank Sec
    2. 0 Rajesh 1 A
    3. 1 Krish 2 A
    4. 2 Ram 3 B
    5. 1 Krish 4 C
    6. 0 Krish 5 B
    7. 1 Krish 6 a
    8. 1 Krish 7 A
    9. 0 Rajesh 1 A
    10. 1 Krish 2 A
    11. 2 Ram 3 B
    12. 0 Rajesh 1 A
    13. 1 Krish 2 A
    14. 2 Ram 3 B
    To copy to clipboard, switch view to plain text mode 


    Now based on the level column I need to build the tree, Is it possible to re use the same model(from table view) and create tree view based on level column ?
    or do I need to create separate new model for tree again. If it is possible please let me know what i need to implement for this.

    Tree view : all the Level 0's are parents, followed by 1's are childs of 0's and 2's are childs of 1's.

    Thanks in Advance :-)

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Building Tree view from Table view which is already build.

    Your model would need to maintain data such that a parent/child relationship is maintained so that your QTreeView can display the parent/child hierarchy.

    You should be able to also use the same model to display a QTableView, your model would essentially need to present data for all of the leaf nodes, with the Level of the parent being returned as one of the columns, etc.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Building Tree view from Table view which is already build.

    While it is possible to change structure with a proxy model, it is not something I would recommend unless you are already quite skilled with the model/view internals.

    Creating a tree model that works on the same data as your table model sounds a lot easier.

    Cheers,
    _

  4. #4
    Join Date
    Jan 2013
    Location
    Bangalore
    Posts
    49
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Building Tree view from Table view which is already build.

    Hi, I am implementing Lazy population tree view.

    have implemented hasChilderen(), fetchMore and canFetchMore() functions as below.
    I am getting the Error: QSortFilterProxyModel: invalid inserted rows reported by source model;

    Please let me know the problem.

    Qt Code:
    1. bool TreeModel::canFetchMore(const QModelIndex &parent ) const
    2. {
    3. if(!parent.isValid())
    4. return true;
    5.  
    6. if(m_totalRowCount > m_lastreadRowIndex) //I am storing lst read row value
    7. {
    8. return true;
    9. }
    10.  
    11. return false;
    12. }
    13.  
    14. void DebugTreeModel::fetchMore(const QModelIndex& parent)
    15. {
    16.  
    17. //to fect readRows od records (rows) from sql
    18. int readRows = 1000;
    19.  
    20. if( m_totalRowCount < ( readRows + m_lastreadRowIndex ) )
    21. {
    22. readRows = m_totalRowCount - m_lastreadRowIndex;
    23. }
    24.  
    25. //will get me last row number
    26. qint64 lastRow = m_lastreadRowIndex ;
    27.  
    28. emit fetchMoreData(readRows); //will fecth readRows number of records form the sql and form tree
    29.  
    30. qint64 parentsInFetchedData = getZerosInFetchedData();
    31.  
    32.  
    33. //i have writtem logic so that, next inserted row should be 0 (so parent should be m_rootItem always (it is my top most parent))
    34. //so I am passing m_rootItem as root and last inserted row number, num of row to insert.
    35. beginInsertRows(QModelIndex(), lastRow , lastRow+parentsInFetchedData -1 ); //same result for beginInsertRows(parent, lastRow , lastRow+parentsInFetchedData -1 );
    36. endInsertRows();
    37. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by DURGAPRASAD NEELAM; 19th April 2015 at 20:06.

  5. #5
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Building Tree view from Table view which is already build.

    What have you done to debug this? When you are inserting rows, what range of row numbers are you passing to beginInsertRows, etc?

    Also, I believe someone else had mentioned model test as a good way to debug models. Have you tried that?

    Edit: I would add that trying to implement canFetchMore and fetchMore for a model that can't properly insert data seems like putting the cart before horse to me. Focus on getting the basics working before you try to pimp out your model!

  6. #6
    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: Building Tree view from Table view which is already build.

    Quote Originally Posted by DURGAPRASAD NEELAM View Post
    Hi, I am implementing Lazy population tree view.

    have implemented hasChilderen(), fetchMore and canFetchMore() functions as below.
    I am getting the Error: QSortFilterProxyModel: invalid inserted rows reported by source model;

    Please let me know the problem.
    This is a cross-post from http://www.qtcentre.org/threads/6224...h-lazy-loading
    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.


  7. #7
    Join Date
    Jan 2013
    Location
    Bangalore
    Posts
    49
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Building Tree view from Table view which is already build.

    Quote Originally Posted by anda_skoa View Post
    While it is possible to change structure with a proxy model, it is not something I would recommend unless you are already quite skilled with the model/view internals._
    Could you please explain little more about this, small example or any link to explore would be great :-)

    Thanks.

Similar Threads

  1. view return stored procedure in table view
    By baradar in forum Qt Programming
    Replies: 5
    Last Post: 15th November 2014, 20:24
  2. table view and tree view
    By MKSPulok in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2012, 22:48
  3. Display sql table menu in a tree view
    By mat.brighi in forum Newbie
    Replies: 2
    Last Post: 19th October 2011, 00:49
  4. Replies: 5
    Last Post: 3rd April 2010, 05:07
  5. Replies: 33
    Last Post: 7th December 2009, 11:11

Tags for this Thread

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.