QPicture and Qpainter PyQt4

BlueBird phil at freehackers.org
Mon Sep 17 04:41:59 EDT 2007


Hi,

It looks like you have several things wrong:

On Sep 17, 9:29 am, luca72 <lucabe... at libero.it> wrote:
> class Form(QWidget, Ui_Form):
>     [...]
>
>     @pyqtSignature("")
>     def on_pushButton_clicked(self):
>         """
>         Slot documentation goes here.
>         """
>         # TODO: not implemented yet
>         #raise "Not implemented yet"
>         #gr = QtGui.QPainter()
>         picture = QtGui.QPicture()
>         gr = QtGui.QPainter()
>         gr.begin(picture)
>         gr.setPen(QtGui.QPen(QtCore.Qt.black, 1, QtCore.Qt.SolidLine))
>         gr.drawLine(70,100,150,100)
>         gr.end()
>         picture.save('dis.pic')
>         self.frame.update()

Normally, you don't paint inside a slot. You paint inside the
paintEvent() method which is called automatically by Qt when an area
of your widget needs to be repainted.

> from PyQt4.QtGui import QWidget
> from PyQt4.QtCore import pyqtSignature
> from PyQt4 import *
>
> class PictureFrame(QtGui.QFrame):
>
>   def __init__(self, parent = None):
>     QFrame.__init__(self, parent)
>     picture = QtGui.QPicture()
>
>   def paintEvent(self, event):
>     picture.load('dis.pic')
>     gr = QtGui.QPainter()
>     gr.begin(self.frame)
>     gr.drawPicture(0, 0, picture)
>     gr.end()

Don't load a picture inside a paintevent. Loading is an expensive
process, you don't want to repeat it all the time.

Second, you should check that your picture was loaded. Sometimes, it's
a file problem. Check the size of your picture for example.

Third, I think you should have :
    gr.begin( self )

Else, the painter is not painting on your widget.

Hope that helps.

    PHilippe






More information about the Python-list mailing list