You can add a background image to your MainWindow
by doing the following:
- create a
QPixmap
and give it the path to your image. - create a
QPalette
and set it’sQBrush
with your pixmap and it’sColorRole
toQPalette::Background
. - set your
MainWindow
palette to the palette you created.
as an example you can add this lines to the constructor of your MainWindow
class:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPixmap bkgnd("/home/user/Pictures/background.png");
bkgnd = bkgnd.scaled(this->size(), Qt::IgnoreAspectRatio);
QPalette palette;
palette.setBrush(QPalette::Background, bkgnd);
this->setPalette(palette);
}
the advantage with this one is that you have the ability of modifying/changing your background image programmatically without having to use or learn any css stylesheet syntaxes.