Results 1 to 7 of 7

Thread: QTreeView memory consumption

  1. #1
    Join Date
    Oct 2009
    Posts
    36
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QTreeView memory consumption

    Hello.

    I'm testing QTreeView functionality right now, and i was amazed by one thing. It seems that QTreeView memory consumption depends on items count O_O. This is highly unusual, since model-view containers of such type only keeps track for items being displayed, and rest of items are in the model. I have written a following code with a simple model that holds no data and just reports that it has 10 millions items. With MFC, Windows API or .NET tree / list with such model will take no memory, since it will display only 10-20 visible elements and will request model for more upon scrolling / expanding items. But with Qt, such simple model results in ~300Mb memory consumtion. Increasing number of items will increase memory consumption. Maybe anyone can hint me what i'm doing wrong?

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QTreeView>
    3. #include <QAbstractItemModel>
    4.  
    5. class CModel : public QAbstractItemModel
    6. {
    7. public: QModelIndex index
    8. (
    9. int i_nRow,
    10. int i_nCol,
    11. const QModelIndex& i_oParent = QModelIndex()
    12. ) const
    13. {
    14. return createIndex( i_nRow, i_nCol, 0 );
    15. }
    16.  
    17. public: QModelIndex parent
    18. (
    19. const QModelIndex& i_oInex
    20. ) const
    21. {
    22. return QModelIndex();
    23. }
    24.  
    25. public: int rowCount
    26. (
    27. const QModelIndex& i_oParent = QModelIndex()
    28. ) const
    29. {
    30. return i_oParent.isValid() ? 0 : 1000 * 1000 * 10;
    31. }
    32.  
    33. public: int columnCount
    34. (
    35. const QModelIndex& i_oParent = QModelIndex()
    36. ) const
    37. {
    38. return 1;
    39. }
    40.  
    41. public: QVariant data
    42. (
    43. const QModelIndex& i_oIndex,
    44. int i_nRole = Qt::DisplayRole
    45. ) const
    46. {
    47. return Qt::DisplayRole == i_nRole ? QVariant( "1" ) : QVariant();
    48. }
    49. };
    50.  
    51. int main(int argc, char *argv[])
    52. {
    53. QApplication a(argc, argv);
    54. QTreeView oWnd;
    55. CModel oModel;
    56. oWnd.setUniformRowHeights( true );
    57. oWnd.setModel( & oModel );
    58. oWnd.show();
    59. return a.exec();
    60. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2009
    Posts
    36
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTreeView memory consumption

    Bump. Still can't find out how to correctly use QAbstractItemModel + QTreeView in order to prevent gigabytes of memory consumption. Will appreciate any help.

  3. #3
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QTreeView memory consumption

    Can you explain how you measured the memory consumption?
    Did you try the QStandardItemModel too?

  4. #4
    Join Date
    Oct 2009
    Posts
    36
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTreeView memory consumption

    Memory consumption is measured via task manager . Run app without connected model = 20mb, run app with connected model = 320mb O_O.
    As far as i understand, using QSTandardItemModel will effectively reduce QTreeView to QTreeWidget - i will not be able to supply data dynamically as needed. For SQL queries that can be millions of records this will be a bit slow .

  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QTreeView memory consumption

    Memory measurement via the task manager is like asking a baby the yield of a certain nuclear bomb :-)
    It doesn't tell you the real information you want.

    Nevertheless I believe you when your program starts using more and more memory. I also enjoyed using the list and tree widgets of windows.

    Your understanding that using QStandardItemModel will reduce QTreeView to QTreeWidget isn't correct. QTreeWidget manages the items itself. Using a QTreeView together with QStandardItemModel changes nothing. It's a standard view and a standard model.

    For SQL queries I suggest you look into one of the QSql...Model classes. Try it first with one of these and see if you still get big memory consumptions.

    For a personal project I need to display thousands of items from a database in a treeview, and the memory consumption is minimal.

  6. The following user says thank you to tbscope for this useful post:

    eyeofhell (25th May 2010)

  7. #6
    Join Date
    Oct 2009
    Posts
    36
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTreeView memory consumption

    It seems that if i replace QTreeView with QTableView in my example code memory is not consumed anymore O_O. So it seems that QTreeView is just not intended to be used for very huge amounts of data and QTableView must be used instead.

  8. #7
    Join Date
    Nov 2010
    Posts
    4
    Qt products
    Qt4

    Default Re: QTreeView memory consumption

    Quote Originally Posted by tbscope View Post
    For a personal project I need to display thousands of items from a database in a treeview, and the memory consumption is minimal.
    Then maybe you could help me with some ideas.

    I also have a big QTreeView and its model gets the display data from an sqlite database. So when the tree is drawed 2 queries are executed per row to get the data. This gives the view an annoying delay (for scrolling, expanding a branch etc.). Also when I have the database in the memory. If I use any styling on it that doubles the delay.

    Didn't you have this problem? Should I initialize the model by reading everything from the database and building a hierarchical structure to the memory? Any other idea to improve performance?

Similar Threads

  1. QSqlQueryModel and memory consumption
    By lunatic fringe in forum Qt Programming
    Replies: 3
    Last Post: 5th February 2010, 11:09
  2. Replies: 0
    Last Post: 17th November 2009, 21:19
  3. QTableView memory consumption
    By LordQt in forum Qt Programming
    Replies: 7
    Last Post: 9th December 2008, 16:51
  4. Pixmap memory consumption
    By bunjee in forum Qt Programming
    Replies: 9
    Last Post: 29th November 2007, 15:35
  5. Tracking memory consumption of plugins.
    By spud in forum General Programming
    Replies: 3
    Last Post: 7th September 2007, 13:14

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.