Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QGraphicsScene, Item Coordinates Affect Performance?

With the below code snippet I create a scene with 100.000 rectangles.
The performance is fine; the view responds with no delays.

QGraphicsScene * scene = new QGraphicsScene;
for (int y = -50000; y < 50000; y++) {
   scene->addRect(0, y * 25, 40, 20);
}
...
view->setScene(scene);

And now the 2nd snippet sucks

for (int y = 0; y < 100000; y++) {
   scene->addRect(0, y * 25, 40, 20);
}

For the 1st half of scene elements the view delays to respond on mouse and key events, and for the other half it seems to be ok ?!?

The former scene has sceneRect (x, y, w, h) = (0, -1250000, 40, 2499995).
The latter scene has sceneRect (x, y, w, h) = (0, 0, 40, 2499995).

I don't know why the sceneRect affects the performance, since the BSP index is based on relative item coordinates.

Am I missing something? I didn't find any information on the documentation, plus the Qt demo 40000 Chips also distributes the elements around (0, 0), without explaining the reason for that choice.

 // Populate scene
 int xx = 0;
 int nitems = 0;
 for (int i = -11000; i < 11000; i += 110) {
     ++xx;
     int yy = 0;
     for (int j = -7000; j < 7000; j += 70) {
         ++yy;
         qreal x = (i + 11000) / 22000.0;
         qreal y = (j + 7000) / 14000.0;
         ...
like image 692
Nick Dandoulakis Avatar asked May 28 '11 21:05

Nick Dandoulakis


1 Answers

I have a solution for you, but promise to not ask me why is this working, because I really don't know :-)

QGraphicsScene * scene = new QGraphicsScene;
// Define a fake symetrical scene-rectangle
scene->setSceneRect(0, -(25*100000+20), 40, 2 * (25*100000+20) );

for (int y = 0; y < 100000; y++) {
    scene->addRect(0, y * 25, 40, 20);
}
view->setScene(scene);
// Tell the view to display only the actual scene-objects area
view->setSceneRect(0, 0, 40, 25*100000+20);
like image 187
Fivos Vilanakis Avatar answered Oct 05 '22 21:10

Fivos Vilanakis