转载地址:http://blog.sina.com.cn/s/blog_52504e3f0100hdgn.html
事件处理流程:
某个事件发生------>exec()循环会接收到这个事件------>
创建一个事件对象,并将对象传递给QObject::event()------>
在QWidget::event()函数中,分配给特定的事件处理函数------>
在QButton的事件处理函数中emit(clicked消息)
signal发送的方式:emit
event发送的方式:
1:窗口系统事件;(QEvent::Type中列出的)
2:由sendEvent()/postEvent()手动发送;
event与signal的关系:
两者没有必然的关系。只是有些事件发生时,在事件处理函数中会调用emit发送一个信号。
bool QObject::event( QEvent *e )
{
#if defined(CHECK_NULL)
if ( e == 0 )
qWarning( "QObject::event: Null events are not permitted" );
#endif
if ( eventFilters ) { // try filters
if ( activate_filters(e) ) // stopped by a filter
return TRUE;
}
switch ( e->type() ) {
case QEvent::Timer:
timerEvent( (QTimerEvent*)e );
return TRUE;
case QEvent::ChildInserted: case QEvent::ChildRemoved:
childEvent( (QChildEvent*)e );
return TRUE;
default:
break;
}
return FALSE;
}
//QObject::event()在QWidget中被重新实现。
bool QWidget::event( QEvent *e )
{
if ( QObject::event( e ) )
return TRUE;
switch ( e->type() ) {
case QEvent::MouseMove:
mouseMoveEvent( (QMouseEvent*)e );
break;
case QEvent::MouseButtonPress:
mousePressEvent( (QMouseEvent*)e );
break;
case QEvent::MouseButtonRelease:
mouseReleaseEvent( (QMouseEvent*)e );
break;
case QEvent::MouseButtonDblClick:
mouseDoubleClickEvent( (QMouseEvent*)e );
break;
case QEvent::Wheel:
wheelEvent( (QWheelEvent*)e );
if ( ! ((QWheelEvent*)e)->isAccepted() )
return FALSE;
break;
case QEvent::KeyPress: {
QKeyEvent *k = (QKeyEvent *)e;
bool res = FALSE;
if ( k->key() == Key_Backtab ||
(k->key() == Key_Tab &&
(k->state() & ShiftButton)) ) {
QFocusEvent::setReason( QFocusEvent::Tab );
res = focusNextPrevChild( FALSE );
QFocusEvent::resetReason();
} else if ( k->key() == Key_Tab ) {
QFocusEvent::setReason( QFocusEvent::Tab );
res = focusNextPrevChild( TRUE );
QFocusEvent::resetReason();
}
if ( res )
break;
QWidget *w = this;
while ( w ) {
w->keyPressEvent( k );
if ( k->isAccepted() || w->isTopLevel() )
break;
w = w->parentWidget();
k->accept();
}
}
break;
case QEvent::KeyRelease: {
QKeyEvent *k = (QKeyEvent *)e;
QWidget *w = this;
while ( w ) {
k->accept();
w->keyReleaseEvent( k );
if ( k->isAccepted() || w->isTopLevel() )
break;
w = w->parentWidget();
}
}
break;
case QEvent::FocusIn:
setFontSys();
focusInEvent( (QFocusEvent*)e );
break;
case QEvent::FocusOut:
focusOutEvent( (QFocusEvent*)e );
break;
case QEvent::Enter:
enterEvent( e );
break;
case QEvent::Leave:
leaveEvent( e );
break;
case QEvent::Paint:
// At this point the event has to be delivered, regardless
// whether the widget isVisible() or not because it
// already went through the filters
paintEvent( (QPaintEvent*)e );
break;
case QEvent::Move:
moveEvent( (QMoveEvent*)e );
break;
case QEvent::Resize:
resizeEvent( (QResizeEvent*)e );
break;
case QEvent::Close: {
QCloseEvent *c = (QCloseEvent *)e;
closeEvent( c );
if ( !c->isAccepted() )
return FALSE;
}
break;
#ifndef QT_NO_DRAGANDDROP
case QEvent::Drop:
dropEvent( (QDropEvent*) e);
break;
case QEvent::DragEnter:
dragEnterEvent( (QDragEnterEvent*) e);
break;
case QEvent::DragMove:
dragMoveEvent( (QDragMoveEvent*) e);
break;
case QEvent::DragLeave:
dragLeaveEvent( (QDragLeaveEvent*) e);
break;
#endif
case QEvent::Show:
showEvent( (QShowEvent*) e);
break;
case QEvent::Hide:
hideEvent( (QHideEvent*) e);
break;
case QEvent::ShowWindowRequest:
if ( !isHidden() )
showWindow();
break;
case QEvent::ParentFontChange:
if ( isTopLevel() )
break;
// FALL THROUGH
case QEvent::ApplicationFontChange:
if ( !own_font && !isDesktop() ) {
if ( !isTopLevel() && QApplication::font( this ).isCopyOf( QApplication::font() ) )
setFont( parentWidget()->font() );
else
setFont( QApplication::font( this ) );
own_font = FALSE;
}
break;
#ifndef QT_NO_PALETTE
case QEvent::ParentPaletteChange:
if ( isTopLevel() )
break;
// FALL THROUGH
case QEvent::ApplicationPaletteChange:
if ( !own_palette && !isDesktop() ) {
if ( !isTopLevel() && parentWidget() && QApplication::palette( this ).isCopyOf( QApplication::palette() ) )
setPalette( parentWidget()->palette() );
else
setPalette( QApplication::palette( this ) );
own_palette = FALSE;
}
break;
#endif
default:
if ( e->type() >= QEvent::User ) {
customEvent( (QCustomEvent*) e );
return TRUE;
}
return FALSE;
}
return TRUE;
}
void QButton::keyPressEvent( QKeyEvent *e )
{
switch ( e->key() ) {
case Key_Enter:
case Key_Return:
if ( inherits("QPushButton") )
emit clicked();
else
e->ignore();
break;
case Key_Space:
if ( !e->isAutoRepeat() ) {
if ( got_a_release )
setDown( TRUE );
else {
buttonDown = TRUE;
repaint( FALSE );
}
if ( inherits("QPushButton") )
emit pressed();
else
e->ignore();
}
break;
case Key_Up:
case Key_Left:
#ifndef QT_NO_BUTTONGROUP
if ( group() )
group()->moveFocus( e->key() );
else
#endif
focusNextPrevChild( FALSE );
break;
case Key_Right:
case Key_Down:
#ifndef QT_NO_BUTTONGROUP
if ( group() )
group()->moveFocus( e->key() );
else
#endif
focusNextPrevChild( TRUE );
break;
case Key_Escape:
if ( buttonDown ) {
buttonDown = FALSE;
update();
break;
}
// fall through
default:
e->ignore();
}
}