我们应该用来显示图片的Widget 是一个QLabel。我们可以直接从 QtCreator 中设置它的 pixmap 属性来完成。
我们应该首先创建一个资源文件,然后将图像添加到该资源文件中。要创建 Qt 资源文件,我们转到菜单:文件 > Qt > Qt 资源文件。
我们可以使用 Qt Creator 设置 QLabel 的图像...
但我想根据用户的一些输入更改图片
我尝试执行以下操作:
#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;
}
但是我得到了这个错误
..\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&)
可能是什么问题?
最佳答案
您尝试使用的方法的签名是
setPixmap ( const QPixmap & )
但是你传递的是一个指针。尝试改用一个值。
QPixmap mypix (":/karim/test.png");
ui->label->setPixmap(mypix);
关于c++ - 以编程方式在 Qt 中设置 QLabel 的像素图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6913575/