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;

}

}

相关推荐
多弗朗皮卡丘41 分钟前
C++STL之空间配置器
开发语言·c++·内存池·空间配置器
牵猫散步的鱼儿1 小时前
Demosaic 和 Remosaic
c++
CodeYp20191 小时前
第19讲:封装红黑树实现 mymap 和 myset
开发语言·c++·红黑树
Quz1 小时前
QML 字体度量与文本度量:FontMetrics 与 TextMetrics
qt
Quz1 小时前
QML 字体加载:FontLoader 与 FontInfo
qt
孙鹏宇.1 小时前
C++类型转换
c++
超级架构师2 小时前
让 COBOL 长出 AI 大脑:原生 Agent 核心框架 praxo-core 深度剖析
c语言·开发语言·c++·人工智能·python·架构·ai编程
·present·2 小时前
CSAPP(lecture 10)
c语言·c++
YLCHUP2 小时前
【模拟&STL】题解:AT_abc203_e [ABC203E] White Pawn_基础算法_数据结构_STL模板库_AtcoderABC_C++算法竞赛
数据结构·c++·算法·leetcode·动态规划·哈希算法·近邻算法
xiaolang_8616_wjl2 小时前
c++游戏_宝可梦大世界(源代码)
开发语言·c++·游戏