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

时间:2022-12-06 10:59:09



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() )