我们用来显示图片的Widget是QLabel.我们可以通过设置pixmap属性直接从QtCreator中完成.
我们应该首先创建一个资源文件,然后将该图像添加到该资源文件中.要创建Qt资源文件,我们转到菜单:File> Qt> Qt Resource File.
我们可以使用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;
}
Run Code Online (Sandbox Code Playgroud)
但我得到了这个错误
..\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&)
Run Code Online (Sandbox Code Playgroud)
可能是什么问题呢 ?
O.C*_*.C. 14
您尝试使用的方法的签名是
setPixmap(const QPixmap&)
但是你正在传递一个指针.请尝试使用值.
QPixmap mypix (":/karim/test.png");
ui->label->setPixmap(mypix);
Run Code Online (Sandbox Code Playgroud)