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);
    }
    }

相关推荐
Quz3 小时前
QML 分隔线:Rectangle、菜单与工具栏分隔线用法
qt
Quz3 小时前
QML PathView 入门:直线路径与曲线路径
qt
实心儿儿4 小时前
Qt — 音视频文件
qt·音视频
Cx330❀8 小时前
【Qt 进阶指南】详解 Qt 常用标准对话框与主窗口组件架构
开发语言·qt·ui·性能优化·架构·图形渲染
阿钱真强道9 小时前
25 嵌入式操作系统 | 设备面板:用 Qt 控件把“设备“画出来(本节不接串口)
开发语言·qt·flask·架构设计·协议契约
kaixin_learn_qt_ing10 小时前
使用 PySide6 编写Qt程序
qt
艾莉丝努力练剑10 小时前
【QT】系统相关:多线程QThread基础
开发语言·网络·c++·qt·学习·大模型
xcyxiner1 天前
DicomViewer24 修复编译bug(window test 失败)
前端·qt
sycmancia1 天前
Qml——Rectangle元素
qt
sycmancia1 天前
Qml——Item元素
qt