QAbstractNativeEventFilter#

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

Inheritance diagram of PySide6.QtCore.QAbstractNativeEventFilter

Synopsis#

Virtual functions#

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

Detailed Description#

class PySide6.QtCore.QAbstractNativeEventFilter#

Creates a native event filter.

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

abstract PySide6.QtCore.QAbstractNativeEventFilter.nativeEventFilter(eventType, message)#
Parameters:
Return type:

PyObject

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

This method is called for every native event.

Note

The filter function here receives native messages, for example, MSG or XCB event structs.

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.

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

On 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.

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

In your reimplementation of this function, if you want to filter the message out, i.e. stop it being handled further, return true; otherwise return false.

Linux example

class MyXcbEventFilter(QAbstractNativeEventFilter):

# public
    bool nativeEventFilter(QByteArray eventType, void message, qintptr ) override

        if eventType == "xcb_generic_event_t":
            xcb_generic_event_t* ev = xcb_generic_event_t(message)
            # ...

        return False

Windows example

class MyMSGEventFilter(QAbstractNativeEventFilter):

# public
    bool nativeEventFilter(QByteArray eventType, void message, qintptr ) override

        if eventType == "windows_generic_MSG":
            msg = MSG(message)
            # ...
         elif eventType == "windows_dispatcher_MSG":
            msg = MSG(message)
            # ...

        return False

macOS example

mycocoaeventfilter.h:

from PySide6.QtCore import QAbstractNativeEventFilter
class MyCocoaEventFilter(QAbstractNativeEventFilter):

# public
    bool nativeEventFilter(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