本文已参与「新人创作礼」活动,一起开启掘金创作之路。
先了解一下什么是QLabel,官方文档中是这样说的 `QLabel Class
The QLabel widget provides a text or image display. More...
\
| Header: | #include <QLabel> |
|---|---|
| qmake: | QT += widgets |
| Inherits: | QFrame` |
从文档中我们可以得知,QLabel继承自QFrame,其主要的功能就是显示一些图片或者是文字,当然视频是由一帧帧的图片组成,所以QLabel也可以显示视频。
下面看一下QLabel有哪些属性,只有了解其属性以及相关的函数,才能方便我们使用,不过也不可能记住每个控件的所有的函数和属性,所以QT的帮助文档就提供了非常好的帮助。
在上面官方文档提供的属性中,非常多,本博客仅用到QPixmap来显示图片。
由于需要对QLabel进行事件处理,让其相应鼠标的单击和双击事件,所以我们需要考虑使用事件过滤器,也就是evenFilter,这个东西官网是这样说的:如果此对象(本博客指的是QLabel)已作为监视对象的事件筛选器安装,则筛选事件。在重新实现此函数时,如果要过滤掉事件,即停止进一步处理,请返回true;否则返回false。
给哪个控件添加事件过滤器就要给哪个空间安装事件过滤器,例如label->installEventFilter(filterObj);就是给QLabel控件安装事件过滤器,官网也给出了例子,如下
横线上面是.h文件,1处表明事件过滤器的定义,横线下面表示cpp文件,2处表示给textEdit控件添加事件过滤器,3处是事件过滤器函数的定义,表示执行的动作,下面直接上代码说明。代码结果如下所示
.pro文件直接默认,没有做修改
.h文件代码如下
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QFileDialog>
#include <QMessageBox>
#include <QDebug>
#include <QMouseEvent>
#include <QPixmap>
#include <QImage>
#include <QDir>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
bool eventFilter(QObject *watched, QEvent *event);
// 图像放大标志位
int flag;
QString patrol_path;
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
.cpp文件如下
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
flag = 1;
ui->stackedWidget->setCurrentIndex(0);
// 安装patrol1-6事件过滤器
ui->label->installEventFilter(this);
// 设置标签内容随窗口变化而变化
ui->biglabel->setScaledContents(true);
}
Widget::~Widget()
{
delete ui;
}
bool Widget::eventFilter(QObject *watched, QEvent *event)
{
// 单击label选择图片显示到label
if(qobject_cast<QLabel*>(watched) == ui->label && flag == 1 &&event->type() == QEvent::MouseButtonPress)
{
QString path = QFileDialog::getOpenFileName(this, tr("选择图片"), ".", tr("Image Files(*.jpg *.png *.jpeg *.bmp)"));
QImage* img = new QImage;
patrol_path = path;
if(!(img->load(path))) //加载图像
{
QMessageBox::information(this, tr("打开图片失败"), tr("打开图片失败!"));
delete img;
return false;
}
img->load(path);
QPixmap pixmap = QPixmap::fromImage(*img);
ui->stackedWidget->setCurrentIndex(0);
int with = ui->label->width();
int height = ui->label->height();
//QPixmap fitpixmap = pixmap.scaled(with, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); // 饱满填充
QPixmap fitpixmap = pixmap.scaled(with, height, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 按比例缩放
ui->label->setPixmap(fitpixmap);
flag = 0;
return true;
}
// 对biglabel进行事件处理,双击查看大图
if(qobject_cast<QLabel*>(watched) == ui->label && event->type() == QEvent::MouseButtonDblClick && flag == 0)
{
QImage Image;
Image.load(patrol_path);
QPixmap pixmap = QPixmap::fromImage(Image);
ui->stackedWidget->setCurrentIndex(1);
int with = ui->biglabel->width();
int height = ui->biglabel->height();
//QPixmap fitpixmap = pixmap.scaled(with, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); // 饱满填充
QPixmap fitpixmap = pixmap.scaled(with, height, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 按比例缩放
ui->biglabel->setPixmap(fitpixmap);
return true;
}
return false;
}
void Widget::on_pushButton_clicked()
{
ui->stackedWidget->setCurrentIndex(0);
}
main文件没有改动,直接默认即可。 下面给出UI结构图
程序运行结果如下
上面已经给出整个项目的所有代码,仍需要整个的项目的可以联系我。