标签:style blog ar div 代码 log sp on line
1. 在目标对象上调用installEventFilter(),将监测对象注册到目标对象上.
2. 在监测对象的eventFilter()方法里处理目标对象的事件.
在ctor里注册监测对象是一个好地方:
CustomerInfoDialog::CustomerInfoDialog(QWidget *parent) :QDialog(parent)
{
...
firstNameEdit->installEventFilter(this);
lastNameEdit->installEventFilter(this);
cityEdit->installEventFilter(this);
phoneNumberEdit->installEventFilter(this);
}
一旦event Filter注册了, 发送到firstNameEdit, lastNameEdit, cityEdit和phoneNumberEdit的事件在被发送到原来的目的地之前, 会先发到CustomerInfoDialog的eventFilter()函数.
这是接收这些事件的eventFilter()函数:
bool CustomerInfoDialog::eventFilter(QObject *target, QEvent *event)
{
if (target == firstnameEdit || target == lastNameEdit
|| target == cityEdit || target == phoneNumberEdit)
{
if(event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Space)
{
focusNextChild();
return true;
}
}
}
return QDialog::eventFilter(target, event);
}
标签:style blog ar div 代码 log sp on line
原文地址:http://www.cnblogs.com/lvdongjie/p/3947060.html