[PyQt] QPainter

David Boddie david at boddie.org.uk
Fri Jun 22 14:38:20 BST 2007


On Fri Jun 22 07:54:17 BST 2007, lucaberto wrote:

> Hello again here's my code but nothing is draw.
> 
>  def on_pushButton_5_clicked(self):
>         re = QtCore.QRect(69, 59, 781, 431)
>         QtGui.QPaintEvent.__init__(re)
>         event = QtGui.QPaintEvent(re)
>         self.paintEvent(event)
>
> def paintEvent(self, event):
>         gr = QtGui.QPainter(self.frame1)
>         gr.setPen(QtGui.QPen(QtCore.Qt.black, 1, QtCore.Qt.SolidLine))
>         gr.drawLine(70,60,740,60)
>         gr.drawLine(70,60, 70, 400)
>         gr.drawLine(70, 230,740, 230)
>         self.frame1.update()

Maybe I wasn't quite clear enough. :-/

You need to subclass QFrame and reimplement its paintEvent() method, not the
paintEvent() method of its parent widget. The subclass could look like this:

class PictureFrame(QFrame):

  def __init__(self, parent = None):
    QFrame.__init__(self, parent)
    self.picture = QPicture()

  def paintEvent(self, event):
    p = QPainter(self)
    p.drawPicture(0, 0, self.picture)

Now, instead of making frame1 a QFrame instance, you could use a PictureFrame
instance and, in your on_pushButton_5_clicked() method, do this instead:

  def on_pushButton_5_clicked(self):
    gr = QPainter(self.frame1.picture)
    gr.setPen(QtGui.QPen(QtCore.Qt.black, 1, QtCore.Qt.SolidLine))
    gr.drawLine(70,60,740,60)
    gr.drawLine(70,60, 70, 400)
    gr.drawLine(70, 230,740, 230)
    self.frame1.update()

David


More information about the PyQt mailing list