返回介绍

QCustomEvent Class

发布于 2019-10-04 14:59:11 字数 3171 浏览 1069 评论 0 收藏 0

The QCustomEvent class provides support for custom events. More...

#include <qevent.h>

Inherits QEvent.

List of all member functions.

Public Members

  • QCustomEvent ( inttype )
  • QCustomEvent ( Typetype, void*data )
  • void * data () const
  • void setData ( void*data )

Detailed Description

The QCustomEvent class provides support for custom events.

QCustomEvent is a generic event class for user-defined events. User defined events can be sent to widgets or other QObject instances using QApplication::postEvent() or QApplication::sendEvent(). Subclasses of QWidget can easily receive custom events by implementing the QWidget::customEvent() event handler function.

QCustomEvent objects should be created with a type ID that uniquely identifies the event type. To avoid clashes with the Qt-defined events types, the value should be at least as large as the value of the "User" entry in the QEvent::Type enum.

QCustomEvent contains a generic void* data member that may be used for transferring event-specific data to the receiver. Note that since events are normally delivered asynchronously, the data pointer, if used, must remain valid until the event has been received and processed.

QCustomEvent can be used as-is for simple user-defined event types, but normally you will want to make a subclass of it for your event types. In a subclass, you can add data members that are suitable for your event type.

Example:

    class ColorChangeEvent : public QCustomEvent
    {
    public:
        ColorChangeEvent( QColor color )
            : QCustomEvent( 346798 ), 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() == 346798 ) {  // It must be a ColorChangeEvent
            ColorChangeEvent* ce = (ColorChangeEvent*)e;
            newColor = ce->color();
        }
    }
    

See also QWidget::customEvent(), QApplication::notify() and Event Classes.


Member Function Documentation

QCustomEvent::QCustomEvent ( inttype )

Constructs a custom event object with event type type. The value of type must be at least as large as QEvent::User. The data pointer is set to 0.

QCustomEvent::QCustomEvent ( Typetype, void*data )

Constructs a custom event object with the event type type and a pointer to data. (Note that any int value may safely be cast to QEvent::Type).

void * QCustomEvent::data () const

Returns a pointer to the generic event data.

See also setData().

void QCustomEvent::setData ( void*data )

Sets the generic data pointer to data.

See also data().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文