7

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? :)

#include <QtGui/QApplication>
#include <QTreeView>
#include <QAbstractItemModel>

class CModel : public QAbstractItemModel
{
  public: QModelIndex index
  (
    int i_nRow,
    int i_nCol,
    const QModelIndex& i_oParent = QModelIndex()
  ) const
  {
    return createIndex( i_nRow, i_nCol, 0 );
  }

  public: QModelIndex parent
  (
    const QModelIndex& i_oInex
  ) const
  {
    return QModelIndex();
  }

  public: int rowCount
  (
    const QModelIndex& i_oParent = QModelIndex()
  ) const
  {
    return i_oParent.isValid() ? 0 : 1000 * 1000 * 10;
  }

  public: int columnCount
  (
    const QModelIndex& i_oParent = QModelIndex()
  ) const
  {
    return 1;
  }

  public: QVariant data
  (
    const QModelIndex& i_oIndex,
    int i_nRole = Qt::DisplayRole
  ) const
  {
    return Qt::DisplayRole == i_nRole ? QVariant( "1" ) : QVariant();
  }
};

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  QTreeView oWnd;
  CModel oModel;
  oWnd.setUniformRowHeights( true );
  oWnd.setModel( & oModel );
  oWnd.show();
  return a.exec();
}
grigoryvp
  • 40,413
  • 64
  • 174
  • 277
  • 5
    that has to be one of the weirdest code styling I've seen :) – Idan K May 18 '10 at 12:13
  • 1
    @Idan K: The style is called "readable" :-) Most programming styles are called "cram-as-much-on-my-tiny-screen-as-possible" and date from an era where monitors were 80x25 (characters, not pixels). – Aaron Digulla May 18 '10 at 12:18
  • 3
    @aaron: well, i wouldn't go as far as that, it's just the first time i see `public:` infront of every public method, and also the way parameters are styled, but i wasn't criticizing, just saying i haven't seen it before. :) – Idan K May 18 '10 at 12:40
  • Normally i reduce styling to something more common - with dence lines and no notation. But for such code sample this will take a notable amount of time :(. My apologies for readability. – grigoryvp May 18 '10 at 12:41
  • "public:" in front of every method is from C#/Java. It increase readability a bit if you search a huge project via IDE 'go to definition'. – grigoryvp May 18 '10 at 12:55

1 Answers1

3

If i replace QTreeView with QTableView in sample source, memory will not be consumed. So it seems that QListView and QTreeView are not intended to be used with very big amount of data and QTableView must be used instead.

grigoryvp
  • 40,413
  • 64
  • 174
  • 277