Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 7

Qt-interest Archive, March 2007
Paint events flooding a QGraphicsView


Message 1 in thread

I was playing with the 'diagramscene' example:

http://doc.trolltech.com/snapshot/graphicsview-diagramscene.html

 when I noticed my CPU was ramped up to 90%. It seems that if you
modify the item in some ways during a paint() method, another paint
event gets triggered. Here's a minimal example in Python (same thing
happens in C++ code):

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
class myLineItem(QGraphicsLineItem):
    def __init__(self,line):
        QGraphicsLineItem.__init__(self,line)
    def paint(self, painter, options, widget):
        print "Painting"
        QGraphicsLineItem.paint(self, painter,options,widget)
        self.setPen(QPen(Qt.red))
app=QApplication(sys.argv)
scene=QGraphicsScene()
l=myLineItem(QLineF(0,0,20,20))
scene.addItem(l)
view= QGraphicsView(scene)
view.show();
app.exec_()

 If you run this it will start spitting out 'Painting' a zillion
times. Take the 'setPen' call out of the paint() method and it
doesn't.

 I can see the logic - if you change the pen of an object you might
want to repaint it - but surely you dont want to queue another paint()
event on the same object from within the paint() event?

 Is this a bug or am I (and the Qt dude who wrote the diagramscene
example) doing it wrong?

Barry

--
 [ signature omitted ] 

Message 2 in thread

Hi,

> I was playing with the 'diagramscene' example:
> 
> http://doc.trolltech.com/snapshot/graphicsview-diagramscene.html

The unmodified example, or a modified version of it?

> when I noticed my CPU was ramped up to 90%. It seems that if you
> modify the item in some ways during a paint() method, another paint
> event gets triggered. Here's a minimal example in Python (same thing
> happens in C++ code):

I don't have PyQt installed right now so I can't test this example.

> [...]
> Is this a bug or am I (and the Qt dude who wrote the diagramscene
> example) doing it wrong?

Which part of the example source code are you referring to? I don't see 
paint() being called from within paint() in this example, but I may have 
missed something.

--
 [ signature omitted ] 

Message 3 in thread

Barry Rowlingson wrote:
> I was playing with the 'diagramscene' example:
> 
> http://doc.trolltech.com/snapshot/graphicsview-diagramscene.html
> 
> when I noticed my CPU was ramped up to 90%. It seems that if you
> modify the item in some ways during a paint() method, another paint
> event gets triggered. Here's a minimal example in Python (same thing
> happens in C++ code):

Why are you modifying the attributes of the object during a paint call? 
Certainly changing an object's state will trigger an update event and 
therefore another paint? Try moving the attribute change outside of the 
paint function, to initialization or event handling code.


-- 
 [ signature omitted ] 

Message 4 in thread

On 3/29/07, Barry Rowlingson <barry.rowlingson@xxxxxxxxx> wrote:
>
>  I suspect the logic leading to the setLine() in Arrow::paint could be
> (should be?) moved into Arrow::updatePosition.

 I've just completed this exercise and it seems to work okay. In fact
what I've actually done is to convert the whole example into Python
using PyQt4 (which has taken me about 3 evenings' work). It mostly
works, but shifting the setLine() call into the updatePosition method
stops the paint flood.

Thanks all

Barry

--
 [ signature omitted ]