QT的QObject对象提供了一个方法:void installEventFilter ( QObject * filterObj );
这里的参数filterObj也是一个QObject, 这个对象具体实施过滤事件的,它要重载
QObject的虚函数: virtual bool eventFilter( QObject * watched, QEvent * event );
第一个参数就是需要过滤事件的对象, 第二个参数就是传入的事件。
可以用static_cast转换成QMouseEvent或QKeyEvent。
这个函数的返回值false代表不过滤,还会传给上层, true表示吃掉了,不返回给上层了。
下面的示例程序就是对鼠标右键和ESC键的过滤:
类定义:
class FooBrowser : public QWebView
{
Q_OBJECT
public:
FooBrowser(QWidget* parent = 0);
Q_SIGNALS:
protected:
bool eventFilter(QObject *obj, QEvent *ev);
};
类实现:
FooBrowser::FooBrowser(QWidget* parent) : QWebView(parent)
{
// For WebView
installEventFilter(this);
// For WebPage
page()->installEventFilter(this);
// For WebFrame
page()->mainFrame()->installEventFilter(this);
}
bool FooBrowser::eventFilter(QObject *obj, QEvent *ev)
{
if (ev->type() == QEvent::MouseButtonPress ||
ev->type() == QEvent::MouseButtonRelease ||
ev->type() == QEvent::MouseButtonDblClick){
QMouseEvent *mouseev = static_cast<QMouseEvent *>(ev);
if (mouseev->button() == Qt::RightButton) {
qDebug("Eat right button!");
return true;
}
}
if (ev->type() == QEvent::KeyPress ||
ev->type() == QEvent::KeyRelease) {
QKeyEvent *keyev = static_cast<QKeyEvent *>(ev);
qDebug("Eat key %d", keyev->key());
if (keyev->key() == Qt::Key_Escape) {
reload();
}
return true;
}
return false;
}
// 测试主程序
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FooBrowser *b = new FooBrowser;
b->setWindowFlags(Qt::FramelessWindowHint);
b->setGeometry(0,0,1280,720);
b->load(QUrl("http://www.google.com"));
b->show();
return a.exec();
}