[Qt] postevent emit ----event与signal



signal发送的方式:emit
event发送的方式:
    1:窗口系统事件;
(QEvent::Type中列出的,些值最好定义成大于5000的值,如:const QEvent::Type CustomEvent_Login = (QEvent::Type)5001;
    2:由sendEvent()/postEvent()手动发送;
(我比较喜欢用postEvent,提交方式,什么时候处理交给Qt system决定,方式温和、不容易出错;sendEvent是提交后,就得立即处理,,好像出错的概念更大些,更不容易操作。)
event与signal的关系:两者没有必然的关系。只是event发生时,在事件处理函数中有时会调用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 );
       returnTRUE;
    case QEvent::ChildInserted:
    case QEvent::ChildRemoved:
       childEvent((QChildEvent*)e );
       returnTRUE;
    default:
       break;
    }
    returnFALSE;
}

//QObject::event()在QWidget中被重新实现,对event的处理变得更完善。
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();
    }
}

你可能感兴趣的:(timer,filter,null,qt,Signal,events)