PDA

View Full Version : QGraphicsItem.setPos takes a lot of time



ricofe25
8th December 2009, 16:25
Hi
I read in documentation to the Graphics View Framework that visualization of even millions of items performs real-time. To check it I put a little experiment. I made just 50000 items of QGraphicsEllipseItem(qrand() % 500, qrand() % 500, 4, 4) and tried to move it chaotically every half of a second. QGLWidget was selected as viewport. Code of the move() slot to the timer's timeout() signal looked like this:


void Visview::move()
{
for (int i = 0; i < 50000; i++)
{
elems[i]->setPos(elems[i]->x() + (qrand() % 20) - 10,
elems[i]->y() + (qrand() % 20) - 10);
}
}

For timer timeout=500 ms it worked extremely slow: about 20 secs to one step of movement. I achieved a big performance improvement by adding


scene.update(-500, -500, 1000, 1000);

to the end of the function. But speed of working is still unsatisfactory to me. About 1.5-2 secs to step, and about 8 secs to first step of movement. I found that the loop with new positions setting


for (int i = 0; i < 50000; i++)
{
elems[i]->setPos(elems[i]->x() + (qrand() % 20) - 10,
elems[i]->y() + (qrand() % 20) - 10);
}

takes about 600 ms to step. Can I make this loop faster somehow?

PS. I learned source code of the setPos and found it call update function of the scene. But if internal flag updateAll is set to true update() does nothing. To set updateAll to true it's enough to call scene.update(QRect()). I tried to do so before loop, but it took no effect.

My Qt version is 4.6.0.

Thank you.

wysota
8th December 2009, 17:14
There are many things you can do. For starters set the viewport update mode to FullViewportUpdate. If you're using OpenGL this should already be the case but then passing scene.update() with some parameters shouldn't make any difference. In general when moving all the items it is wise to disable indexing, enabling cache and doing some other things but it depends on a case-by-case basis.

ricofe25
8th December 2009, 19:28
Thank you. The FullViewportUpdate mode and indexing disabling have given valuable performance improvements.:)