View Full Version : Adding image in background

A.H.M. Mahfuzur Rahman
8th August 2009, 01:06
How Can I add image in the background of my application?

Say, I am drawing a QGraphicsItem in a QGraphicsScene and I want to add some background from an image( may be svg or png). And the background can be plain or tiled using the image.

Thank You

8th August 2009, 08:11
in your graphicsView class use



setCacheMode(CacheBackground); //makes better view

8th August 2009, 08:25
Do you mean the background of your whole application or of your QGraphicsView?

A.H.M. Mahfuzur Rahman
8th August 2009, 11:15
I meant background of the whole application.

A.H.M. Mahfuzur Rahman
9th August 2009, 05:16
I implemented the drawBackGround() virtual function of QGraphicsScene:

void GraphicsScene::drawBackground(QPainter* painter, const QRectF &rect){

QSvgRenderer* m_renderer = new QSvgRenderer(m_gameTheme->current());
QPixmap pix;

pix = QPixmap(sceneRect().size().toSize());



But, the in the output I saw that the whole Screen is not covered. I found, (0,0) is not right from the corner. What Should be the problem?


9th August 2009, 14:11
If you want a background for the whole application you need to set it on your QMainWindow. If it should only be for the view use QGraphicsView, because QGraphicsScene is only a part of the view!

--> QGraphicsView::drawBackground().

A.H.M. Mahfuzur Rahman
9th August 2009, 17:39
I have implemented the same funcation for QGraphicsView, but got the same output in both case.