Qt 鼠标双击QLabel全屏显示图像

1、打开图片,并显示在QLabel上

void MainWindow::on_pB_ts_clicked()

{

QString filePath= QFileDialog::getOpenFileName(this,u8"选择图像" , "",

u8"Images (*.png *.bmp *.jpg *.tif *.GIF )" );

if(filePath.isEmpty())

return;

QImage image(filePath);

QPixmap pixmap = QPixmap::fromImage(image);

QSize drawSize = ui->label_img->size();

QPixmap fitPix = pixmap.scaled(drawSize, Qt::KeepAspectRatio);

ui->label_img->setPixmap(fitPix);

}

2、实现鼠标双击QLabel全屏显示图像

使用事件过滤器实现

在 mainwindow.h中添加:

protected:

bool eventFilter(QObject *obj, QEvent *event) override;

在 mainwindow.cpp的构造函数

ui->label_img->installEventFilter(this); //为 Label 安装事件过滤器

实现 eventFilter:

bool MainWindow::eventFilter(QObject *obj, QEvent *event)

{

if (obj == ui->label_img && event->type() == QEvent::MouseButtonDblClick) {

toggleFullScreen(); // 双击全屏

return true; // 事件已处理

}

// 全屏窗口的 Label 也被双击时,退出全屏

if (obj == m_fullScreenLabel && event->type() == QEvent::MouseButtonDblClick) {

toggleFullScreen(); // 双击退出全屏

return true;

}

return QMainWindow::eventFilter(obj, event);

}

// 全屏切换核心函数

void MainWindow::toggleFullScreen()

{

if (!m_isFullScreen) {

// ===== 进入全屏 =====

// 检查是否有图像显示

if (ui->label_img->pixmap() == nullptr || ui->label_img->pixmap()->isNull())

return;

// 获取当前显示的图像

QPixmap currentPixmap = *(ui->label_img->pixmap());

// 获取屏幕尺寸

QDesktopWidget *desktop = QApplication::desktop();

QRect screenRect = desktop->screenGeometry();

// 创建全屏窗口(无边框、置顶)

m_fullScreenWindow = new QWidget(nullptr, Qt::Window | Qt::FramelessWindowHint);

m_fullScreenWindow->setStyleSheet("background-color: black;");

m_fullScreenWindow->setGeometry(screenRect);

// 创建全屏用的 QLabel

m_fullScreenLabel = new QLabel(m_fullScreenWindow);

m_fullScreenLabel->setAlignment(Qt::AlignCenter);

// 缩放图像适应屏幕(保持宽高比)

QSize scaledSize = currentPixmap.size().scaled(screenRect.size(), Qt::KeepAspectRatio);

m_fullScreenLabel->setPixmap(currentPixmap.scaled(scaledSize, Qt::KeepAspectRatio, Qt::SmoothTransformation));

m_fullScreenLabel->setFixedSize(scaledSize);

// 布局:将 Label 居中放置

QVBoxLayout *layout = new QVBoxLayout(m_fullScreenWindow);

layout->setContentsMargins(0, 0, 0, 0);

layout->addWidget(m_fullScreenLabel, 1, Qt::AlignCenter);

// 也为全屏 Label 安装事件过滤器,捕获双击退出

m_fullScreenLabel->installEventFilter(this);

// 显示全屏窗口

m_fullScreenWindow->showFullScreen();

m_isFullScreen = true;

} else {

// ===== 退出全屏 =====

// 关闭并删除全屏窗口

if (m_fullScreenWindow) {

m_fullScreenWindow->close();

delete m_fullScreenWindow;

m_fullScreenWindow = nullptr;

m_fullScreenLabel = nullptr;

}

m_isFullScreen = false;

}

}

相关推荐
辰小忆5 分钟前
11 · RunCurveChart —— 实时运行曲线
c++·ui·duilib
Hhy_11073 小时前
《C++深度解构07》手撕 vector —— 从底层三指针架构到迭代器失效彻底吃透
c语言·开发语言·c++·学习·visual studio
艾莉丝努力练剑3 小时前
【QT】系统相关:TCP的API介绍
开发语言·前端·c++·人工智能·qt·学习·tcp/ip
青少儿编程课堂4 小时前
蓝桥杯·全国青少年信息素养大赛·电子学会Python/C++考级:中国剩余定理解析
c++·python·算法·bfs·信息学竞赛
wo_jack4 小时前
C++ 入门笔记(十八):list 进阶 —— const 迭代器、operator-> 与 clear 的坑
数据结构·c++·面试·stl·list
ShineWinsu5 小时前
对于Qt:QVBoxLayout常见属性以及方法的解析
开发语言·数据库·c++·qt·gui·客户端·布局管理器
ShineWinsu16 小时前
对于Qt:QFormLayout常见属性以及方法的解析
开发语言·c++·qt·面试·客户端·表单·qformlayout
kyle~16 小时前
嵌入式 SoC 系统
linux·c++·嵌入式·计算系统
爱写Bug的浮游20 小时前
C++移动构造真的比拷贝快吗
c++·性能优化
Aaa西红柿鸡蛋20 小时前
网络协议分析实验:基于 UDP 广播模型的 C/S 通信设计与实现
c++