0

I'm trying to find a way to mark the border of a QGraphicsScene, and make it resizable inside a QGraphicsView, to create something similar to Microsoft Paint.


In other words, my current QGraphicsView looks like this:

Current QGraphicsView

But my image is only this big, as indicated by the red box:

Image size indicated by red box.  The background doesn't count as part of the image.

I want my QGraphicsView to be like this (the little black boxes are cornergrabbers for resizing the canvas):

Final resizable QGraphicsView


Functionally, I want it to be similar to MS Paint: MS Paint example

The canvas (scene) is resizable, and the scrollbars on the window (view) appear when needed. The blue background color (solid gray background) appears behind the canvas.

How would I go about accomplishing this?


To try to get the grey background, I've been experimenting with QGraphicsView.setBackgroundBrush() and QGraphicsScene.setBackgroundBrush(). I've learned that QGraphicsView's background brush completely overrides QGraphicsScene's background brush if one is set. Even if I only set the background brush for QGraphicsScene, that background brush extends over the image's original boundaries.


Here is a link to my test code. Help is appreciated!

midrare
  • 2,371
  • 28
  • 48
  • Do you use setSceneRect at all ? – Alex Mar 01 '13 at 11:12
  • @Alex I've tried it, but it's not what I'm looking for. `QGraphicsView.setSceneRect()` limits the scrollable area for the scene, but I want to be able to zoom out to see the entire scene, and have a grey background where it extends over the scene boundaries. `QGraphicsScene.setSceneRect()` doesn't seem to affect anything. – midrare Mar 01 '13 at 21:26
  • SceneRect applied for scene does position your coordinate system inside view, but you can drag beyond its dimensions. With scale() at view you can zoom it easy. Do you tried to put rect item behind and use it as frame? Sorry if I've not understand your problem... If you post example code, it could be easier to copy - try and solve problem. – Alex Mar 02 '13 at 16:14
  • @Alex I'm not sure if I explained clearly enough.. I've updated my original post with test code and a more elaborate explanation. Please have a look! – midrare Mar 04 '13 at 02:55

1 Answers1

0

I have to struggle with your constructors...dunno if it works on Windows, but have to make it to work with Linux. Try :

def setPixmap(self, pixmap):
    if self.pixmap_item:
        self.removeItem(self.pixmap_item)

    self.pixmap_item = self.addPixmap(pixmap)

    self.setPixBackGround()

def setPixBackGround(self):
    # put Background rect for image
    pixR = self.pixmap_item.pixmap().rect()
    bgRectangle = self.addRect(pixR.x()-10, pixR.y()-10, 
                    pixR.width()+20, pixR.height()+20)
    # set color and Z value to put it behind image
    bgColor = QColor(58, 176, 176)
    bgRectangle.setBrush(bgColor)
    bgRectangle.setZValue(-.1)
    # take coordinates for brabbers
    bgR = bgRectangle.rect()
    grab1R = QRect(-5,-5,10,10)
    # put grabbers as wish...
    grab1 = self.addRect(grab1R)
    grab1.setPos(bgR.topLeft())

    grab2 = self.addRect(grab1R)
    grab2.setPos(bgR.topRight())
    # ....etc....
Alex
  • 3,167
  • 6
  • 35
  • 50
  • So that's what you meant by using a rect item. I didn't know you could do that, thanks! – midrare Mar 08 '13 at 01:34
  • Now we understand each other. Use that rectItem as reference for all manipulations with image. – Alex Mar 08 '13 at 09:55