繁体   English   中英

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

[英]Programatically setting the pixmap of a QLabel in Qt

我们应该用来显示图片的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;
}

但我得到了这个错误

..\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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM