QT中的event事件解析
2016-11-25 21:39
316 查看
在QT中使用一个对象表示一个事件,继承自QEvent类。需要说明的是事件与信号是不相同的。在每个程序的main()函数的最开始都会调用QApplication类的exec()函数,它会使Qt应用程序进入事件循环,这样就可以使应用程序在运行时候接受发生的各种事件,一旦有事件发生,Qt便会创建一个相应的QEvent子类对象的事件来表示,然后传递给相应的QObject对象或其子对象。
下面我们创建一个Qt Gui工程,基类选择QWidget,还是直接上代码,个别地方我会注释讲解的。
mian.cpp
#include <QtGui/QApplication>
#include "widget.h"
#include <QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
Widget w;
w.show();
return a.exec();
}
mylineedit.h
#ifndef MYLINEEDIT_H
#define MYLINEEDIT_H
#include <QLineEdit>
class MyLineEdit : public QLineEdit
{
Q_OBJECT
public:
explicit MyLineEdit(QWidget *parent = 0);
bool event(QEvent *event);
protected:
void keyPressEvent(QKeyEvent *event);
signals:
public slots:
};
#endif // MYLINEEDIT_H
在这个文件中我们声明了一个keyPressEvent按键事件处理函数,这个函数在QlineEdit类中已经被声明了,只是是一个虚函数,所以这里我们可以重新定义。
myLineEdit.cpp
#include "mylineedit.h"
#include <QKeyEvent>
#include <QDebug>
MyLineEdit::MyLineEdit(QWidget *parent) :
QLineEdit(parent)
{
}
bool MyLineEdit::event(QEvent *event)
{
if(event->type()==QEvent::KeyPress)
qDebug() << tr("MyLineEdit event functions");
return QLineEdit::event(event); /* exec QLineEdit default event() funcion dealwith */
}
void MyLineEdit::keyPressEvent(QKeyEvent *event)
{
qDebug() << tr("MyLineEdit键盘按下事件");
QLineEdit::keyPressEvent(event); /* exec QLineEdit default event dealwith */
event->ignore(); /* ignore the event */
}
myLineEdit.cpp中构造函数没有做任何操作,MyLineEdit::keyPressEvent函数就是我们定义的按键事件函数,当按下按键后就会调用该函数,QLineEdit::keyPressEvent(event);函数意思是执行默认操作,因为该事件调用了这个函数,而本身事件就被忽略了,event->ignore();就是忽略这个事件,然后在widget窗口如果定义了这个keyPressEvent,也就可以触发这个事件。 MyLineEdit::event是一个大类触发事件,按键事件只是它的一个子类。
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class MyLineEdit;
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
bool eventFilter(QObject *obj, QEvent *event);
protected:
void keyPressEvent(QKeyEvent *event);
private:
Ui::Widget *ui;
MyLineEdit *lineEdit;
};
#endif // WIDGET_H
widget类中声明了一个事件过滤函数eventFilter,有这个函数后,我们可以筛选我们想要的事件。创建了一个指向MyLineEdit类型的一个lineEdit指针。
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include "mylineedit.h"
#include <QKeyEvent>
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
lineEdit =new MyLineEdit(this);
lineEdit->move(100,100);
lineEdit->installEventFilter(this);
}
Widget::~Widget()
{
delete ui;
}
bool Widget::eventFilter(QObject *obj, QEvent *event)
{
if(obj==lineEdit)
{
if(event->type()==QEvent::KeyPress)
qDebug() << tr("Widget event Filter");
}
return QWidget::eventFilter(obj,event);
}
void Widget::keyPressEvent(QKeyEvent *event)
{
qDebug() << tr("Widget键盘按下事件");
}
设计界面和事件的打印顺序如下:
文章出自:Linux_Google
下面我们创建一个Qt Gui工程,基类选择QWidget,还是直接上代码,个别地方我会注释讲解的。
mian.cpp
#include <QtGui/QApplication>
#include "widget.h"
#include <QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
Widget w;
w.show();
return a.exec();
}
mylineedit.h
#ifndef MYLINEEDIT_H
#define MYLINEEDIT_H
#include <QLineEdit>
class MyLineEdit : public QLineEdit
{
Q_OBJECT
public:
explicit MyLineEdit(QWidget *parent = 0);
bool event(QEvent *event);
protected:
void keyPressEvent(QKeyEvent *event);
signals:
public slots:
};
#endif // MYLINEEDIT_H
在这个文件中我们声明了一个keyPressEvent按键事件处理函数,这个函数在QlineEdit类中已经被声明了,只是是一个虚函数,所以这里我们可以重新定义。
myLineEdit.cpp
#include "mylineedit.h"
#include <QKeyEvent>
#include <QDebug>
MyLineEdit::MyLineEdit(QWidget *parent) :
QLineEdit(parent)
{
}
bool MyLineEdit::event(QEvent *event)
{
if(event->type()==QEvent::KeyPress)
qDebug() << tr("MyLineEdit event functions");
return QLineEdit::event(event); /* exec QLineEdit default event() funcion dealwith */
}
void MyLineEdit::keyPressEvent(QKeyEvent *event)
{
qDebug() << tr("MyLineEdit键盘按下事件");
QLineEdit::keyPressEvent(event); /* exec QLineEdit default event dealwith */
event->ignore(); /* ignore the event */
}
myLineEdit.cpp中构造函数没有做任何操作,MyLineEdit::keyPressEvent函数就是我们定义的按键事件函数,当按下按键后就会调用该函数,QLineEdit::keyPressEvent(event);函数意思是执行默认操作,因为该事件调用了这个函数,而本身事件就被忽略了,event->ignore();就是忽略这个事件,然后在widget窗口如果定义了这个keyPressEvent,也就可以触发这个事件。 MyLineEdit::event是一个大类触发事件,按键事件只是它的一个子类。
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class MyLineEdit;
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
bool eventFilter(QObject *obj, QEvent *event);
protected:
void keyPressEvent(QKeyEvent *event);
private:
Ui::Widget *ui;
MyLineEdit *lineEdit;
};
#endif // WIDGET_H
widget类中声明了一个事件过滤函数eventFilter,有这个函数后,我们可以筛选我们想要的事件。创建了一个指向MyLineEdit类型的一个lineEdit指针。
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include "mylineedit.h"
#include <QKeyEvent>
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
lineEdit =new MyLineEdit(this);
lineEdit->move(100,100);
lineEdit->installEventFilter(this);
}
Widget::~Widget()
{
delete ui;
}
bool Widget::eventFilter(QObject *obj, QEvent *event)
{
if(obj==lineEdit)
{
if(event->type()==QEvent::KeyPress)
qDebug() << tr("Widget event Filter");
}
return QWidget::eventFilter(obj,event);
}
void Widget::keyPressEvent(QKeyEvent *event)
{
qDebug() << tr("Widget键盘按下事件");
}
设计界面和事件的打印顺序如下:
文章出自:Linux_Google
相关文章推荐
- javascript event 事件解析
- C#事件(event)解析
- javascript event 事件解析
- [转]C#事件(event)解析
- C#事件(event)解析
- javascript event 事件解析
- QT源码解析(四)剖析Qt的事件机制原理
- C#事件(event)解析
- javascript event 事件解析
- C#事件(event)解析
- C#事件(event)解析
- C#事件(event)解析 -- 新手必看
- C#事件(event)解析
- C#事件(event)解析
- C#事件(event)解析
- javascript event 事件解析
- C#事件(event)解析
- C#事件(event)解析(转载)
- JS - event 事件解析收藏
- C#事件(event)解析