QObject的event函数就可以改写对消息的处理

一个既自己处理Tab事件又自己处理某些按键事件,然后将其它不需自己处理的事件转发给基类处理:

bool MyWidget::event(QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
QKeyEvent *ke = static_cast<QKeyEvent *>(event);
if (ke->key() == Qt::Key_Tab) {
// 特别的Tab操作
return true;
}
} else if (event->type() == MyCustomEventType) {
MyCustomEvent *myEvent = static_cast<MyCustomEvent *>(event);
// 自定义事件处理
return true;
}

return QWidget::event(event);
}

注意:对没有处理的事件仍调用QWidget::event(),并返回该基类调用的返回值以指示事件是否被处理了;若返回true,则将会禁止将该事件再发往其它对象。

http://blog.csdn.net/liang19890820/article/details/51932033

原文地址:https://www.cnblogs.com/findumars/p/5702153.html