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

Ole Streicher ole-usenet-spam at gmx.net
Wed Sep 30 16:45:09 CEST 2009


Hello,

Samuel Rødal <sroedal at trolltech.com> writes:
> 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?

I did some stop-watch :-) profiling:

for gray, item in zip(mydata, myitems):
     item.brush().color().setRgb(gray, gray, gray)

takes about a second for 10^5 items (including Python->C++).

scene.update() takes about 5 seconds for 10^5 QGraphicsRectItem to
render (Intel(R) Core(TM)2 Duo CPU P8600@ 2.40GHz, Intel Corporation
Mobile 4 Series Chipset Integrated Graphics Controller, Qt 4.5.0,
kubuntu 9.04).

The same 5 seconds (sometimes a bit more) are needed if I change the
scaling (and therefore need to redraw it).

Do these numbers sound reasonable? 

What I also tried was to write my own QGraphicsItem:

class RectSpaxel(QtGui.QGraphicsItem):
    def __init__(self, pos, size, color_array, index):
        QtGui.QGraphicsItem.__init__(self)
        self.rect = QtCore.QRectF(pos[0] - size/2, pos[1] - size/2, size, size)
        self.color_array = color_array
        self.index = index

    def boundingRect(self):
        return self.rect

    def paint(self, painter, option, widget):
        c = self.color_array[self.index]
        painter.fillRect(self.rect, QtGui.QColor(c, c, c))

But since this needs at least one C++->Python call and one
Python->C++-Call for every item, it is even slower on update(): 12
seconds, so this does not help.

Best regards

Ole




More information about the Qt-interest-old mailing list