Disable background scaling for a QGraphicsView/QGraphicsScene
Basically what I’m looking to do is replicate photoshop’s transparency pattern in a QGraphicsScene, but when I scale the scene, the background scales also. I subclassed QGraphicsView and set it’s background brush (as I understand it, it overrides the scene’s background brush) but that didn’t work either. I also tried overriding the drawBackground event in my subclassed QGraphicsView like so:
- painter->fillRect(rect, backgroundBrush());
but that still scaled the background. Does anyone know how I could keep the background image from scaling?
Examples -> Elastic Nodes Example -> graphicsview/elasticnodes/graphwidget.cpp
In the drawBackground() the examples shows how to paint directly in the QPainter.
So you can apply the scale factor as you want…
Thanks, but that doesn’t really help. I am already drawing directly to the QPainter of the QGraphicsView exactly like they are. Their background gradient also scales.
I see now that the QPainter has a scale, so all I needed to do was keep track of the scaling manually and inverse the scaling right before drawing the background and rescale right after, like so:
- QRectF sceneRect = this->sceneRect();
- QRectF bgrects = sceneRect;
- //scale the rectangle so that it actually covers the area it's supposed to
- bgrects.setBottomRight(bgrects.bottomRight() * scalefact);
- painter->scale(1/scalefact, 1/scalefact);
- painter->fillRect(bgrects, bgbr);
- painter->scale(scalefact, scalefact);