• 2702阅读
  • 3回复

QTableView 内存占用过大? [复制链接]

上一主题 下一主题
离线kaon
 

只看楼主 倒序阅读 楼主  发表于: 2020-08-16
  1. #ifndef TESTMODEL_H
  2. #define TESTMODEL_H
  3. #include <QAbstractTableModel>
  4. class TestModel : public QAbstractTableModel
  5. {
  6.     Q_OBJECT
  7. public:
  8.     explicit TestModel(QObject *parent = nullptr);
  9.     virtual int rowCount(const QModelIndex & parent) const;
  10.     virtual int columnCount(const QModelIndex &parent) const;
  11.     virtual QVariant data(const QModelIndex &index, int role) const;
  12.     virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;
  13.     bool addData(int i);
  14. private:
  15.     QVector<int> m_list;
  16. };
  17. #endif // TESTMODEL_H

  1. #include "testmodel.h"
  2. TestModel::TestModel(QObject *parent)
  3. {
  4. }
  5. int TestModel::rowCount(const QModelIndex &parent) const
  6. {
  7.     return m_list.size();
  8. }
  9. int TestModel::columnCount(const QModelIndex &parent) const
  10. {
  11.     return 100;
  12. }
  13. QVariant TestModel::data(const QModelIndex &index, int role) const
  14. {
  15.     return QVariant();
  16. }
  17. QVariant TestModel::headerData(int section, Qt::Orientation orientation, int role) const
  18. {
  19.     if (orientation == Qt::Vertical) return QString::number(section);
  20.     if (role != Qt::DisplayRole) return QVariant();
  21.     return QString::number(section);
  22. }
  23. bool TestModel::addData(int i)
  24. {
  25.     m_list.push_back(i);
  26.     return true;
  27. }

  1. #include <QApplication>
  2. #include "testmodel.h"
  3. #include <QTableView>
  4. int main(int argc, char *argv[])
  5. {
  6.     QApplication a(argc, argv);
  7.     TestModel model;
  8.     for(int i = 0; i < 100000; ++i)
  9.     {
  10.         model.addData(i);
  11.     }
  12.     QTableView view;
  13.     view.setModel(&model);
  14.     view.show();
  15.     return a.exec();
  16. }

10w条数据,100个column,不仅加载慢,而且内存消耗巨大,> 5000MB
不知道哪里写的问题。。。
离线kaon

只看该作者 1楼 发表于: 2020-08-16
5.9.9没有这个问题,似乎5.15就不行,
https://forum.qt.io/topic/118086/slow-performance-and-large-memory-usage-with-qtableview-in-qt-5-15
这边回答是win7 mingw64 5.15可以,linux可以, 其他尚未求证
离线kenbe

只看该作者 2楼 发表于: 2020-08-17
我在qt5.9.8下面6w条数据,8列,用了100多M的内存。
离线kaon

只看该作者 3楼 发表于: 2020-08-19
回 kenbe 的帖子
kenbe:我在qt5.9.8下面6w条数据,8列,用了100多M的内存。 (2020-08-17 13:43) 

20w条数据,100列,数据都是空的,占用也就10-20M
快速回复
限100 字节
 
上一个 下一个