7

私は今、QTreeView の機能をテストしていますが、1 つのことに驚きました。QTreeView のメモリ消費量は、アイテム数 O_O に依存しているようです。このようなタイプのモデル ビュー コンテナーは、表示されているアイテムのみを追跡し、残りのアイテムはモデル内にあるため、これは非常に珍しいことです。データを保持せず、1,000 万のアイテムがあることを報告するだけの単純なモデルを使用して、次のコードを作成しました。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();
}
4

1 に答える 1

3

サンプルソースでQTreeViewをQTableViewに置き換えると、メモリは消費されません。したがって、QListViewとQTreeViewは、非常に大量のデータで使用することを意図していないようであり、代わりにQTableViewを使用する必要があります。

于 2010-05-25T06:55:01.567 に答える