QLabel实现点击事件

主要是通过安装过滤器,实现点击事件。

事件过滤器的操作:

  1. 首先给需要添加事件过滤器的部件注册监听对象;

    对象名->installEventFilter(true);

  1. 重写eventFilter(QObject *obj, QEvent *event)函数进行处理。

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    ui->label_LianJieDuXieQi->installEventFilter(this); //1.给label安装事件过滤器
    }
    bool MainWindow::eventFilter(QObject *watched, QEvent *event)
    {
    if (watched == ui->label_LianJieDuXieQi) {//2.监听label
    if (event->type() == QEvent::MouseButtonPress) {
    qDebug()<<"1";
    FuncOpenReader();
    return true;
    }
    else {
    return false;
    }
    }
    else {
    return QWidget::eventFilter(watched, event);
    }
    }

相关推荐
天燹14 小时前
Qt 6 嵌入 Android 原生应用完整教程
android·开发语言·qt
liu****14 小时前
第一章 Qt 概述
开发语言·c++·qt
陌路2014 小时前
RPC分布式通信(5)--发布 RPC 服务、处理客户端调用请求
分布式·qt·rpc
少控科技15 小时前
QT进阶日记004
开发语言·qt
抠脚学代码16 小时前
Qt与Linux
linux·数据库·qt
机器视觉知识推荐、就业指导16 小时前
Qt 6 所有 C++ 类(官方完整清单 · 原始索引版)
开发语言·c++·qt
凯子坚持 c17 小时前
在 Visual Studio 2019 中配置 Qt 插件开发环境:编译原理 + 底层逻辑解析
ide·qt·visual studio
枫叶丹418 小时前
【Qt开发】Qt系统(八)-> Qt UDP Socket
c语言·开发语言·c++·qt·udp
艾莉丝努力练剑19 小时前
【QT】Qt 从零上手:Hello World、项目文件与实战避坑指南
linux·运维·开发语言·c++·qt·继承·qt5
prettyxian19 小时前
【QT】信号与槽:自定义信号、参数传递与Lambda
开发语言·qt