[PyQt] Handling Windows messages

Claessen, Matt - [Avantes] MattC at avantes.com
Sat Apr 25 16:16:34 BST 2015


Hi,

I found a solution myself, but I will post it here so others may save some time.

There is an easy solution in overriding nativeEvent in the MainWindow class:

    def nativeEvent(self, eventType, message):
        msg = ctypes.wintypes.MSG.from_address(message.__int__())
        if eventType == "windows_generic_MSG":
            if msg.message == WM_MEAS_READY:
                # print("Message Received!")
                self.newdata.emit()
        return False, 0

Because QMainWindow derives from QObject, you can now easily connect a signal
to the incoming event.
newdata is defined in the MainWindow class, with:
newdata = pyqtSignal()
and it connects to a slot by putting this in the constructor:
self.newdata.connect(self.handle_newdata)

The signal also just fires once per incoming message, so this solves both my problems.

The main problem is that all Qt documentation and most posts on the Internet point 
you to solution with a NativeEventFilter that has to be installed in the QApplication.
The filter approach looks however like a dead end in PyQt5.

Regards, Matt


More information about the PyQt mailing list