QAbstractNativeEventFilter 類提供用於接收本機事件 (譬如:MSG 或 XCB 事件結構) 的接口。 更多...
| 頭: | #include <QAbstractNativeEventFilter> |
| qmake: | QT += core |
| Since: | Qt 5.0 |
該類在 Qt 5.0 引入。
| QAbstractNativeEventFilter () | |
| virtual | ~QAbstractNativeEventFilter () |
| virtual bool | nativeEventFilter (const QByteArray & eventType , void * message , long * 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
,
long
*
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