I just updated my windows machine to the latest Qt and PyQt versions and I've
been
having a problem with my custom subclasses of QGraphicsItems. When I
reimplement
the paint method it gets passed several option as a QStyleOptionGraphicsItem
object.
The exposedRect member of that is suppose to contain the area the needs to be
repainted, however I always get a null rectangle. Attached is a simple example
I
put together to illustrate the problem. The example attached is suppose to
draw a
3x3 rectangle under the mouse cursor when the mouse button is pressed down, but
since the exposedRect is always null it does nothing. It also prints out the
rectangle and I always get lines of all zeroes. I think this may be a bug, but
please tell me if I'm just doing something wrong or if something is messed up
with
my PyQt install.
Thanks,
Buddy
import sys
import PyQt4.QtGui as qtgui
import PyQt4.QtCore as qtcore
class CustomPixmapItem(qtgui.QGraphicsItem):
def __init__(self,parent=None,scene=None):
qtgui.QGraphicsPixmapItem.__init__(self,parent,scene)
self.image=qtgui.QImage(200,200,qtgui.QImage.Format_ARGB32_Premultiplied)
self.image.fill(0xffffffff)
def drawPoint(self,x,y):
painter=qtgui.QPainter()
painter.begin(self.image)
rect=qtcore.QRect(x,y,3,3)
painter.fillRect(rect,qtgui.QColor(0,0,0))
self.update(qtcore.QRectF(rect))
def paint(self,painter,options,widget=None):
rect=options.exposedRect
print "Exposed Rect:", rect.x(), rect.y(), rect.width(),
rect.height()
# this I think should work, but doesn't since the rectangle
never has anything in it
painter.drawImage(options.exposedRect,self.image,options.exposedRect)
# this alternate line works fine
#painter.drawImage(qtcore.QPoint(0,0),self.image)
class CustomScene(qtgui.QGraphicsScene):
def __init__(self,rect,parent=None):
qtgui.QGraphicsScene.__init__(self,rect,parent)
def mousePressEvent(self,event):
for item in self.items():
item.drawPoint(event.scenePos().x(),event.scenePos().y())
self.update()
app=qtgui.QApplication(sys.argv)
window=qtgui.QMainWindow()
window.show()
window.resize(240,240)
scene=CustomScene(qtcore.QRectF(0,0,200,200))
scene.setBackgroundBrush(qtgui.QBrush(qtgui.QColor(255,255,255)))
scene.update()
view=qtgui.QGraphicsView(scene,window)
view.show()
window.setCentralWidget(view)
item=CustomPixmapItem(None,None)
scene.addItem(item)
app.exec_()
_______________________________________________
PyQt mailing list PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt