PDA

View Full Version : Set background image in QMainWindow



superteny
25th May 2009, 12:50
hi,

I have a problem when i try to load a background image in a QMainWindow.
i override the paintevent method with the next code:

void MainWindow::paintEvent(QPaintEvent *pe)
{

QPainter* pPainter = new QPainter(this);
pPainter->drawPixmap(rect(), QPixmap(":/images/metal.png"));
delete pPainter;
QWidget::paintEvent(pe);

}

I think the code is ok, but i am not sure!!! It doesnt load any bg image.
the image size has to match with mainwindow size?
any ideas??
thank you.

munna
25th May 2009, 13:11
I think the following line of code is drawing on top of your image and that is why you are not able to see the image



QWidget::paintEvent(pe);


Remove the following line, make sure that the image file is part of your resources and then try.

gikidy
26th May 2009, 05:24
pPainter->drawPixmap(rect(), QPixmap(":/images/metal.png"));
Maybe you should change this line to
pPainter->drawPixmap(rect(), QPixmap("./images/metal.png"));

aamer4yu
26th May 2009, 06:16
Instead of overriding the main window, you can set the background through palette
QPalette::setBrush with QPalette::Window.
And I hope you you know how to make brush of pixmap :)

wagmare
26th May 2009, 06:23
using palette for setting background


QPixmap pixmap("jughead-jones_large.png");
QListView w;
QPalette p = w.palette();
p.setBrush(QPalette::Base, pixmap);
w.setPalette(p);
w.resize(pixmap.size())