QAbstractNativeEventFilter 类提供用于接收本机事件 (譬如:MSG 或 XCB 事件结构) 的接口。 更多...
头: | #include <QAbstractNativeEventFilter> |
CMake: |
find_package(Qt6 COMPONENTS Core REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
Since: | Qt 5.0 |
QAbstractNativeEventFilter () | |
virtual | ~QAbstractNativeEventFilter () |
virtual bool | nativeEventFilter (const QByteArray & eventType , void * message , qintptr * result ) = 0 |
创建本机事件过滤器。
By default this doesn't do anything. Remember to install it on the application object.
[虚拟]
QAbstractNativeEventFilter::
~QAbstractNativeEventFilter
()
销毁本机事件过滤器。
这会从应用程序中自动移除它。
[pure virtual]
bool
QAbstractNativeEventFilter::
nativeEventFilter
(const
QByteArray
&
eventType
,
void
*
message
,
qintptr
*
result
)
此方法被调用,为每个本机事件。
注意: 此处的过滤器函数接收本机消息,例如: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:
HEADERS += mycocoaeventfilter.h OBJECTIVE_SOURCES += mycocoaeventfilter.mm LIBS += -framework AppKit