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

标签 c++ qt

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

相关文章:

qt - QString::toUtf8 在做什么?

c++ - Qt 所见即所得 - 字体大小问题

c++ - std::vector 中的值传播

c++ - Eigen Map - 它是否拥有数据指针?

c++ - 无法使用 Poco 库检索电子邮件内容

c++ - 在 Visual Studio C++ 2008 win32 运行时问题中调试与发布构建

c++ - 在 C++ 中,类型信息存储在哪里以及如何执行类型安全检查?

qt - cv::双边过滤器

c++ - QCompleter 在 NetworkRequest 完成后无法正常工作

python - QSystemTrayIcon 判断它是否被点击或处于焦点