QAbstractNativeEventFilter Class
QAbstractNativeEventFilter クラスは、MSG や XCB イベント構造体などのネイティブイベントを受信するためのインターフェースを提供します。詳細...
| ヘッダー: | #include <QAbstractNativeEventFilter> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
| qmake: | QT += core |
パブリック関数
| QAbstractNativeEventFilter() | |
| virtual | ~QAbstractNativeEventFilter() |
| virtual bool | nativeEventFilter(const QByteArray &eventType, void *message, qintptr *result) = 0 |
メンバ関数のドキュメント
QAbstractNativeEventFilter::QAbstractNativeEventFilter()
ネイティブのイベントフィルターを作成します。
デフォルトでは、これだけでは何も行われません。アプリケーションオブジェクトにインストールすることを忘れないでください。
[virtual noexcept] QAbstractNativeEventFilter::~QAbstractNativeEventFilter()
ネイティブのイベントフィルターを破棄します。
これにより、アプリケーションから自動的に削除されます。
[pure virtual] bool QAbstractNativeEventFilter::nativeEventFilter(const QByteArray &eventType, void *message, qintptr *result)
このメソッドは、ネイティブイベントが発生するたびに呼び出されます。
注:ここでのフィルタ関数は 、MSGやXCBイベント構造体などのネイティブメッセージを受け取ります。
これは QPA プラットフォームプラグインによって呼び出されます。Windows では、イベントディスパッチャによって呼び出されます。
イベント型 `eventType ` は、実行時に選択されたプラットフォームプラグインに固有のものであり、message を適切な型にキャストするために使用できます。
X11 では、eventType は「xcb_generic_event_t」に設定され、message を xcb_generic_event_t ポインタにキャストすることができます。
Windows では、eventType は、トップレベルウィンドウに送信されるメッセージの場合は「windows_generic_MSG」に、登録されたホットキーからのメッセージなどのシステム全体のメッセージの場合は「windows_dispatcher_MSG」に設定されます。どちらの場合も、message は MSG ポインタにキャストできます。result ポインタはWindowsでのみ使用され、LRESULTポインタに対応しています。
macOS では、eventType は「mac_generic_NSEvent」に設定されており、message は NSEvent ポインタにキャストできます。
この関数を再実装する際、message を除外したい場合(つまり、それ以上の処理を停止したい場合)はtrueを返し、そうでない場合はfalseを返してください。
Linux の例
class MyXcbEventFilter : public QAbstractNativeEventFilter
{
public:
bool nativeEventFilter(const QByteArray &eventType, void *message, qintptr *) override
{
if (eventType == "xcb_generic_event_t") {
xcb_generic_event_t* ev = static_cast<xcb_generic_event_t *>(message);
// ...
}
return false;
}
};Windows の例
class MyMSGEventFilter : public QAbstractNativeEventFilter
{
public:
bool nativeEventFilter(const QByteArray &eventType, void *message, qintptr *) override
{
if (eventType == "windows_generic_MSG") {
MSG *msg = static_cast<MSG *>(message);
// ...
} else if (eventType == "windows_dispatcher_MSG") {
MSG *msg = static_cast<MSG *>(message);
// ...
}
return false;
}
};macOS の例
mycocoaeventfilter.h:
#include <QAbstractNativeEventFilter>
class MyCocoaEventFilter : public QAbstractNativeEventFilter
{
public:
bool nativeEventFilter(const QByteArray &eventType, void *message, qintptr *) override;
};mycocoaeventfilter.mm:
#include "mycocoaeventfilter.h"
#import <AppKit/AppKit.h>
bool MyCocoaEventFilter::nativeEventFilter(const QByteArray &eventType, void *message, qintptr *)
{
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© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.