C++ 在 Qt 中以编程方式设置 QLabel 的像素图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6913575/
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-28 20:52:46  来源:igfitidea点击:

Programatically setting the pixmap of a QLabel in Qt

c++qt

提问by karim

The Widget we should use to show pictures is a QLabel. we can do it directly from QtCreator, by setting its pixmap property.

我们应该用来显示图片的 Widget 是一个 QLabel。我们可以通过设置它的 pixmap 属性直接从 QtCreator 中完成。

we should first create a resource file and then add the image to that resource file. To create a Qt Resource File, we go to the menus: File > Qt > Qt Resource File.

我们应该首先创建一个资源文件,然后将图像添加到该资源文件中。要创建 Qt 资源文件,我们转到菜单:文件 > Qt > Qt 资源文件。

we can set the image of the QLabel using Qt Creator...

我们可以使用 Qt Creator 设置 QLabel 的图像...

but i would want to change the pic according to some input from the user

但我想根据用户的一些输入更改图片

i tried to do the following :

我尝试执行以下操作:

#include "form1.h"
#include "form.h"
#include "ui_form.h"
#include "ui_form1.h"

Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);
    QPixmap * mypix = new QPixmap(":/karim/test.png");
    ui->label->setPixmap(mypix);
    delete mypix;
}

but i got this error

但我收到了这个错误

..\Project\form.cpp: In constructor 'Form::Form(QWidget*)':

..\Project\form.cpp:12: error: no matching function for call to 'QLabel::setPixmap(QPixmap*&)'

c:\QtSDK\Simulator\Qt\mingw\include/QtGui/qlabel.h:123: note: candidates are: void QLabel::setPixmap(const QPixmap&)

what could be the problem ?

可能是什么问题呢 ?

回答by O.C.

The signature of the method you are trying to use is

您尝试使用的方法的签名是

setPixmap ( const QPixmap & )

setPixmap ( const QPixmap & )

but you are passing in a pointer. Try using a value instead.

但你正在传递一个指针。尝试改用一个值。

QPixmap mypix (":/karim/test.png");
ui->label->setPixmap(mypix);