PDA

View Full Version : QPainter and QGraphicsView drawBackground() override



forrestfsu
30th November 2006, 13:15
I have an override function for a QGraphicsView similar to what's below:



void form::drawBackground(QPainter *painter, const QRectF &rect)
{
...
QBrush brush(Qt::SolidPattern);
brush.setColor(Qt::black);
painter->setBrush(brush);
painter->drawPolygon(...);
...
}


I'm trying to set the entire background color of the QGraphicsView to a color, but the only way I've been able to do that is by using one of the draw functions (in the example above I used drawPolygon). Is there any functions available to do this. I tried using "setBackground(brush)" but that doesn't seem to be accomplishing what I need.

Thanks in advance!

wysota
30th November 2006, 20:46
Doesn't this work?


void MyGV::drawBackground(QPainter *painter, const QRectF &rect){
painter->save();
painter->setBrush(Qt::black);
painter->drawRect(rect);
painter->restore();
}

forrestfsu
4th December 2006, 19:37
Wysota, thanks for the code example. Unfortunately it doesn't solve my problem. The issue is that I have scaling and rotating going on within the graphics view. So, if I only use the dimensions that are contained wtihin rect, I sometimes see stray lines in the graphics view when it is scaled and rotated. A quick fix was to make the rect dimensions extremely large, so that even if I zoom out and rotate it, the rect still holds a large enough dimension to paint the full background. However, I think this is a problematic solution because I am unsure of the size of the input imagery. It could be thousands of pixels, so I don't want to call a drawRect function with dimensions much larger than that. I figured if there was a way to paint the entire graphics view's background color this might solve the problem. Any idea how to do it without setting a rectangular region?

wysota
4th December 2006, 22:22
Maybe you should just enable antialiasing and smooth transformations?

forrestfsu
5th December 2006, 15:52
Thanks for the suggestion, but it didn't fix the prob.