[Qt-interest] Best way to update 10^5 items

Samuel Rødal sroedal at trolltech.com
Wed Sep 30 09:18:04 CEST 2009


Ole Streicher wrote:
> Hi,
> 
> I have a huge graphics scene that contains about 100.000 individual
> items (Ellipses, squares) whose colors are regularly updated.
> 
> My problem is now, that updating the colors takes quite a long time (10
> seconds on a quite fast machine). Is there a way to improve the speed or
> do I have to live with it? Here the (Python) code that takes so long
> (mydata is an array that holds the colors, myitems is the list that holds
> the items)
> -----------------------------8<---------------------------------
> #
> # This takes a long time.
> # 
> for gray, item in zip(mydata, myitems):
>     item.brush().color().setRgb(gray, gray, gray)
> 
> #
> # scene.update() is quite fast compared to the lines above.
> #
> scene.update()
> -----------------------------8<---------------------------------
> 
> Aside from having this written in Python (which is testable not the
> bottleneck), what is the reason why this is quite slow? It seems that
> the setRgb() is the cause here.
> 
> Or is QGraphicsScene not designed for so many objects? What else should
> one use instead?
> 
> Best regards
> 
> Ole

If mydata already contains all the color information, I suggest you 
subclass QGraphicsItem, implement paint(), and just read from mydata 
there to get the color you need. Then you can use the QPainter::fillRect 
overload that takes a color, instead of operating on brushes that change 
all the time.

Still, your code shouldn't be that slow... setRgb simply does some bit 
masking, so you should be able to do far more than 10^5 of those at a 
good speed. Maybe it's the zip operation that's expensive?

--
Samuel



More information about the Qt-interest-old mailing list