Several ways to correctly set the background image of a form in Qt

Several ways to correctly set the background image of a form in Qt

  • One of the ways to load images in QLabel
  • Chapter1 Several ways to correctly set the background image of a form in Qt
    • 1. Use styleSheet to set the background image of the form
  • Chapter2 Qt’s main window background setting
    • Method 1: The simplest way is to set it through the UI interface, such as setting the background image
    • Method 2: Set the palette property of the window through code.
    • Method 3: Set the background image and background color by overriding the paintEvent event
    • Method 4: Set the style sheet in code, which is similar to method 1
  • Chapter3 Vs + Qt two ways to add background images to the interface (very practical)
    • 1. Use code to implement the interface to add a background image
    • 2. Use style sheets to add background images to the interface
  • Chapter4 How to set a background image in Qt Desginer without covering other controls
  • Chapter5 Power software interface based on QT

One of the ways to load images in QLabel

 QPixmap pixmap(":/images/abc.jpg"); //Load images through the constructor
 pixmap.load(":/images/ab.jpg"); //Another way to load images
 
 pixmap.scaled(ui->label1->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
 ui->label1->setScaledContents(true);
 ui->label1->setPixmap(pixmap);

Chapter1 Several ways to correctly set the background image of a form in Qt

Original link: https://blog.csdn.net/yanche521/article/details/51017601

There are roughly two ways to correctly set the background image of a form in Qt, which will be explained one by one below:

1. Use styleSheet to set the background image of the form

When using stylesheet to set the background image of a form, you can directly follow the operation as shown below:

However, something to note is:

1. This method does not work in QWidget. If you are careful enough, you will find that using the same method of setting the background image, the background image does not actually change, but its subform background image does. changed.

In fact, we can solve this problem by adding a QWidget, that is, adding a Frame window in QtDesigner. We only need to set the string value of styleSheet for this newly added Frame window. All newly added sub-controls are added to this new Frame window.

2. Friends who have done Qt development should all know that Qt’s child form will inherit the properties of the parent form. This proves why the background of the parent form will also be in the child form. This is a reference to this A very reasonable explanation as to why. So the question is, how can we make the child form not inherit the background of the parent form?

Now that the reason has been analyzed above, we know how to solve it. Still opening the styleSheet code editing interface, we only need to enter the following lines of code to solve this problem. The specific code is as follows:

#Form name {<!-- -->
border-image: url(:/HouseRentSystem/Resources/test.png);
}

Chapter2 Qt’s main window background setting

Original link: https://blog.csdn.net/yinchengkai/article/details/124056878

First, let’s explain the three differences between background-image, border-image, and image.

background-image: A simple understanding is to map the image from the upper left corner of the component. The size of the component limits the range of the displayed image; for example, we crop the image according to the size of the component.

border-image: This is to scale the texture into the component. The component can see the complete image, but at this time the image will be compressed and deformed.

iamge: The widget will be filled according to the original size of the image

Method 1: The simplest way is to set it through the UI interface, such as setting a background image


There are three ways to add resources in the drop-down box:

There will be a problem after selecting background-image, that is, other sub-components on the window will also be pasted with images, as follows:

This way of adding will cause all components to be mapped, which is definitely not possible; you need to put background-image: url(:/image/background.png); in #MainWindow{} (MainWindow is the object name of the main window), which means It is only valid for the main window setting, and the sub-components are useless;

The same goes for background color settings and background image settings. If you don’t want the components in the window to have the same color as the main window, you also need #MainWindow to set the limited range.

The effect after setting is as follows:

Method 2: Set the palette attribute of the window through code

1) Background image settings

QPixmap pixmap=QPixmap(":/image/background.png").scaled(this->size());
QPalette palette;
//Set the main window background image
palette.setBrush(QPalette::Window,QBrush(pixmap));

2).Background color setting

QPalette palette;
//Set the main window background color
palette.setColor(QPalette::Window,QColor(255, 150, 30));
this->setPalette(palette);

Method 3: Set the background image and background color by overriding the paintEvent event

1) Set background image

void MainWindow::paintEvent(QPaintEvent *)
{<!-- -->
    QPainter painter(this);
 
    QPixmap pixmap(":/image/background.png");
    painter.drawPixmap(this->rect(),pixmap);
 
}

2) Set background color

void MainWindow::paintEvent(QPaintEvent *)
{<!-- -->
    QPainter painter(this);
 
    QColor color(255, 150, 30);
    painter.setBrush(color);
    painter.drawRect(this->rect());
}

Method 4: Set the style sheet in code, which is similar to method 1

1) Set the main window background image

this->setStyleSheet("QMainWindow {background-image:url(:/image/background.png)}");

2).Set the main window background color

this->setStyleSheet("QMainWindow {background-color:rgb(255, 150, 30)}");

Two ways to add background images in Chapter3 Vs + Qt interface (very practical)

Original link

1. Use code to implement the interface to add a background image

//Add a background image to the window
QPixmap Images("./1.png");
QPalette Palette = this->palette();
Palette.setBrush(QPalette::Background, Images);
this->setPalette(Palette);


Note: Use this method to add a background image to the interface and it can be displayed normally, as shown in the picture above. However, if you need to package the project into .exe and then transplant it to another computer to run, you need to pay attention to the following two details:

(1) Put the background image into the project file (as shown below), and use relative paths in the code (the code is as shown above);

(2) Copy another background image in the .exe file directory (as shown below);

Note: The transplanted .exe will now be able to display the background image on the interface normally;

2. Use style sheets to add background images to the interface

Chapter4 How to set a background image in Qt Desginer without covering other controls

Original link

Chapter5 QT-based power software interface

Original link: https://blog.csdn.net/2301_76989824/article/details/129490264

Power software interface based on QT
Contains software engineering source code, software configuration environment: VS2012 + Qt4.6, windows operating system
The software specifically includes functions:
[1] The home page of the interface is displayed, the system drop-down navigation bar is displayed, and the main function tab page is displayed.
[2] Retractable display on the right side of the drop-down navigation bar and customized list control;
[3] Customize tab page and table page functions;
[4] Contains software packaging program, one-click operation;
[5] Custom controls, QSS interface beautification, flat interface style display;
[6] Unify the interface style and background images.