PDA

View Full Version : Drawing the background of QGraphicsView



aknuds1
28th February 2007, 13:47
I'm trying to change the background in my QGraphicsView after it is initially created, but in QGraphicsView.drawBackground only part of the view is drawn. How do I trigger a repaint of the whole view (or certain parts of it)?

camel
28th February 2007, 14:32
QWidget::update() will help you there. (Since QGraphicsView is a child of QWidget)

The coordinates are in "View Coordinates" which you might have to map from "Scene Coordinates" via QGraphicsView::mapFromScene()

aknuds1
28th February 2007, 16:19
I tried update() on the whole view I think, but still only part of the view was scheduled for background drawing. I'm not at work, so I don't have the actual program in front of me, but I'm pretty sure I tried what you're proposing. Are you sure this should work?

camel
28th February 2007, 16:31
Well, it should, unless graphicsview is optimizing a bit (more than you would like)...and it seems to be (i.e. caching). But one look at the documentation revealed the nice solution to your problem: QGraphicsView::resetCachedContent()

aknuds1
28th February 2007, 18:52
I did try resetCachedContent(), but it did nothing really. Besides, caching is turned off by default according to Assistant.

maxpower
28th February 2007, 19:35
My GV is created in a UI file and then as part of my program's init i set a brackground. This is the code I use:



QString fieldImage = ":/images/Field.png";
scene = new QGraphicsScene();
scene->setSceneRect(0, 0, fieldWidth, fieldHeight);
fieldGV->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
fieldGV->setBackgroundBrush(QImage(fieldImage));
fieldGV->setCacheMode(QGraphicsView::CacheNone);
fieldGV->setScene(scene);


Hope it helps.

mAx

camel
28th February 2007, 21:46
I did try resetCachedContent(), but it did nothing really.

Could you post a little programm (i.e. minimal but runable ;-) That shows the behaviour? That makes helping easier :-)

aknuds1
28th February 2007, 21:55
Could you post a little programm (i.e. minimal but runable ;-) That shows the behaviour? That makes helping easier :-)
I'll see what I can do tomorrow.

aknuds1
28th February 2007, 21:57
My GV is created in a UI file and then as part of my program's init i set a brackground. This is the code I use:



QString fieldImage = ":/images/Field.png";
scene = new QGraphicsScene();
scene->setSceneRect(0, 0, fieldWidth, fieldHeight);
fieldGV->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
fieldGV->setBackgroundBrush(QImage(fieldImage));
fieldGV->setCacheMode(QGraphicsView::CacheNone);
fieldGV->setScene(scene);


Hope it helps.

mAx
Thanks for the tip, but the problem is rather that I want to trigger redrawing of the background at some point. Initial drawing of the background is no problem.

aamer4yu
1st March 2007, 03:57
do the image width and height and Scene rect width/height match ??

might be thers some scaling problem ???

maxpower
1st March 2007, 16:23
Okay, but why can't you use setBackGroundBrush() again? I can call that from anywhere in my app and the background changes just fine. I guess it depends on exactly what you are trying to draw as the background.

mAx

rajesh
2nd March 2007, 06:05
workaround.
just add one item to scene and remove, this will redraw the background.

Bitto
8th March 2007, 21:36
Or define the scene rect, no workaround needed. ;-)

giverson
9th March 2007, 14:53
is it a partial background refresh where it just refreshes the scene and leaves everything outside the scene the old background color or does it just plain not work?

You might also try updating to 4.2.3 since it looks like they did something on this...

"General improvements
--------------------

- Graphics View

* Calling QGraphicsScene::update() without arguments now correctly
updates the entire scene.
* Changing the background brush in QGraphicsScene now correctly updates
the entire scene
"