我现在正在测试QTreeView的功能,我对一件事感到惊讶。QTreeView内存消耗似乎依赖于项目计数O_O。这是非常不寻常的,因为这种类型的模型视图容器只跟踪正在显示的项目,而其余的项目都在模型中。我用一个简单的模型编写了以下代码,该模型不包含任何数据,只报告它有1000万个项目。有了MFC,Windows API或带有这种模型的.NET树/列表将不会占用内存,因为它将只显示10-20个可见元素,并且在滚动/展开项目时将请求模型提供更多元素。但是对于Qt,这种简单的模型会导致大约300Mb的内存消耗。增加项目的数量会增加内存消耗。也许有人能提示我哪里做错了?:)
#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();
}发布于 2010-05-25 14:55:02
如果我在示例源码中用QTableView替换QTreeView,则不会消耗内存。因此,似乎QListView和QTreeView并不是用来处理大量数据的,而必须使用QTableView。
https://stackoverflow.com/questions/2857008
复制相似问题