如何在QLabel、QT中设置图片?

How to set a image in QLabel, QT?

想在 QLabels 中为 QT 中的一个小游戏设置图像: 我是QT的初学者。

我的页眉是:

#include <QWidget>
#include <QPushButton>
class Window : public QWidget
{
    Q_OBJECT

 public:
 explicit Window(QWidget *parent = 0);
 void initButtons();
 void initMap();

signals:
public slots:

private:
    QPushButton *m_button;
    QLabel *label;


};

在 .cpp 中

Window::Window(QWidget *parent) : QWidget (parent)
{
    setFixedSize(1500,900);

    m_button = new QPushButton("Hello world", this);
    m_button->setGeometry(10,10,80,30);
    label = new QLabel("I'm a label", this);
    label->setGeometry(50,50,80,30);
    // ? hot to set image in label
}

你能帮帮我吗?

我用像素图和其他布局查看了 post,但我无法在标签中设置图像..

您可以像这样在 project.qrc 文件中定义资源:

<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="/">
   <file>resources/icon.svg</file>
</qresource>                                                                     
</RCC>

并在您的 .pro 文件中添加资源:

RESOURCES += project.qrc

然后在代码中你可以简单地做:

label->setPixmap(QPixmap(":/resources/icon.svg"));

假设您的项目中已有 .pro 和 .qrc 文件,您可以直接编辑它们。