新建一个项目:06Event

新建cpp文件

06Event.pro

HEADERS
+=
\

MyWidget.h

SOURCES
+=
\

MyWidget.cpp

QT
+=
widgets gui

MyWidget.h

#ifndef
MYWIDGET_H

#define
MYWIDGET_H

#include
<QWidget>

#include
<QPushButton>

#include
<QLineEdit>

class
MyWidget
:
public
QWidget

{

Q_OBJECT

public:

explicit
MyWidget(QWidget
*parent
);

bool
event(QEvent
*);

void
mousePressEvent(QMouseEvent
*);

void
mouseReleaseEvent(QMouseEvent
*);

void
mouseMoveEvent(QMouseEvent
*);

/*不要用,因为它默认调用两次mousePressEvent*/

//void
mouseDoubleClickEvent(QMouseEvent
*);

void
keyPressEvent(QKeyEvent
*);

void
keyReleaseEvent(QKeyEvent
*);

void
closeEvent(QCloseEvent
*);

//void
showEvent(QShowEvent
*);

//void
hideEvent(QHideEvent
*);

void
paintEvent(QPaintEvent
*);

QPushButton*
button;

QLineEdit*
edit;

signals:

public
slots:

void
slotButtonClicked();

};

#endif
//
MYWIDGET_H

MyWidget.cpp

#include "MyWidget.h"
#include <QApplication>
#include <QEvent>
#include <QDebug>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QVBoxLayout>
#include <QPainter>
 
MyWidget::MyWidget(QWidget *parent) :
    QWidget(parent)
{
//  QVBoxLayout* lay = new QVBoxLayout(this);
#if 0
    QPushButton* button2;
    lay->addWidget(button = new QPushButton("OK", this));
    lay->addWidget(button2 = new QPushButton("Button2"));
    //设置它为默认的焦点,当点击tab键之后可以切换焦点
    button->setDefault(true);
 
    // 鼠标不需要按下,mouseMove就能得到调用
    this->setMouseTracking(true);
 
    connect(button2, SIGNAL(clicked()), this, SLOT(slotButtonClicked()));
    connect(button, SIGNAL(clicked()), this, SLOT(slotButtonClicked()));
#endif
//    lay->addWidget(edit = new QLineEdit());
//    connect(edit, SIGNAL(returnPressed()), this, SLOT(slotButtonClicked()));
}
 
//通过这一句实现点击按钮的时候获得按钮上的text()内容
void MyWidget::slotButtonClicked()
{
    QLineEdit* button = (QLineEdit*)sender();
    qDebug() << button->text();
}
 
/* QApplication先得到->具体应该处理的窗口::event()->event()根据消息类型来调用具体的虚函数 */
/* 1)可以重载具体的虚函数,来实现对消息的响应
   2)可以重载event函数,用来处理或者截取消息 */
 
/* 截取消息 */
bool MyWidget::event(QEvent *ev)
{
    // 鼠标消息被截断
   // if(ev->type() == QEvent::MouseButtonPress)
     //   return true;
    ev->accept();
 
    return QWidget::event(ev);
}
 
/**
 * @brief MyWidget::closeEvent 窗口关闭的事件
 */
void MyWidget::closeEvent(QCloseEvent *)
{
    qDebug() << "closeEvent";
}
 
void MyWidget::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    //通过下面的方式实现画线
    p.drawLine(QPoint(0, 0), QPoint(100, 100));
}
 
/**
 * @brief MyWidget::mousePressEvent 鼠标按下的事件
 * @param ev
 */
void MyWidget::mousePressEvent(QMouseEvent *ev)
{
#if 0
    QPoint pt = ev->pos();
    qDebug() << pt;
    //如果鼠标按下的是左键的处理
    if(ev->button() == Qt::LeftButton){}
 
    //如果按下的Shift键了
    if(ev->modifiers() == Qt::ShiftModifier)
    {
        qDebug() << "shift press";
    }
#endif
    //改进办法:先判断是否有左键,然后如果判断是否还按了Ctrl键
    if(ev->button() == Qt::LeftButton)
    {
        if(ev->modifiers() == Qt::ControlModifier)
        {
            // handle with Control;
            return;
        }
        // handle2 without control;
    } else {}
}
 
/**
 * @brief MyWidget::mouseReleaseEvent 鼠标释放的按键
 */
void MyWidget::mouseReleaseEvent(QMouseEvent *){}
 
/**
 * @brief MyWidget::mouseMoveEvent 鼠标移动的事件
 */
void MyWidget::mouseMoveEvent(QMouseEvent *)
{
    static int i=0;
    qDebug() << "mouse move"<< i++;
}
 
/**
 * @brief MyWidget::keyPressEvent 鼠标按下的事件,通过这个可以获得按下的键
 * @param ev
 */
void MyWidget::keyPressEvent(QKeyEvent *ev)
{
    ev->modifiers();
    int key = ev->key();
    qDebug() << key;
    char a = key;
    qDebug() << (char)a;
}
 
