PDA

View Full Version : Adding image in background



A.H.M. Mahfuzur Rahman
8th August 2009, 00: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

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

setBackgroundBrush(QPixmap(":/images/myimage.jpg"));

also

setCacheMode(CacheBackground); //makes better view

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

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

A.H.M. Mahfuzur Rahman
9th August 2009, 04: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());
pix.fill(Qt::transparent);
m_renderer->render(painter,"BackGround");

painter->drawPixmap(0,0,pix);

}

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?

Mahfuz

Lykurg
9th August 2009, 13: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, 16:39
I have implemented the same funcation for QGraphicsView, but got the same output in both case.