PDA

View Full Version : Question about drawing background images in a QGraphicsScene and QGraphicsView



di_zou
8th December 2010, 16:16
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?

I took a look at QGraphicsScene::drawBackground() which requires a QPainter as a parameter. However, I am not sure of how to tell the QPainter which image to use.

di_zou
8th December 2010, 19:35
Figured this out. Just me being stupid...

karthic
28th April 2012, 09:58
How to do this.?

Added after 11 minutes:

Finally found it.