/**
 * @brief MyWidget::keyReleaseEvent 按键释放的事件
 */
void MyWidget::keyReleaseEvent(QKeyEvent *){}
 
int main(int argc, char* argv[])
{
    QApplication app(argc, argv);
 
    MyWidget w;
    w.show();
 
    return app.exec();
}
运行结果:


QT消息过滤器

EventFilter.pro

HEADERS
+=
\

MyWidget.h
\

MyApplication.h

SOURCES
+=
\

MyWidget.cpp
\

MyApplication.cpp

QT
+=
widgets gui

MyWidget.h

#ifndef MYWIDGET_H
#define MYWIDGET_H
 
#include <QWidget>
#include <QPushButton>
class MyWidget : public QWidget
{
    Q_OBJECT
public:
    explicit MyWidget(QWidget *parent);
 
    QPushButton* _button;
    bool eventFilter(QObject *, QEvent *);
    bool event(QEvent *);
signals:
 
public slots:
 
};
 
#endif // MYWIDGET_H

MyWidget.cpp

#include "MyWidget.h"
#include <QPushButton>
#include <QEvent>
#include "MyApplication.h"
#include <QDebug>
#include <QApplication>
 
MyWidget::MyWidget(QWidget *parent) :
    QWidget(parent)
{
    QPushButton* button;
 
    button = new QPushButton("This button", this);
    connect(button, SIGNAL(clicked()), this, SLOT(close()));
    _button = button;
 
    /*button给自己安装了一个消息过滤器,那么经过button的消息,
     * 都先要调用它的过滤器的eventFilter函数*/
    button->installEventFilter(this);
}
 
bool MyWidget::eventFilter(QObject *o, QEvent *e)
{
#if 0
    if(0 == (QObject*)_button &&(
                e->type() == QEvent::MouseButtonRelease ||
                e->type() == QEvent::MouseButtonDblClick ||
                e->type() == QEvent::MouseButtonPress)) {
        return true;
    }
#endif
 
    return QWidget::eventFilter(o,e);
}
 
bool MyWidget::event(QEvent *e)
{
    if(e->type() == QEvent::User)
    {
        qDebug() << "User event is comming";
    }
    return QWidget::event(e);
}
 
int main(int argc,char *argv[])
{
    MyApplication app(argc,argv);
    MyWidget w;
    w.show();
 
    //发送一个Event给MyWidget
    qDebug() << "begin send";
    app.postEvent(&w,new QEvent(QEvent::User));
    qDebug() << "end send";
 
    return app.exec();
}

MyApplication.h

#ifndef MYAPPLICATION_H
#define MYAPPLICATION_H
 
#include <QApplication>
 
class MyApplication : public QApplication
{
    Q_OBJECT
public:
    MyApplication(int argc,char *argv[]):QApplication(argc,argv)
    {}
    bool notify(QObject *,QEvent *);
 
signals:
 
public slots:
 
};
 
#endif // MYAPPLICATION_H

MyApplication.cpp

#include "MyApplication.h"
#include <QEvent>
 
#include <QDebug>
bool MyApplication::notify(QObject *o, QEvent *e)
{
    if(this->topLevelWidgets().count()>0)
    {
        QWidget* mainWnd = this->topLevelWidgets().at(0);
        if(o==(QObject*)mainWnd && e->type() == QEvent::MouseButtonPress)
        {
            // do ...
            qDebug() << "mainwnd is clicked";
        }
    }
 
    return QApplication::notify(o, e);
}

运行结果:

最新文章

  1. jQuery Mockjax插件使用心得
  2. 第三天--html列表
  3. C#程序开发中经常遇到的10条实用的代码
  4. html总集
  5. Verilog学习笔记简单功能实现(八)...............异步FIFO
  6. NetBeans IDE配置调试
  7. ARC指南1 - strong和weak指针
  8. 2014多校第二场1011 || HDU 4882 ZCC Loves Codefires (贪心)
  9. lintcode :最长公共前缀
  10. UI:target-action设计模式、手势识别器
  11. 关于iOS6应用中第三方类库不支持armv7s的问题解决
  12. LayoutInflater 与 inflate
  13. C#Excel导出导入
  14. Java枚举储存的一种索引实现方式
  15. cookie和sesssion
  16. 配置web pack loader 报错:Module build failed: Error: The node API for `babel` has been moved to `babel-core`.
  17. tomcat 修改内存配置
  18. SpringBoot 之jsp
  19. 一本通1587【例 3】Windy 数
  20. Bootstrap3.0学习第二轮(栅格系统原理)

热门文章

  1. [HAOI2007]上升序列
  2. [HNOI2004]树的计数
  3. ●POJ 2007 Scrambled Polygon
  4. poj 2886 线段树+反素数
  5. Android通过聚合数据API实现天气预报
  6. Python中模块之os的功能介绍
  7. C语言程序设计第二次作业--顺序结构
  8. Linux 下 HTTP连接超时
  9. Linux下常用设置文件和文件夹读写权限操作
  10. 使用linux部署tomcat项目