我目前正在努力用许多(> 100 万)对象(多边形、线、点)填充 qgraphicsscene。我观察到的是,当随机创建 100000 个多边形时,我已经消耗了 130MB 的内存。(下面的简单示例是基于使用chip-demo示例的View.cpp的默认Qt-Project)
QtGrafikTestBasic::QtGrafikTestBasic(QWidget *parent): QMainWindow(parent)
{
ui.setupUi(this);
QSplitter *h1Splitter = new QSplitter;
QSplitter *vSplitter = new QSplitter;
vSplitter->addWidget(h1Splitter);
View* view = new View("asdf");
h1Splitter->addWidget(view);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(vSplitter);
setLayout(layout);
setCentralWidget(view);
QBrush *brush = new QBrush();
brush->setColor(Qt::blue);
brush->setStyle(Qt::SolidPattern);
QPen *pen = new QPen();
pen->setWidth(0);
QGraphicsScene *scene = new QGraphicsScene;
srand ( time(NULL) );
int m_PolyWidth = 10;
for (int i = 0 ; i < 100000; i++)
{
double lBaseX = rand() % ((int)floor(width()) - m_PolyWidth);
double lBaseY = rand() % ((int)floor(height()) - m_PolyWidth);
QPolygonF polygon;
polygon << QPointF(lBaseX, lBaseY);
polygon << QPointF(lBaseX + m_PolyWidth, lBaseY);
polygon << QPointF(lBaseX + m_PolyWidth, lBaseY + m_PolyWidth);
polygon << QPointF(lBaseX, lBaseY + m_PolyWidth);
scene->addPolygon(polygon, *pen, *brush);
}
view->view()->setScene(scene);
}
那么我在这里做错了什么/我可以在哪里改进?我已经阅读了一些创建自己的类(如芯片示例)的帖子,所以我只是使用了芯片示例,但我也遇到了一个问题,即一旦我将均匀分布芯片的部分更改为item->setPos(QPointF(i, j));
随机分布item->setPos(QPointF(lBaseX, lBaseY));
,内存消耗也在这里爆炸......
那么,在 Qt 中绘制多边形、(多)线和点的性能最高且内存消耗最少的方法是什么?