QTreeView内存消耗
我现在正在测试 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();
}
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我在示例源中将 QTreeView 替换为 QTableView,则不会消耗内存。所以看来QListView和QTreeView不适合用于大量数据,而必须使用QTableView。
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.