I would like to add an image as background of my main window.
What is the best way to add this background image?
The remaining part of my main window must be transparent.
I would like to do this by QtCreator editor (for this component, therefore, no code)
You can do it directly from QtCreator, by setting its pixmap property. As others said, you should first create a resource file and then add the image to that resource file. To create a Qt Resource File, go to the menus: File > Qt > Qt Resource File.
You can add a background image to your MainWindow by doing the following:
QPixmap and give it the path to your image.QPalette and set it's QBrush with your pixmap and it's ColorRole to QPalette::Background.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With