• Qt事件学习


    一、创建Qt gui应用对应的源码:

    点击(此处)折叠或打开

    1. //mylineedit.h
    2. #ifndef MYLINEEDIT_H
    3. #define MYLINEEDIT_H

    4. #include <QWidget>
    5. #include <QLineEdit>

    6. class MyLineEdit : public QLineEdit
    7. {
    8. public:
    9.     explicit MyLineEdit(QWidget *parent = 0);
    10. protected:
    11.     void keyPressEvent(QKeyEvent *event);
    12. };

    13. #endif // MYLINEEDIT_H


    14. //mylineedit.cpp
    15. #include "mylineedit.h"
    16. #include <QLineEdit>
    17. #include <QDebug>
    18. #include <QKeyEvent>

    19. MyLineEdit::MyLineEdit(QWidget *parent):QLineEdit(parent)
    20. {

    21. }

    22. void MyLineEdit::keyPressEvent(QKeyEvent *event)
    23. {
    24.     qDebug() << QObject::tr("MyLineEdit键盘按下事件");
    25. }


    此时只会出现“MyLineEidt键盘按下事件”。

    二、第二次修改

    在mylineedit.cpp中keyPressEvent()添加

    1. event->ignore(); //忽略该事件

    结果:多出现了"Widget键盘按下事件",并且lineedit无法输入了。

    三、第三次修改

    在mylineedit.h中添加public函数:

    1. bool event(QEvent *event);

    然后定义:

    1. bool MyLineEdit::event(QEvent *event)
    2. {
    3.     if(event->type() == QEvent::KeyPress)
    4.         qDebug()<<QObject::tr("MylineEdit的event()函数");
    5.     return QLineEdit::event(event);
    6. }

    四、第四次修改

    widget.h中public申明:

    1. bool eventFilter(QObject *obj,QEvent *event);

    widget.cpp构造函数增加代码,并增加事件过滤器函数的定义:

    1. lineEdit->installEventFilter(this); //在widget上为lineEdit安装事件过滤器

    2. bool Widget::eventFilter(QObject *watched, QEvent *event)
    3. {
    4.     if(watched==lineEdit) {
    5.         if(event->type()==QEvent::KeyPress)
    6.             qDebug()<<QObject::tr("Widget的事件过滤器");
    7.     }
    8.     return QWidget::eventFilter(watched, event);
    9. }


    五、最后的源码:
    mylineedit:

    1. //mylineedit.h
    2. #ifndef MYLINEEDIT_H
    3. #define MYLINEEDIT_H

    4. #include <QWidget>
    5. #include <QLineEdit>

    6. class MyLineEdit : public QLineEdit
    7. {
    8. public:
    9.     explicit MyLineEdit(QWidget *parent = 0);
    10.     bool event(QEvent *event);

    11. protected:
    12.     void keyPressEvent(QKeyEvent *event);
    13. };

    14. #endif // MYLINEEDIT_H


    15. //mylineedit.cpp
    16. #include "mylineedit.h"
    17. #include <QLineEdit>
    18. #include <QDebug>
    19. #include <QKeyEvent>

    20. MyLineEdit::MyLineEdit(QWidget *parent):QLineEdit(parent)
    21. {

    22. }

    23. bool MyLineEdit::event(QEvent *event)
    24. {
    25.     if(event->type() == QEvent::KeyPress)
    26.         qDebug()<<QObject::tr("MylineEdit的event()函数");
    27.     return QLineEdit::event(event);
    28. }

    29. void MyLineEdit::keyPressEvent(QKeyEvent *event)
    30. {
    31.     qDebug() << QObject::tr("MyLineEdit键盘按下事件");
    32.     QLineEdit::keyPressEvent(event);
    33.     event->ignore();
    34. }

    midget:

    1. //wdiget.h
    2. #ifndef WIDGET_H
    3. #define WIDGET_H

    4. #include <QWidget>

    5. class MyLineEdit;

    6. namespace Ui {
    7. class Widget;
    8. }

    9. class Widget : public QWidget
    10. {
    11.     Q_OBJECT

    12. public:
    13.     explicit Widget(QWidget *parent = 0);
    14.     ~Widget();
    15.     bool eventFilter(QObject *watched, QEvent *event);
    16. protected:
    17.     void keyPressEvent(QKeyEvent *event);
    18. private:
    19.     Ui::Widget *ui;
    20.     MyLineEdit *lineEdit;
    21. };

    22. #endif // WIDGET_H


    23. //widget.cpp
    24. #include "widget.h"
    25. #include "ui_widget.h"
    26. #include "mylineedit.h"
    27. #include <QKeyEvent>
    28. #include <QDebug>

    29. Widget::Widget(QWidget *parent) :
    30.     QWidget(parent),
    31.     ui(new Ui::Widget)
    32. {
    33.     lineEdit = new MyLineEdit(this);
    34.     lineEdit->move(100, 100);
    35.     lineEdit->installEventFilter(this); //在widget上为lineEdit安装事件过滤器
    36.     ui->setupUi(this);
    37. }

    38. Widget::~Widget()
    39. {
    40.     delete ui;
    41. }

    42. bool Widget::eventFilter(QObject *watched, QEvent *event)
    43. {
    44.     if(watched==lineEdit) {
    45.         if(event->type()==QEvent::KeyPress)
    46.             qDebug()<<QObject::tr("Widget的事件过滤器");
    47.     }
    48.     return QWidget::eventFilter(watched, event);
    49. }

    50. void Widget::keyPressEvent(QKeyEvent *event)
    51. {
    52.     qDebug()<<QObject::tr("Widget键盘按下事件");
    53. }










    无欲速,无见小利。欲速,则不达;见小利,则大事不成。
  • 相关阅读:
    列表常用的方法
    python 三级联动
    tkinter--gui工具:MD5
    python PublicMethod方法如下<LoggerBase有使用>
    python config/config.ini 配置文件如下
    python browser engine封装
    python 页面基类 Page封装 →driver层的封装(最底层的封装Page)
    Eclipse配置maven环境
    appium 多个测试用例<多个 **.py文件>,只执行一次app <Python 3.8.1>
    python3版本使用weditor报错,解决方案
  • 原文地址:https://www.cnblogs.com/ch122633/p/7363233.html
Copyright © 2020-2023  润新知