QT中的事件过滤器
处理事件过滤
bool eventFilter(QObject *watched, QEvent *event)
{
if(watched == ui->lineEdit||watched == ui->lineEdit_2||
ui->lineEdit_3||ui->lineEdit_4||ui->lineEdit_5||ui->lineEdit_6)
{
if(event->type() == QEvent::KeyPress)
{
QKeyEvent*e = static_cast<QKeyEvent*>(event);
if(e->key() == Qt::Key_Space)
{
focusNextChild();
}
return false; // reture false 不会拦截其他事件,return true 会拦截其他事件
}
}
return QWidget::eventFilter(watched,event);
}
安装事件过滤器
ui->lineEdit->installEventFilter(this);
ui->lineEdit_5->installEventFilter(this);
ui->lineEdit_2->installEventFilter(this);
ui->lineEdit_3->installEventFilter(this);
ui->lineEdit_4->installEventFilter(this);
ui->lineEdit_6->installEventFilter(this);
6个lineEdit 输入数字后按空格键焦点会跳到下一个控件