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

相关推荐
_林枭_5 小时前
ZW3D二次开发_原生表单_如何设置表单左上角图标
qt·zw3d
Quz11 小时前
QML 快捷键篇:基础快捷键与组合键序列
qt·qml
≮傷£≯√13 小时前
Qt 面试(一)
qt·ffmpeg·音视频
西西弗Sisyphus16 小时前
Qt 分类导航区的实现
开发语言·qt
galaxy_strive16 小时前
Qt C++插件化编写项目(1)
开发语言·c++·qt
kaixin_learn_qt_ing1 天前
Qt Model/View
qt
Quz1 天前
QML MouseArea:鼠标拖拽与滚轮缩放
qt·qml
CHPCWWHSU1 天前
DeepSeek-Harness-Qt将浏览器端Agent装入桌面应用
开发语言·qt
飘忽不定的bug2 天前
ubuntu20.04交叉编译QT5.15.12(RK3562+ubuntu20.04)
linux·qt·ubuntu
秋田君2 天前
Qt_QMediaPlayer类与QMediaPlaylist类
开发语言·qt