内容表

上一话题

QAbstractListModel

下一话题

QAbstractProxyModel

QAbstractNativeEventFilter

QAbstractNativeEventFilter class provides an interface for receiving native events, such as MSG or XCB event structs. 更多

Inheritance diagram of PySide2.QtCore.QAbstractNativeEventFilter

详细描述

class QAbstractNativeEventFilter

创建本机事件过滤器。

By default this doesn’t do anything. Remember to install it on the application object.

PySide2.QtCore.QAbstractNativeEventFilter. nativeEventFilter ( eventType , message )
参数
返回类型

PyObject

此方法被调用,为每个本机事件。

注意

此处的过滤器函数接收本机消息,例如:MSG 或 XCB 事件结构。

It is called by the QPA platform plugin. On Windows, it is called by the event dispatcher.

The type of event eventType is specific to the platform plugin chosen at run-time, and can be used to cast message to the right type.

在 X11, eventType is set to “xcb_generic_event_t”, and the message can be casted to a xcb_generic_event_t pointer.

在 Windows, eventType is set to “windows_generic_MSG” for messages sent to toplevel windows, and “windows_dispatcher_MSG” for system-wide messages such as messages from a registered hot key. In both cases, the message can be casted to a MSG pointer. The result pointer is only used on Windows, and corresponds to the LRESULT pointer.

在 macOS, eventType is set to “mac_generic_NSEvent”, and the message can be casted to an NSEvent pointer.

在此函数的重实现中,若希望过滤 message 即:停止进一步处理,返回 true;否则返回 false。

Linux 范例

class MyXcbEventFilter : public QAbstractNativeEventFilter
{
public:
    bool nativeEventFilter(const QByteArray &eventType, void *message, long *) override
    {
        if (eventType == "xcb_generic_event_t") {
            xcb_generic_event_t* ev = static_cast<xcb_generic_event_t *>(message);
            // ...
        }
        return false;
    }
};
											

macOS 范例

mycocoaeventfilter.h:

#include <QAbstractNativeEventFilter>
class MyCocoaEventFilter : public QAbstractNativeEventFilter
{
public:
    bool nativeEventFilter(const QByteArray &eventType, void *message, long *) override;
};
											

mycocoaeventfilter.mm:

#include "mycocoaeventfilter.h"
#import <AppKit/AppKit.h>
bool CocoaNativeEventFilter::nativeEventFilter(const QByteArray &eventType, void *message, long *)
{
    if (eventType == "mac_generic_NSEvent") {
        NSEvent *event = static_cast<NSEvent *>(message);
        if ([event type] == NSKeyDown) {
            // Handle key event
            qDebug() << QString::fromNSString([event characters]);
        }
    }
    return false;
}
											

myapp.pro:

<Code snippet "code/src_corelib_kernel_qabstractnativeeventfilter.pro:0" not found>