[PyQt] Re: how to show interactive Picture

David Boddie david at boddie.org.uk
Tue Feb 10 22:57:54 GMT 2009

On Tue Feb 10 18:15:52 GMT 2009, Markus Feldmann wrote:

> To compare i post the example.
> There are some commands in the example i didn't understand.
> I do some notes in the Code !!!
> import sys
> from PyQt4 import QtCore, QtGui
> import basicdrawing_rc

Note that you need to have this module to be able to show the image used
by the example. It is created by running pyrcc4 in the basicdrawing
directory like this:

  pyrcc4 -o basicdrawing_rc.py basicdrawing.qrc

This causes all the resources declared in the basicdrawing.qrc file (it's
just XML) to be written to a Python source file that can be imported.

> class RenderArea(QtGui.QWidget):
>      Line, Points, Polyline, Polygon, Rect, RoundRect, Ellipse, Arc, \
>      Chord, Pie, Path, Text, Pixmap = range(13) ###WHAT SHALL THAT ???###

Someone just wanted to define some constants. It's quicker to use Python's
sequence unpacking feature with a range() call instead of doing this:

  Line = 0
  Points = 1
  Polyline = 2

and so on.

>      def __init__(self, parent = None):
>          QtGui.QWidget.__init__(self, parent)
>          self.shape = RenderArea.Pixmap ###IS IT CALLING HIMSELF ???###

No, it's just referencing the Pixmap constant (with a value of 12).

>          self.pen = QtGui.QPen()
>          self.brush = QtGui.QBrush()
>          self.pixmap = QtGui.QPixmap()
>          self.pixmap.load(":/images/qt-logo.png")

Here's what might be causing the image not to appear. If, for some reason,
the basicdrawing_rc.py file was created without the image data, this will
fail, leaving you with a null pixmap.

>      def minimumSizeHint(self):
>          return QtCore.QSize(100, 100)
>      def sizeHint(self):
>          return QtCore.QSize(400, 200)

Defining these two methods are important if you want your widget to have
minimum and preferred sizes. If you put a widget into a QScrollArea and it
doesn't have these methods, it might not appear at all.

>      def setPen(self, pen):
>          self.pen = pen
>          self.update()
>      def setBrush(self, brush):
>          self.brush = brush
>          self.update()
>      def paintEvent(self, event):
>          painter = QtGui.QPainter()
>          painter.begin(self)
>          painter.setPen(self.pen)
>          painter.setBrush(self.brush)
>          painter.save()
>          painter.drawPixmap(10, 10, self.pixmap)
>          painter.restore()
>          painter.end()

Here we draw the pixmap. If everything was set up correctly, this should
just work.

Does this make things any clearer?


More information about the PyQt mailing list