Qt add background image

Add in the main interface

First, create a resource file.
Insert picture description here
My project is called untitled, right click untitled->add new file->Qt Resource file
and create a folder image in the project directory to store pictures

Add in main.cpp
Insert picture description here

    w.setAutoFillBackground(true);
    QPalette palette;
    QPixmap pixmap(":/image/2.jpg");
    palette.setBrush(QPalette::Window, QBrush(pixmap));
    w.setPalette(palette);

Get renderings
Insert picture description here
but this alone, the picture does not change as the window size changes, as is the case
Insert picture description here
then how can the dynamic image size is fixed that, and I found a problem, when the sub-interface settings
Insert picture description here
and then exit the main interface background on He is gone
then I thought for a moment, because I was such a link is established sub-interface and the main interface of
I wrote in chiled.cpp in the sub-interface

Widget *back = new Widget;
back->show();
this->hide();

This is equivalent to the original creation of a main interface in main, but because of clicking the signal to enter the sub
-interface, some functions of the sub-interface are executed, and then the return button in the sub-interface I re-created a main interface, but this main interface does not have a background setting. , So there will be no background display

I know this way to establish contacts main interface and sub-interface is very bad, because this child-per-click interface inside the return to the main interface will create a new main interface
like this is not not write, just to remember the main interface Jump to the slot function of the sub-interface

void Widget::on_pushButton_3_clicked()
{
    
    
    chiled *to_child = new chiled;
    to_child->show();
    delete this;
}

To write delete this;
do not write this->hide();
otherwise there will be a bunch of main interfaces that have not disappeared in memory

The same need to re-add the background setting part in the sub-interface jump to the main interface slot function every time

void chiled::on_commandLinkButton_clicked()
{
    
    
    Widget *back = new Widget;

    back->setAutoFillBackground(true);
    QPalette palette;
    QPixmap pixmap(":/image/2.jpg");
    palette.setBrush(QPalette::Window, QBrush(pixmap));
    back->setPalette(palette);
    back->show();

    delete this;
}

Okay, so the problem is solved, then let's take a look at how to make the background image size change with the window size

One solution is
Insert picture description here
to set the picture size and the interface size to be the same, and fix the interface size, as the above code does, but the interface size cannot be adjusted in this way

Another option is:

add in label

label is the name of the label determined in the ui, the following method will make the image fill the label

void Widget::set5()
{
    
    
    QPixmap *pixmap = new QPixmap(":/image/1.jpg");//加载图片
    pixmap->scaled(ui->label->size(), Qt::KeepAspectRatio);//设置图片大小
    ui->label->setScaledContents(true);//设置label全填效果
    ui->label->setPixmap(*pixmap);//把图片放到label中,其他控件加载图片思路一样的
}

There is another way to set the label to the size of the picture

void Widget::set5()
{
    
    
    QImage image ;
    image.load(":/image/1.jpg") ;

    ui->label_5->setPixmap(QPixmap::fromImage(image));
    ui->label_5->resize(QSize(image.width(),image.height()));

}

Guess you like

Origin blog.csdn.net/weixin_44972129/article/details/110389423
Recommended