PDA

View Full Version : Scene.DrawBackgrount: want to know dimensions of view window



lightning2911
20th May 2010, 11:41
I have a MyScene derived from QGraphicsScene where I implement a drawBackground. I now paint the sceneRect() with a grid. But I would like to extend painting the grid to the outside area if the sceneRect is smaller than the view window. How do I get these dimensions?

tbscope
20th May 2010, 11:48
But I would like to extend painting the grid to the outside area if the sceneRect is smaller than the view window.
I read that 10 times, but I still do not get it.
Can you explain this a little bit better?

What is an outside area?
What is a sceneRect?
What is a view window?

aamer4yu
20th May 2010, 12:44
Better use QGraphicsView::drawBackground instead of the scene background

lightning2911
20th May 2010, 12:47
I read that 10 times, but I still do not get it.
Can you explain this a little bit better?
I will try with an image or two:

http://www.lukaslang.com/fileadmin/user_upload/gridscene1.jpg

This is a scene where the sceneRect() is painted gray and dots are painted on grid position. The sceneRect is centered in the window.

How do I know the dimensions of the white space around so that I can fill this space also with grid dots (or whatever)? I want to achieve this:

http://www.lukaslang.com/fileadmin/user_upload/gridscene2.jpg
For this screen i have just added a fixed amount to the screenRect but I would like to know what the dimensions of that space around the sceneRect really are.

This is what the code looks like.


class MyScene(QGraphicsScene):

def __init__(self, parent=None):
super(MyScene, self).__init__(parent)
self.pen = QPen(Qt.black, 1, Qt.SolidLine)
self.brush = QBrush(Qt.gray, Qt.SolidPattern)
self.gridSize = SCREEN_GRID_SIZE
self.gridX = SCREEN_GRID_X
self.gridY = SCREEN_GRID_Y
self.setSceneRect(- self.gridSize * self.gridX / 2, - self.gridSize * self.gridY / 2, self.gridSize * self.gridX, self.gridSize * self.gridY)

def drawBackground(self, painter, rect):
# draw a rect in size of sceneRect

# draw frame
painter.setPen(QPen(Qt.red, 0, Qt.NoPen))
painter.setBrush(QBrush(Qt.lightGray, Qt.SolidPattern))
painter.drawRect(self.sceneRect())

# draw grid
painter.setBrush(QBrush(Qt.darkGray, Qt.SolidPattern))
print self.sceneRect()
for y in range(int(self.sceneRect().y()), int(self.sceneRect().y())+int(self.sceneRect().hei ght()+1), self.gridSize):
for x in range(int(self.sceneRect().x()), int(self.sceneRect().x())+int(self.sceneRect().wid th()+1), self.gridSize):
# change color for center point
if x == 0 and y == 0:
painter.save()
painter.setBrush(QBrush(Qt.red, Qt.SolidPattern))
# draw a point
painter.drawRect(x-1, y-1, 2, 2)
# change color back if center point
if x == 0 and y == 0:
painter.restore()

lightning2911
20th May 2010, 13:10
thanks aamer4yu, i did not see that the view also had a drawbackground method. got it working now.