问题
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();
}
回答1:
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.
来源:https://stackoverflow.com/questions/2857008/qtreeview-memory-consumption