C++ 如何将背景图像添加到 QMainWindow?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19939938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:16:42  来源:igfitidea点击:

How do I add a background image to the QMainWindow?

c++qt

提问by Sahil Gupta

Hi I am new to QT creator. I have tried a bunch of things to set my background image for the Q mainwindow. I added a resource folder with my image. I tried to add the by using setstylesheet in the UI and tried coding it. When I use the UI I can see the image, but when I run it nothing shows. I want put an image of a poker table that I have as the background and be able to put pushbuttons, etc on top.

嗨,我是 QT 创作者的新手。我尝试了很多方法来为 Q 主窗口设置背景图像。我用我的图像添加了一个资源文件夹。我尝试在 UI 中使用 setstylesheet 添加并尝试对其进行编码。当我使用 UI 时,我可以看到图像,但是当我运行它时,什么也没有显示。我想放一张扑克桌的图像作为背景,并且能够在上面放置按钮等。

main.cpp:

主.cpp:

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();


    return a.exec();
}

mainwindow.cpp

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setStyleSheet("{background-image: url(:/images/images/PokerTableBackground.jpg);}");
}

MainWindow::~MainWindow()
{
    delete ui;
}

Like I said I tried doing this, and putting the image through the UI and neither one of them work. I want the image set as the background for the whole thing.

就像我说的那样,我尝试这样做,并将图像通过 UI 放置,但它们都不起作用。我希望图像设置为整个事情的背景。

I also tried using this:

我也尝试使用这个:

QWidget *pic = new QWidget(ui->tab);
    pic->setStyleSheet("background-image: url(:/images/images/PokerTableBackground.jpg)");
    pic->setGeometry(QRect(10,10,220,48)); // your location and size.

回答by Barracuda

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

您可以MainWindow通过执行以下操作将背景图像添加到您的:

  1. create a QPixmapand give it the path to your image.
  2. create a QPaletteand set it's QBrushwith your pixmap and it's ColorRoleto QPalette::Background.
  3. set your MainWindowpalette to the palette you created.
  1. 创建一个QPixmap并为其指定图像的路径。
  2. 创建一个QPalette并将其设置QBrush为您的像素图,然后将其设置ColorRoleQPalette::Background.
  3. 将您的MainWindow调色板设置为您创建的调色板。

as an example you can add this lines to the constructor of your MainWindowclass:

例如,您可以将此行添加到MainWindow类的构造函数中:

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.

这样做的好处是您可以以编程方式修改/更改背景图像,而无需使用或学习任何 css 样式表语法。

回答by Hadi Rasekh

You can set background style sheet of central window,

您可以设置中央窗口的背景样式表,

this->centralWidget()->setStyleSheet("background-image:url(\"bkg.jpg\"); background-position: center; ");

In the constructor it would be something like this:

在构造函数中,它会是这样的:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->centralWidget()->setStyleSheet(
         "background-image:url(\"bkg.jpg\"); background-position: center;" );
}

回答by DomTomCat

The CSS style background will be inherited in the child widgets and thus create weird looking windows. A possible solution is to limit the background to MainWindowor #centralWidget. If, additionally, you want to stretch the image to cover the full widget, use this kind of CSS

CSS 样式背景将在子小部件中继承,从而创建外观怪异的窗口。一个可能的解决方案是将背景限制为MainWindow#centralWidget。此外,如果您想拉伸图像以覆盖整个小部件,请使用这种 CSS

this->setStyleSheet(
            "#centralWidget { "
            " border-image: url(:/background.png) 0 0 0 0 stretch stretch;"
            "}");