[PyQt] Question about drawing background images in a QGraphicsScene and QGraphicsView

Hans-Peter Jansen hpj at urpla.net
Wed Dec 8 17:35:18 GMT 2010

On Wednesday 08 December 2010, 16:26:01 dizou wrote:
> I have a QGraphicsScene and a QGraphicsView. I am currently using the
> QGraphicsView to draw a backgorund image. I have reimplemented the
> QGraphicsView::drawBackground() function like this:
> def drawBackground(self, painter, rect):
>         sceneRect = self.sceneRect()
>         if self.background:
>             rectf = QRectF(self.background.rect())
>             painter.drawPixmap(sceneRect, self.background, rectf)
>         else:
>             painter.fillRect(rect.intersect(sceneRect),
> QBrush(Qt.lightGray))
>             painter.setBrush(Qt.NoBrush)
>             painter.drawRect(sceneRect)
> self.background is a QPixmap:
> self.background = QPixmap(':/some_image.png')
> The problem with this is that if the PNG is not in my .qrc file, the
> background image does not show up. I would like to make it so that I
> can use any image I want without having to put it into the .qrc. How
> would I do this?

Why don't you check a physical path beforehand, and only use the 
resource png, if none physically exist?


More information about the PyQt mailing list