Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt Creator and main window background image

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)

like image 429
Safari Avatar asked Jun 13 '13 20:06

Safari


People also ask

How do I create a PNG file in Qt?

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.


1 Answers

You can add a background image to your MainWindow by doing the following:

  1. create a QPixmap and give it the path to your image.
  2. create a QPalette and set it's QBrush with your pixmap and it's ColorRole to QPalette::Background.
  3. 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.

like image 103
moki Avatar answered Oct 05 '22 01:10

moki