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

原创 2011年08月27日 00:31:54



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

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

QT中event事件机制与signal关系

事件处理流程: 某个事件发生------>exec()循环会接收到这个事件------> 创建一个事件对象,并将对象传递给QObject::event()------>  在QWidge...

QT的signal和slot的几种常见使用场景

本文介绍了QT中的Signal和Slot使用的几种场景。

Qt 信号-槽的同步与异步处理

通常使用的connect,实际上最后一个参数使用的是Qt::AutoConnection类型:Qt支持6种连接方式,其中3中最主要: 1.Qt::DirectConnection(直连方式)(信号与槽...

Qt postEvent

关于Qt postEvent 呵呵,转载了这么多别人的关于Event方面的文章,我也来说说两三句吧 前几天一直被这个postEvent给困扰着,所以主要讲解postEv...

[Qt] postevent emit

最近在为bestv做一个播放的plugin,这个plugin主要是在Qt中完成play等功能并能在JavaScript里面进行调用,其中里面就遇到了信息、事件等问题。开始时,我选择通过connect关...

QT 之 sendEvent & PostEvent

QT文档中这样解释: sendEvent(QObject* receiver,QEvent* event) 使用notify()函数直接给receiver发送事件。 postEvent(QObj...

Qt多线程一

Qt 多 线 程一 何谓线程? 线程与并行处理任务息息相关,就像进程一样。那么,线程与进程有什么区别呢?当你在电子表格上进行数据结算的时候,在相同的桌面上可能有一个播放器正在播放你最喜欢的歌曲...

QT sendEvent()和postEvent()

QT文档中这样解释: sendEvent(QObject* receiver,QEvent* event) 使用notify()函数直接给receiver发送事件。 postEven...

QT学习之Timer的用法

QT学习之Timer的用法
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)