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