Qt:postEvent 與 customEvent() 函式 進行非同步通訊; 以及引數的傳遞 // 防止介面卡死;;

Rocky_Ansi發表於2015-11-26

 

 class ColorChangeEvent : public QCustomEvent
    {
    public:
        ColorChangeEvent( QColor color )
            : QCustomEvent( 65432 ), c( color ) {}
        QColor color() const { return c; }
    private:
        QColor c;
    };

    // To send an event of this custom event type:

    ColorChangeEvent* ce = new ColorChangeEvent( blue );
    QApplication::postEvent( receiver, ce );  // Qt will delete it when done

    // To receive an event of this custom event type:

    void MyWidget::customEvent( QCustomEvent * e )
    {
        if ( e->type() == 65432 ) {  // It must be a ColorChangeEvent
            ColorChangeEvent* ce = (ColorChangeEvent*)e;
            newColor = ce->color();
        }
    }
    

 

 

// 可以進行型別的轉化;;

相關文章