【QT常用技术讲解】事件处理

前言

        本文内容结合本人引用的代码,在QT项目的开发框架提供下载的源代码中可以找到。另外,本人风格倾向于用最常用(最高效)的方式去实现功能,而非学术性的全面讲解。

概述

        所有的QT控件都继承自QObject,QObject提供了事件处理机制。在界面交互过程中,最常见的是按钮的鼠标单击事件、双击事件,而QT仅默认提供了鼠标单击事件,鼠标双击事件在默认列表中是不存在的,此时就需要通过事件处理(事件过滤器)来实现。

功能讲解

按钮单击

以【基础信息】菜单的【添加】按钮功能为例:

右键->选择->转到槽...->clicked()->单击完成,之后即可在头文件tab_basemsg.h中查看到新增了槽

函数:

public slots:
    void on_basemsgadd_pushbutton_clicked();

在tab_basemsg.cpp实现功能即可:

//添加功能
void tab_basemsg::on_basemsgadd_pushbutton_clicked()
{
    qDebug() << __LINE__ << __FUNCTION__;
    //int rowCount = ui->usbchangetable->rowCount();
    stBasemsg st_basemsg=stBasemsg();
    qDebug() << __LINE__ << "on_usbchangeButton_clicked";
    basemsgDialg->setModal(false);
    basemsgDialg->setWindowTitle("添加");
    //basemsgDialg->setFixedSize(600,560);
    basemsgDialg->open();
    basemsgDialg->init(0,st_basemsg);
    basemsgDialg->exec();
}

标签单击

QLabel默认是没有单击事件的,需要通过事件过滤器来实现效果。首页工具栏上的【字段管理】和【用户管理】都是用图片填充的QLabel。

在主界面的结构函数中需要通过控件引用installeventfilter(this)来安装事件过滤器;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    m_last_width=920;
    m_last_height=600;
    ui->setupUi(this);
    m_Mac=CommFuntion::Get().getLocalMac();
    qDebug() << "mac:" << m_Mac;
    m_Cpuid=CommFuntion::Get().getWindowcpuid();
    qDebug() << "win cpuid:" << m_Cpuid;
    m_Compressid=CommFuntion::Get().getCompressid(m_Cpuid.toUtf8().constData());
    qDebug() << "win comid:" << m_Compressid;

    ui->bar_field->installEventFilter(this);//安装事件过滤器,qlabel没有默认自带鼠标点击事件,通过事件过滤器捕获
    ui->bar_user->installEventFilter(this);//安装事件过滤器,qlabel没有默认自带鼠标点击事件,通过事件过滤器捕获
    ...
}

然后需要详细的写事件处理函数eventfilter(),如下面的代码段,事件处理函数只有一个,同一个单击事件,通过watched来识别是哪个控件的事件。

bool MainWindow::eventFilter(QObject *watched, QEvent *event) {
    if(event->type() == QEvent::MouseButtonPress){
        qDebug() << "QEvent::MouseButtonPress";
        if (watched == ui->bar_field) {//不同的bar调用的对话框不一样,需要watched识别被点击的控件
            QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
            if (mouseEvent->button() == Qt::LeftButton) {//捕获鼠标单击事件
                qDebug() << "Label clicked==>field !";
                fieldsmgt->setModal(false);
                fieldsmgt->setWindowTitle("字段管理");
                fieldsmgt->open();
                fieldsmgt->exec();
                return true; // 吸收事件
            }
        }else if(watched == ui->bar_user) {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
            if (mouseEvent->button() == Qt::LeftButton) {//捕获鼠标单击事件
                qDebug() << "Label clicked==>user !";
                usermgt->setModal(false);
                usermgt->setWindowTitle("用户管理");
                usermgt->open();
                usermgt->exec();
                return true; // 吸收事件
            }
        }
    }
    return QMainWindow::eventFilter(watched, event); // 其他事件交给默认处理
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值