PDA

View Full Version : QGraphicsView best way to draw fixed (static) background



wayfaerer
20th May 2012, 19:50
I've reimplemented QGraphicsView::drawBackground() to draw some grid lines, and now I want to draw a static background behind them. In particular, I'd like a gradient that isn't affected by the view transform, scrolling, etc.

Does it make more sense to do draw this background under the grid lines in my drawBackground() reimplementation (using e.g., this method (https://bugreports.qt-project.org/browse/QTBUG-7079), or to give the QGraphicsView a transparent background and put it on top of another QWidget to draw the background?

wayfaerer
21st May 2012, 20:58
Perhaps a better way to ask this question:

If I make the background of a QGraphicsView transparent, and put it on top of another widget, will that widget need to be redrawn whenever the QGraphicsView is redrawn? If so, there isn't much benefit to that approach. If not, I imagine it would be much faster.

wysota
22nd May 2012, 00:15
Set a custom widget with reimplemented paintEvent() as the viewport of the Graphics View or set a custom brush as Base component of the standard viewport's palette.

wayfaerer
22nd May 2012, 06:10
I managed to get it working using style sheets, i.e.,


myView->setStyleSheet("QGraphicsView {background: qradialgradient(cx:0.5, cy:0.5, fx:0.5, fy:0.5, radius: 1, stop:0 white, stop:1 gray)}");

Is that roughly the same thing? In particular I'm really curious whether or not the gradient from the style sheet is continuously being redrawn or not.

wysota
23rd May 2012, 08:10
Roughly yes. It depends what you mean by "redrawn". It is redrawn but the image of the background should be taken from the backingstore so the actual painting routine of the widget should not be called.