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

相关推荐
听风吹等浪起1 小时前
第26章:ViT+InceptionDW+FocalLoss改进|CT骨折影像二分类完整Pytorch实现(含PyQt5推理GUI)
pytorch·qt·分类
ShineWinsu2 小时前
对于Qt:QRadioButton常见属性以及QButtonGroup的解析
数据库·c++·qt·microsoft·gui·qradiobutton·qbuttongroup
sycmancia6 小时前
Qml——Image和AnimatedImage使用
qt
lhbweilai9 小时前
QT(了解各个控件和布局)上
数据库·qt
ShineWinsu15 小时前
对于Qt:QLabel常见属性的解析
c++·qt·面试·笔试·gui·标签·qlabel
小徐不徐说15 小时前
数据库存储基础:理解表结构、主键、事务与 BLOB
开发语言·数据库·c++·qt·程序设计
蓝祁子yoky1 天前
Qt学习日记 Day13:网络编程(一)-TCP基础与Socket实战
网络·qt·学习
飞浪纪元[FWC–FE]1 天前
从零到一键绿:Qt 5.14.2 + VS2019 开发环境搭建、Kit 套件配置与构建错误修复完全手册
开发语言·qt
ShineWinsu1 天前
对于Qt:QCheckBox常见属性的详细解析
c++·qt·面试·笔试·gui·qcheckbox·多选框
ShineWinsu2 天前
对于Qt:QPushButton常见属性的解析
开发语言·c++·qt·api·gui·按钮·qpushbutton