Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QTreeView memory consumption

Tags:

qt

qtreeview

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();
}
like image 653
grigoryvp Avatar asked May 18 '10 12:05

grigoryvp


1 Answers

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.

like image 194
grigoryvp Avatar answered Sep 16 '22 21:09

grigoryvp