ben1996123

21st October 2013, 17:35

I have a lot of items in a QGraphicsScene (about 10000), and I need to update the position of every one of them, but when I do this it can take over 1 minute, so how can I (if possible) make it faster?

Here's the position update code:

void Dialog::updateAllPositions(){

int x,y,z,x2,y2;

for(int aa=0;aa<size.x()*size.y();aa++){

x=aa%size.x();

y=aa/size.x();

z=tiles[y][x];

if(z==0) continue;

x2=(z-1)%size.x();

y2=(z-1)/size.x();

scene->items()[size.x()*size.y()-1-z]->setPos(ssize*x+sgap*x-sgap+(ssize-fontMetric.width(QString::number(z)))/2, ssize*y+sgap*y-sgap+(ssize-fontMetric.height())/2);

scene->items()[2*size.x()*size.y()-2-z]->setPos((x-x2)*ssize+(x-x2)*sgap, (y-y2)*ssize+(y-y2)*sgap);

}

}

ssize and sgap are constants. Even if I disable viewport updating it still takes just as long. Any ideas?

Here's the position update code:

void Dialog::updateAllPositions(){

int x,y,z,x2,y2;

for(int aa=0;aa<size.x()*size.y();aa++){

x=aa%size.x();

y=aa/size.x();

z=tiles[y][x];

if(z==0) continue;

x2=(z-1)%size.x();

y2=(z-1)/size.x();

scene->items()[size.x()*size.y()-1-z]->setPos(ssize*x+sgap*x-sgap+(ssize-fontMetric.width(QString::number(z)))/2, ssize*y+sgap*y-sgap+(ssize-fontMetric.height())/2);

scene->items()[2*size.x()*size.y()-2-z]->setPos((x-x2)*ssize+(x-x2)*sgap, (y-y2)*ssize+(y-y2)*sgap);

}

}

ssize and sgap are constants. Even if I disable viewport updating it still takes just as long. Any ideas?