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

相关推荐
小徐不徐说21 小时前
Qt 线程迁移机制完整实战指南(moveToThread)
开发语言·c++·qt·程序设计
Traveler飞21 小时前
Ubuntu20.04 VMware 黑屏无法进入桌面,Ctrl+Alt+F2 可以进入终端,最终发现是磁盘空间满了
qt·ubuntu
m0_587098992 天前
Qt,二进制文件读写建议
开发语言·qt
秋田君2 天前
QT_QT信号与槽机制
开发语言·数据库·qt
辞旧 lekkk2 天前
【Qt】 系统相关:事件与定时器
开发语言·qt·学习·萌新
HAPPY酷2 天前
【ROS2】VMware + Docker 运行 Gazebo 崩溃与“假死”排查实录
qt·docker·容器
初阳7852 天前
【Qt】界面优化(1)——QSS
开发语言·qt
Irissgwe2 天前
第六章 界面优化
qt
必须得开心呀2 天前
QT解决中文乱码问题
开发语言·qt
熊猫_豆豆2 天前
QT6 Android C++ 自制美观闹钟
android·c++·qt·闹钟