Results 1 to 14 of 14

Thread: Need to show SQL data in tree view with lazy loading.

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

    Default Need to show SQL data in tree view with lazy loading.

    Hi,

    I have a huge data in mysql data base. The data in data base as follows for example.

    Qt Code:
    1. Level Date name rank
    2. 0 1/1/15 Rajesh 1
    3. 1 1/1/15 Krish 2
    4. 2 1/1/15 Ram 3
    5. 1 1/1/15 Krish 4
    6. 0 1/1/15 Krish 5
    7. 1 1/1/15 Krish 6
    8. 1 1/1/15 Krish 7
    9. 0
    10. 1
    11. 1
    12. 2
    13. 2
    14. 1
    15. .
    16. .
    17. .
    To copy to clipboard, switch view to plain text mode 
    So, From the above table I need to build the tree view based on level column.
    All zeros are parents followed by 1’s are child's of 0's above that, and 2’s are child's of 1’s above that (tree should be formed as below).



    Tree:

    0
    ---------1
    ---------------------------2
    ----------1
    0
    -----------1
    -----------1
    0
    -----------1
    -----------1
    ---------------------------2
    ---------------------------2
    ------------1

    I have huge set of records, more than 20 lak , here I have to do lazy loading i.e. I should not read all the data and put in some container and then building parent and child relationship.

    Lazy Loading: instead of reading and putting all the data memory, read the data which is currently viewing.

    Here I have to do lazy loading, I want read the data of how many tree items I am showing on view currently.
    I am not able to get how to build parent and child relationship with this requirement,
    Any suggestions for the above requirement ???

    Requirement = Mysql data base (huge set of data) + Tree view + Lazy loading. (I am using Qt 4.8.5)


    Thanks in advance.
    Last edited by DURGAPRASAD NEELAM; 15th April 2015 at 16:30.

  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: Need to show SQL data in tree view with 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.


  3. The following user says thank you to wysota for this useful post:

    DURGAPRASAD NEELAM (15th April 2015)

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

    Default Re: Need to show SQL data in tree view with lazy loading.

    Hi wysota,

    Thanks for the replay. I have gone through the functions you have suggested above, and I have gone through the Fetch-more example presents in the Qt example's.
    After this I came to the conclusion (May be I'm wrong, correct me if I'm) that, at the end (as we go down with scroll bar till end) we will have all the data into memory (All the records will be present in memory), Right ?

    If so, Could you please suggest a way to implement this with out loading all the data(even at the end).. at any point of time I want to load only the data which I'm showing in view.

    Thanks :-)
    Last edited by DURGAPRASAD NEELAM; 15th April 2015 at 21:52.

  5. #4
    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: Need to show SQL data in tree view with lazy loading.

    Quote Originally Posted by DURGAPRASAD NEELAM View Post
    Hi wysota,

    Thanks for the replay. I have gone through the functions you have suggested above, and I have gone through the Fetch-more example presents in the Qt example's.
    After this I came to the conclusion (May be I'm wrong, correct me if I'm) that, at the end (as we go down with scroll bar till end) we will have all the data into memory (All the records will be present in memory), Right ?
    It depends how you implement your model. It is entirely up to you to decide which data to keep in memory and which to discard.

    If so, Could you please suggest a way to implement this with out loading all the data(even at the end).. at any point of time I want to load only the data which I'm showing in view.
    Use QContignousCache as storage in the model and fetch data from the remote server if user requests range of data which is not in the cache. Note however this will cause stalls.
    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.


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

    Default Re: Need to show SQL data in tree view with lazy loading.

    Hi, Some how I could able to read and show the data in view with lazy population, But has below problems.

    I have implemented canFetchData & featchData functions
    I am using proxy model too, setting below TreeModel as a sourse model;


    Qt Code:
    1. bool TreeModel::canFetchMore(const QModelIndex &parent ) const
    2. {
    3. qint64 totalRowCount = sql_reader->getTotalRowsNumber(); //returns Total no.of rows
    4.  
    5. if(totalRowCount > lastreadRowNumber) //I am storing Last read Row
    6. {
    7. return true; //still not reached end of the table
    8. }
    9.  
    10. return false;
    11. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void TreeModel::fetchMore(const QModelIndex& parent)
    2. {
    3. qint64 totalRows = sql_reader->getRowNumber();
    4. int readBulk = 1000; //reading 1000 rows on demand
    5.  
    6. if( totalRows < ( readBulk + lastreadRowNumber) )
    7. {
    8. readRows = totalRows - lastreadRowNumber; //adjustment for last few rows
    9. }
    10.  
    11.  
    12. beginInsertRows(QModelIndex(), m_lastreadRowIndex, m_lastreadRowIndex+readRows-1);
    13.  
    14. emit fetchMoreData(lastreadRowNumber, m_lastreadRowIndex+readRows);
    15.  
    16. //by emiting signal, I am getting data from sql database of range above
    17. //with that data I am forming My Parent and child relationship
    18. //and saving lastreadRowNumber
    19.  
    20. endInsertRows();
    21. }
    To copy to clipboard, switch view to plain text mode 


    //With this I am able to load all the data, But when loading a data, I am getting below messages on console

    QSortFilterProxyModel: invalid inserted rows reported by source model

    //When I try to move and exapand the tree i am able to do it for 2/3 times then tool suddnley geting crashed by showing
    Error message

    QSortFilterProxyModel: invalid inserted rows reported by source model


    When I tried: beginInsertRows(parent, m_lastreadRowIndex, m_lastreadRowIndex+readRows-1);
    I got below error and tool got crashed

    QSortFilterProxyModel: invalid inserted rows reported by source model
    QSortFilterProxyModel: index from wrong model passed to mapFromSource

    QSortFilterProxyModel: index from wrong model passed to mapFromSource
    QSortFilterProxyModel: index from wrong model passed to mapFromSource



    Could you please let me know, what is the wrong thing I am doing here ?

  7. #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: Need to show SQL data in tree view with lazy loading.

    First your implementation of canFetchMore and fetchMore are incorrect if what you are building is a tree model because you are neglecting the parent index. Second of all if you want to lazy populate the data and only keep the data the user sees in the view, how do you expect this to happen if you apply a sorting model on top of that? If sorting requires the first and last row to be nwighbours then obviously your base model will need to contain both the first and last item and thus it will need all other items as well.
    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.


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

    Default Re: Need to show SQL data in tree view with lazy loading.

    1. as you said, isn't it possible to apply sorting/filtering with proxy Model when we are doing lazy load ? , If so, Is there any way to achieve this ??

    2. and As you observed I am loading data step by step, this I will get back as i am still exploring and implementing.

    Thanks :-)

  9. #8
    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: Need to show SQL data in tree view with lazy loading.

    Quote Originally Posted by DURGAPRASAD NEELAM View Post
    1. as you said, isn't it possible to apply sorting/filtering with proxy Model when we are doing lazy load ? , If so, Is there any way to achieve this ??
    Lazy initialization is not the problem. Your other requirement is.
    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.


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

    Default Re: Need to show SQL data in tree view with lazy loading.

    1st requirement is lazy loading.
    2nd is, need to apply filtering only on top of this(sorting is not required).
    Last edited by DURGAPRASAD NEELAM; 18th April 2015 at 09:38.

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

    Default Re: Need to show SQL data in tree view with lazy loading.

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

    Please let me know where to change the code, I have lost somewhere and tree view is bit confusing for me.


    Qt Code:
    1. bool TreeModel::hasChildren ( const QModelIndex & parent ) const
    2. {
    3. DebugTreeItem *parentItem;
    4. if ( parent.column() > 0 )
    5. return false;
    6.  
    7. if ( ! parent.isValid() )
    8. parentItem = m_rootItem;
    9. else
    10. parentItem = static_cast<DebugTreeItem*>( parent.internalPointer() );
    11.  
    12. return parentItem->hasChildren();
    13. }
    14.  
    15. bool TreeModel::canFetchMore(const QModelIndex &parent ) const
    16. {
    17. if(!parent.isValid())
    18. return true;
    19.  
    20. if(m_totalRowCount > m_lastreadRowIndex) //I am storing lst read row value
    21. {
    22. return true;
    23. }
    24.  
    25. return false;
    26. }
    27.  
    28. void TreeModel::fetchMore(const QModelIndex& parent)
    29. {
    30.  
    31. //to fect readRows od records (rows) from sql
    32. int readRows = 1000;
    33.  
    34. if( m_totalRowCount < ( readRows + m_lastreadRowIndex ) )
    35. {
    36. readRows = m_totalRowCount - m_lastreadRowIndex;
    37. }
    38.  
    39. //will get me last row number
    40. qint64 lastRow = m_lastreadRowIndex ;
    41.  
    42. emit fetchMoreData(readRows); //will fecth records and form tree
    43.  
    44. qint64 parentsInFetchedData = getZerosInFetchedData();
    45.  
    46.  
    47. //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))
    48. //so I am passing m_rootItem as root and last inserted row number, num of row to insert.
    49. beginInsertRows(QModelIndex(), lastRow , lastRow+parentsInFetchedData -1 ); //same result for beginInsertRows(parent, lastRow , lastRow+parentsInFetchedData -1 );
    50. endInsertRows();
    51. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by DURGAPRASAD NEELAM; 19th April 2015 at 19:09.

  12. #11
    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: Need to show SQL data in tree view with lazy loading.

    Your canFetchRow() still makes no sense. Lines #17-18 say "If you are asking about top-level items, I always have more of them". The error you are getting is probably related rather to an invalid implementation of index() rather than fetch-more.
    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.


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

    Default Re: Need to show SQL data in tree view with lazy loading.

    Finally I could do lazy loading, but not as I expected.

    I am reading X amount of rows and building parent and child relationship. as we keep on scroll down I am getting remaining data by using canFetchMore() and fetchMore() functions.
    once I get the data I am appending this data to existing data by forming parent and child relationship.

    1. The 1st problem is, I am not able to delete the old data, If I tried to delete old data and keep only new data I am not able to see any scroll bars.
    2. the 2nd problem is when I have huge data, As the user keep on scroll down, scroll bar is becoming small. if we have a huge amount of data it becoming difficult for the user to really go to the end of the view and wait for the result to fetch and once results got fetched then come to know that there are some more records are available and still scroll down.

    for the second problem I tried to create vertical scroll bar and set a value for it and then set it to the view. but It did not workout.
    could you please suggest some solution for this 2 problems.




    Below is the code snippet, please suggest how to delete old data and maintain only current records in a memory.


    Qt Code:
    1. bool TreeModel::canFetchMore(const QModelIndex &parent ) const
    2. {
    3. if(m_totalRowCount > m_lastFetchedRowNumber)
    4. {
    5. return true;
    6. }
    7.  
    8. return false;
    9. }
    10.  
    11. void TreeModel::fetchMore(const QModelIndex& parent)
    12. {
    13. int readRows = 1000; //reading 1000 records/rows every time
    14.  
    15. if( m_totalRowCount < ( readRows + m_lastFetchedRowNumber ) ) //boundary condition
    16. {
    17. readRows = m_totalRowCount - m_lastFetchedRowNumber;
    18. }
    19.  
    20. qint64 l_lastInsertedRow = m_lastreadRowIndex;
    21.  
    22.  
    23.  
    24. //m_parent is my parent, I tried by doing this; delete m_parrent; m_parrent = createParentItem(); // but it did not workout.
    25.  
    26.  
    27. emit fetchMoreData(readRows); // will fetch readRows no.of rows, may vary depends on logic(next fetch would start from a row which Level is 0) I have written.
    28.  
    29. // I have written a logic so that every time I will get 0 as 1st row, so every time I insert data, parent must me QModelIndex()
    30.  
    31. beginInsertRows(QModelIndex(), l_lastInsertedRow, m_lastreadRowIndex-1);
    32. endInsertRows();
    33.  
    34. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance.

  14. #13
    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: Need to show SQL data in tree view with lazy loading.

    Quote Originally Posted by DURGAPRASAD NEELAM View Post
    1. The 1st problem is, I am not able to delete the old data, If I tried to delete old data and keep only new data I am not able to see any scroll bars.
    2. the 2nd problem is when I have huge data, As the user keep on scroll down, scroll bar is becoming small. if we have a huge amount of data it becoming difficult for the user to really go to the end of the view and wait for the result to fetch and once results got fetched then come to know that there are some more records are available and still scroll down.

    for the second problem I tried to create vertical scroll bar and set a value for it and then set it to the view. but It did not workout.
    could you please suggest some solution for this 2 problems.
    Your model needs to report its full size to the caller but it doesn't mean you have to actually have all the data at hand. As I said before you can use e.g. QContignousCache.
    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.


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

    Default Re: Need to show SQL data in tree view with lazy loading.

    Hi, Could you please help me out for this :

    http://www.qtcentre.org/threads/6232...281#post276281

Similar Threads

  1. table view and tree view
    By MKSPulok in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2012, 21:48
  2. Replies: 1
    Last Post: 3rd October 2011, 15:40
  3. How to map tree model data to list view
    By msopanen in forum Qt Programming
    Replies: 0
    Last Post: 10th November 2009, 19:56
  4. data, model and tree view
    By larry104 in forum Qt Programming
    Replies: 17
    Last Post: 3rd July 2006, 14:43
  5. Model, view, resize to show all data
    By jcr in forum Qt Programming
    Replies: 2
    Last Post: 17th April 2006, 20:59

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.