bool IpAddressWidget::eventFilter(QObject *object, QEvent *event) { //如果object不是四个输入框,则返回 int nowBox = -1; for(int i=0; i<4; i++) { if( object == m_SpinBox[i] ) nowBox = i; } if( -1 == nowBox ) return false; QSpinBox *spinBox = dynamic_cast<QSpinBox*>(object); //如果某个输入框得到了输入焦点,则将它们设为全选状态 if( QEvent::FocusIn == event->type() ) { spinBox->selectAll(); return true; } //如果输入框内的字符为0,则清空该输入框(防止用户输入多个0) else if( QEvent::KeyPress == event->type() ) { QString context = spinBox->text(); if( "0" == context ) spinBox->clear(); } //如果输入框内的数字大于25,则输入焦点自动跳到下一格 else if( QEvent::KeyRelease == event->type() ) { int val = spinBox->value(); if( spinBox->value() > 25 && nowBox < 3 ) { m_SpinBox[nowBox+1]->setFocus(); } } return false; }