QLabel使用setPixmap函数无法显示jpg图片的问题

  在Qt的QLabel控件中我们可以使用setPixmap函数显示图片。但很多时候我们会发现无法显示图片,导致该问题产生的原因有很多种,下面我们举出导致该现象产生的其中两种原因。

 

一、原因一:要被显示的图片路径含有中文,用户没有对中文进行转换

我们新建一个Qt工程,将下面的三个文件包含进来

main.cpp

#include "Label.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	Label w;
	w.show();
	return a.exec();
}

 

Label.h

#pragma once

#include <QLabel>
#include "ui_Label.h"

class Label : public QLabel
{
	Q_OBJECT

public:
	Label(QWidget *parent = Q_NULLPTR);
	~Label();

private:
	Ui::Label ui;
};

 

Label.cpp

#include "Label.h"

Label::Label(QWidget *parent)
	: QLabel(parent)
{
	ui.setupUi(this);
	this->setPixmap(QPixmap("Resources/小狗.jpg"));
}

Label::~Label()
{
}

 

工程的Resources目录下有图片“小狗.jpg”如下所示:

 

我们编译上述工程,运行。预期应该是可以显示“小狗.jpg”图片,但实际效果却如下所示:

 

导致上述问题产生的原因是:要被显示的图片路径(Resources/小狗.jpg)中含有中文。这个时候我们可以使用使用QString的fromLocal8Bit()函数,实现从本地字符集GBK到Unicode的转换。我们将Label.cpp的代码改成如下所示:

#include "Label.h"

Label::Label(QWidget *parent)
	: QLabel(parent)
{
	ui.setupUi(this);
	this->setPixmap(QPixmap(QString::fromLocal8Bit("Resources/小狗.jpg")));
}

Label::~Label()
{
}

 

重新编译,运行,效果如下:

可以看到,图片可以正常显示了。

 

二、原因二:使用了paintEvent函数

我们将上述Label.h改成

#pragma once

#include <QLabel>
#include "ui_Label.h"

class Label : public QLabel
{
	Q_OBJECT

public:
	Label(QWidget *parent = Q_NULLPTR);
	~Label();
	void paintEvent(QPaintEvent *);

private:
	Ui::Label ui;
};

 

将上述Label.cpp改成

#include "Label.h"
#include <QPainter>

Label::Label(QWidget *parent)
	: QLabel(parent)
{
	ui.setupUi(this);
	this->setPixmap(QPixmap(QString::fromLocal8Bit("Resources/小狗.jpg")));
}

Label::~Label()
{
}

void Label::paintEvent(QPaintEvent *)
{
	;
}

 

重新编译,运行,效果如下:

 

我们发现改动程序后,又无法显示图片了。导致该现象的原因是引入了paintEvent函数,引入该函数我们就没法再通过setPixmap函数显示图片了,我们只能通过QPainter显示图片了。

 

我们改动Label.h如下:

#pragma once

#include <QLabel>
#include "ui_Label.h"

class Label : public QLabel
{
	Q_OBJECT

public:
	Label(QWidget *parent = Q_NULLPTR);
	~Label();
	void paintEvent(QPaintEvent *);

private:
	Ui::Label ui;
	QPixmap m_image1;
};

 

改动Label.cpp如下:

#include "Label.h"
#include <QPainter>

Label::Label(QWidget *parent)
	: QLabel(parent)
{
	ui.setupUi(this);
	m_image1.load(QString::fromLocal8Bit("Resources/小狗.jpg"));
}

Label::~Label()
{
}

void Label::paintEvent(QPaintEvent *)
{
	QPainter painter(this);
	painter.drawPixmap(0, 0, this->width(), this->height(), m_image1);
}

 

重新编译,运行,我们发现又可以显示图片了,效果如下:

  • 20
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要让 QLabel 自适应 Mat 图片大小显示,可以通过以下步骤实现: 1. 将 Mat 图像转换为 QPixmap 类型,使用 QPixmap::fromImage() 函数将 Mat 转换为 QImage,再使用 QPixmap::fromImage() 函数将 QImage 转换为 QPixmap。 2. 根据 QLabel 的大小和 QPixmap 的大小计算缩放比例,使用 QPixmap::scaled() 函数进行缩放。 3. 将缩放后的 QPixmap 设置为 QLabel 的图像,使用 QLabel::setPixmap() 函数进行设置。 下面是一个示例代码: ```python import cv2 from PyQt5.QtGui import QPixmap, QImage from PyQt5.QtWidgets import QLabel, QWidget, QHBoxLayout # 加载 Mat 图像 mat_img = cv2.imread('test.jpg') # 将 Mat 转换为 QPixmap qimg = QImage(mat_img.data, mat_img.shape[1], mat_img.shape[0], QImage.Format_RGB888) qpixmap = QPixmap.fromImage(qimg) # 创建 QLabel 和 QWidget label = QLabel() widget = QWidget() # 将 QLabel 放置在 QWidget 中 layout = QHBoxLayout(widget) layout.addWidget(label) # 计算缩放比例并设置 QLabel 的图像 scale_ratio = min(label.width() / qpixmap.width(), label.height() / qpixmap.height()) scaled_pixmap = qpixmap.scaled(qpixmap.width() * scale_ratio, qpixmap.height() * scale_ratio) label.setPixmap(scaled_pixmap) # 显示 QWidget widget.show() ``` 在上面的代码中,我们首先将 Mat 图像转换为 QPixmap,并根据 QLabel 大小和 QPixmap 大小计算缩放比例。然后使用 QLabel::setPixmap() 函数将缩放后的 QPixmap 设置为 QLabel 的图像,并将 QLabel 放置在 QWidget 中进行显示

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值