在Qt中,事件是从抽象QEvent类派生的对象,它们表示发生在应用程序内部或由于应用程序需要了解的外部活动而发生的事情。 QObject子类的任何实例都可以接收和处理事件,所以,要使用事件机制,也得直接或者间接继承QObject
一、事件的处理
当事件发生时,Qt通过构造适当的QEvent子类的实例来创建事件对象,并通过调用event()函数将事件传递到QObject的特定实例或其子类中。event()函数根据所传递事件的类型,将为特定类型的事件调用事件处理程序,而且event()函数根据事件是被接受还是被忽略做出对应的动作。
virtual bool QObject::event(QEvent *e)
该虚函数接收到对象的事件,如果识别并处理了事件e,则应返回true。
可以重新实现event()函数以自定义对象的行为。
示例
class MyClass : public QWidget
{
Q_OBJECT
public:
MyClass(QWidget *parent = 0);
~MyClass();
bool event(QEvent* ev)
{
if (ev->type() == QEvent::PolishRequest) {
// overwrite handling of PolishRequest if any
doThings();
return true;
} else if (ev->type() == QEvent::Show) {
// complement handling of Show if any
doThings2();
QWidget::event(ev);//显示相关的事件依旧要调用父类的event
return true;
}
// Make sure the rest of events are handled
return QWidget::event(ev);//确保为所有未处理的事件调用父类的event函数处理。
}
};
某些事件做了特殊处理外,对于其他不关注的事件,依旧调用父类的event函数处理
事件有很多种,一些关于鼠标和键盘的事件,如QMouseEvent和QKeyEvent。 另外还有定时器事件,如QTimerEvent。每个事件都有一个关联的类型,该类型在QEvent :: Type中定义,见https://doc.qt.io/qt-5/qevent.html
其中,定时器事件QTimerEvent会定期发送给已启动的一个或多个定时器对象。 每个定时器都有唯一的标识符。 使用QObject::startTimer()启动定时器。
int QObject::startTimer(int interval, Qt::TimerType timerType = Qt::CoarseTimer)
该函数启动定时器并返回定时器的标识符,返回零时,定时器启动失败。
第一个参数interval表示定时器事件发送的频率,单位是ms,如果interval为0,那么当没有窗口系统事件时,定时器只会发一次,如果有的话,会一直调用interval为0的定时器
当调用killTimer时,将对应序号的定时器停掉
void QObject::killTimer(int id)
当定时器事件发生时,QObject::timerEvent()被调用,用来接收计时器事件。
virtual void QObject::timerEvent(QTimerEvent *event)
可以在子类中重新实现timerEvent,以接收对象的定时器事件。
示例
class MyObject : public QObject
{
Q_OBJECT
public:
MyObject(QObject *parent = 0);
protected:
void timerEvent(QTimerEvent *event);
};
MyObject::MyObject(QObject *parent)
: QObject(parent)
{
startTimer(50); // 50-millisecond timer
startTimer(1000); // 1-second timer
startTimer(60000); // 1-minute timer
using namespace std::chrono;
startTimer(milliseconds(50));
startTimer(seconds(1));
startTimer(minutes(1));
}
void MyObject::timerEvent(QTimerEvent *event)
{
cout<< "Timer ID:" << event->timerId()<<endl;
}
上述代码按照startTimer的调用顺序一次对定时器进行id的分配,当定时器时间到,调用timerEvent,定时器的序号从1开始
如果需要对特定的事件进行重新处理,可以重写父类的event函数或者对父类的一些具体的事件处理函数进行重写
示例
void MyCheckBox::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
// handle left mouse button here
} else {
// pass on other buttons to base class
QCheckBox::mousePressEvent(event);
}
}
上述代码就对mousePressEvent进行了重新,左键事件会有特殊处理,而其按键的处理方法则依旧调用父类中的mousePressEvent
注意:对于不关注的事件,要调用父类的同名的成员函数处理事件
二、事件过滤器
有时,一个对象需要查看并可能拦截传递给另一个对象的事件,QObject :: installEventFilter()函数可以启用此功能,如果在单个对象上安装了多个事件过滤器,则首先激活最后安装的过滤器。
void QObject::installEventFilter(QObject *filterObj)
该函数的主要功能就是在QT对象filterObj上安装事件过滤器,在事件发送给QT对象之前,事件过滤器可以接收发送到该对象的所有事件。 进而可以决定是否停止将事件发送给对象。
事件过滤器通过eventFilter()函数接收事件。 如果要过滤事件(即停止将事件发送给对象),则eventFilter()函数返回true,之后,对象将无法收到该事件; 否则返回false。表示目标对象可以收到该事件并进一步处理该事件
virtual bool QObject::eventFilter(QObject *watched, QEvent *event)
如果在eventFilter()函数中删除接收对象watched,请确保返回true。 如果返回false,则Qt会将事件发送到已删除的对象,程序将崩溃。
过滤对象(就是installEventFilter函数的参数指向的对象)必须与此对象watched在同一线程中。 如果filterObj在其他线程中,则eventFilter不执行任何操作。
QObject :: removeEventFilter()函数可以删除此功能
void QObject::removeEventFilter(QObject *obj)
将对象obj上的事件过滤器删除。 如果未安装事件过滤器,则忽略该请求。
销毁对象后,将自动删除该对象的所有事件过滤器。即使在激活事件过滤器期间(即从eventFilter()函数执行中),也可以删除事件过滤器。
示例
class MainWindow : public QMainWindow
{
public:
MainWindow();
protected:
bool eventFilter(QObject *obj, QEvent *ev);
private:
QTextEdit *textEdit;
};
MainWindow::MainWindow()
{
textEdit = new QTextEdit;
setCentralWidget(textEdit);
textEdit->installEventFilter(this);
}
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (obj == textEdit) {
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
cout << "Ate key press" << keyEvent->key()<<endl;
return true;
} else {
return false;
}
} else {
// pass the event on to the parent class
return QMainWindow::eventFilter(obj, event);
}
}
上述代码就实现了一个不接受目标对象textEdit的KeyPress事件的事件过滤器,当敲击键盘时,会有log输出,提示按键没有被发送,对应的键盘内容就不会在textEdit中显示
也就是说,事件处理常用的方法有三种:1、重写paintEvent(),mousePressEvent()等特定的事件处理程序。2、重写父类中的event()。3、在对象上安装事件过滤器
参考
https://doc.qt.io/qt-5/qobject.html#event
https://doc.qt.io/qt-5/qobject.html#eventFilter
https://doc.qt.io/qt-5/qobject.html#timerEvent
https://doc.qt.io/qt-5/qobject.html#startTimer
https://doc.qt.io/qt-5/qobject.html#installEventFilter
https://doc.qt.io/qt-5/qobject.html#removeEventFilter
欢迎大家评论交流,作者水平有限,如有错误,欢迎指出