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

Programatically setting the pixmap of a QLabel in Qt

本文关键字:QLabel 像素 设置 方式 Qt 编程      更新时间:2023-10-16

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

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

我们可以使用Qt Creator来设置QLabel的映像…

但是我想根据用户

的一些输入来改变PIC

我试着做以下事情:

#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;
}

但是我得到了这个错误

..Projectform.cpp: In constructor 'Form::Form(QWidget*)':
..Projectform.cpp:12: error: no matching function for call to 'QLabel::setPixmap(QPixmap*&)'
c:QtSDKSimulatorQtmingwinclude/QtGui/qlabel.h:123: note: candidates are: void QLabel::setPixmap(const QPixmap&)

您要使用的方法的签名是

setPixmap (const QPixmap &)

但是你传递的是一个指针。试试用一个值来代替。

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