View Full Version : How to improve memory-consumption when drawing polygons, (poly-)lines and points

12th November 2013, 10:46
I'm currently struggling with populating a qgraphicsscene with many (> 1Million) objects (Polygons, Lines, Points). What I've observed is, that when creating randomly 100000 Polygons, I'm already ending up withe 130MB memory consumption. (The simple example below is based on a default Qt-Project using the View.cpp of the chip-demo example)

QtGrafikTestBasic::QtGrafikTestBasic(QWidget *parent): QMainWindow(parent)
QSplitter *h1Splitter = new QSplitter;
QSplitter *vSplitter = new QSplitter;

View* view = new View("asdf");

QHBoxLayout *layout = new QHBoxLayout;

QBrush *brush = new QBrush();
QPen *pen = new QPen();

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);

So what am I doint wrong here / where can I improve? I've read some posts of creating an own class like the chip-example, so I simply used the chip example but also there I've encountered the problem that as soon as I change the part which uniformly distributes the chips from
item->setPos(QPointF(i, j)); to a random distribution
item->setPos(QPointF(lBaseX, lBaseY));, the memory consumption explodes also here...

So, what is the most performant and least memory-consuming way of drawing polygons, (poly-)lines and points in Qt?

12th November 2013, 12:16
I guess the most memory efficient way is to draw yourself using the QPainter API.
This way you can work with the data you already have and don't need to copy it.

Alternatively create your own graphics item that does not require a copy of the data but has access to your already existing data.
That will still require at least one pointer for the item, one to point to the global data and some index into the data.