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;

}

}

相关推荐
NeilYuen5 小时前
【C++】STL源码仿写(二):vector
开发语言·c++
郝学胜_神的一滴6 小时前
游戏引擎原理与实践 04:拆解引擎基础系统与内存管理
c++·游戏
All for pursuit.6 小时前
【设计-1】208.实现Trie (前缀树)
数据结构·c++·算法·leetcode
weixin_307779136 小时前
C++代码实现MATLAB中的crossval函数功能
开发语言·c++·算法·matlab
青少儿编程课堂6 小时前
背包问题(0/1 背包与完全背包)解题精讲——动态规划入门
c++·python·算法·bfs·信息学竞赛
倒头就睡的小比特7 小时前
C++多态
c++
潼心1412o9 小时前
C++初阶(长期更新)第5讲:类和对象(中)
开发语言·c++
weixin_307779139 小时前
C++代码实现MATLAB中的normalize函数功能
开发语言·c++·算法·matlab
(Charon)9 小时前
【C++面试】手写String类:同时实现拷贝构造与移动构造
c++·算法
程序员老陆9 小时前
通过std::unique_ptr初始化std::shared_ptr
开发语言·c++