[PyQt] QGraphicsScene/Item/View optimisation

Brian Kelley kelley at eyesopen.com
Fri Feb 6 13:11:41 GMT 2009


I have had great success with QtGui.QGraphicsItemAnimation alleviating the same issues that you are looking at.

It looks like that your PictureItem is returning a really large bounding box so that caching system is repainting all or most of them at every step.  Sometime it helps in debugging to just draw the bounding rect in red or some other obvious color for each item.

I added a debugging code to swap the color of picture items when they are repainted:

    def paint(self, painter, options, widget):
        self.p += 1
        x, y, w, h = self._computeRect()
        if self.p % 2 == 0:
            color = "blue"
        else:
            color = "white"
        painter.fillRect(x, y, w, h, QtGui.QColor(color))
       ...

My other suggestion (that doesn't really help in this case) is not using a thread.  You can always fake your thread with timers, in general threads are evil and difficult to debug.  Since python has a global interpreter lock, timers are just as fast (unless you call a long running bit of non-python code)

class FakeThread(QtCore.QObject):
    def __init__(self, scene):
        QtCore.QObject.__init__(self)
        self.scene = scene
        self.__x = 0
        self.__y = 0
        self.count = 0
        self.timer = QtCore.QTimer(self)
        self.timer.setInterval(100)
        self.connect(self.timer, QtCore.SIGNAL("timeout()"), self.timeout)
        self.timer.start()

    def timeout(self):
        self.scene.setHeadPosition(self.__x, -self.__y)
        self.__x += 3.6
        self.__y += 1.8
        self.count += 1
        if self.count > 10000:
            QtCore.quit()


On 2/6/09 7:34 AM, "Frédéric" <frederic.mantegazza at gbiloba.org> wrote:

Le 5/2/2009, "Grissiom" <chaos.proton at gmail.com> a écrit:

> I think you may try some CacheFlag or OptimizationFlag in QGraphicsView, see:
>
>http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qgraphicsview.html#CacheModeFlag-enum
>http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qgraphicsview.html#OptimizationFlag-enum

I tried these flags, but they didnt change anything :o(

I attached a short example to show my problem. As soon as the crosshair
starts to move, the CPU goes to 100%!

Any idea to improve that code?

--
   Frédéric

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20090206/3631b388/attachment.html


More information about the PyQt mailing list