March 11, 2012

ProfessorFang ProfessorFang
Lab Rat
3 posts

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:

  1. void FontPageView::drawBackground(QPainter *painter, const QRectF &rect)
  2. {
  3.     painter->fillRect(rect, backgroundBrush());
  4. }

but that still scaled the background. Does anyone know how I could keep the background image from scaling?

3 replies

March 12, 2012

msx_br msx_br
Lab Rat
38 posts

Look at:

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…

 Signature 

msx_br - Brazil

March 14, 2012

ProfessorFang ProfessorFang
Lab Rat
3 posts

msx_br wrote:
Look at:

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.

Edit:
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:

  1. void FontPageView::drawBackground(QPainter *painter, const QRectF &rect)
  2. {  
  3.     QRectF sceneRect = this->sceneRect();
  4.    
  5.     QRectF bgrects = sceneRect;
  6.     //scale the rectangle so that it actually covers the area it's supposed to
  7.     bgrects.setBottomRight(bgrects.bottomRight() * scalefact);
  8.     painter->scale(1/scalefact, 1/scalefact);
  9.     painter->fillRect(bgrects, bgbr);
  10.     painter->scale(scalefact, scalefact);
  11. }

March 14, 2012

Asperamanca Asperamanca
Hobby Entomologist
371 posts

Maybe QGraphicsItem::ItemIgnoresTransformations is what you are looking for.

 
  ‹‹ qApp->setLayoutDirection() doesn’t work!      [SOLVED]How to fit in view the pixmaps in QGraphicsView/QGraphicsScene without changing aspect ratio ››

You must log in to post a reply. Not a member yet? Register here!