PDA

View Full Version : Problem with background image



qt890
23rd April 2015, 15:49
Hi, I have a window with a Qwt barchart and I need to display a background image that fills all the window.

The best I could get was:
11132

As you can see, the image isn't displayed correctly. I've overridden MainWindow's resizeEvent() method this way in main.c:


void MainWindow::resizeEvent(QResizeEvent*)
{
QPixmap bkgnd(":/files/image.png");
bkgnd = bkgnd.scaled(this->size(), Qt::IgnoreAspectRatio);
QPalette palette;
palette.setBrush(QPalette::Background, bkgnd);
this->setPalette(palette);
}

And commented in barchart.cpp:

//setAutoFillBackground( true );
//setPalette( Qt::white );
//canvas()->setPalette( QColor( "LemonChiffon" ) );

The original image is:
11131

Using bkgnd = bkgnd.scaled(this->size(), Qt::KeepAspectRatioByExpanding); produces the same output.

How can I solve it?

Uwe
24th April 2015, 07:03
How can I solve it?
Depends on what you want to do.

If you really want to have the image as background of the main window you probably need to make the widgets of the plot having no background. But I would overload the paintEvent drawing the image instead of trying to set the background brush as the palette is something that is inherited by the children.

Uwe

qt890
27th April 2015, 13:23
Thanks for your reply Uwe. Yes, I want to have the image as background of the main window.

I've tried to overload paintEvent methods this way:


void BarChart::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setBackgroundMode(Qt::TransparentMode);
}



void MainWindow::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QPixmap bkgnd(":/files/image.png");
painter.drawPixmap(QPoint(0,0), bkgnd);
}


But the output is:
11137