QGraphicsItem.setPos takes a lot of time
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:
Code:
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
Code:
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
Code:
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.
Re: QGraphicsItem.setPos takes a lot of time
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.
Re: QGraphicsItem.setPos takes a lot of time
Thank you. The FullViewportUpdate mode and indexing disabling have given valuable performance improvements.:)