eventsloggingpysideevent-loopqeventloop

pyside - Is there a way of getting log information for every event in the event loop?


I'm developing an application with PySide, and I was wondering if there is a way of logging which events enter the main event loop and which ones leave. my application use no threads, it is complete asynchronous, but I wanted to have log registries of the event loop for debugging purpose. I use logging as my logging facility (duh).

Pax et bonum.


Solution

  • As a good user on reddit replies:

    Check this out: QCoreApplication.notify. The event loop is implemented in QCoreApplication, so you can subclass and install your own code to inspect events.

    class Application(QApplication): events = {constant: name for name,constant in QEvent.dict.items() if name[0].isupper() and isinstance(constant, int)}

    def notify(self, obj, event):
        print("{:<20}{}".format(Application.events[event.type()], obj))
        return super().notify(obj, event)
    

    Works pretty well. https://i.sstatic.net/G7Z46.png

    Of course, you will still have to work out which events are being consumed. That super call returns True or False, but almost every event I saw was returning True. Your might have to traverse object relationships or something—I'm not sure because I haven't done this, but there appears to be pretty good support for getting down into that loop and doing what you like.


    However, I'm more inclined to follow the third option listed on Qt's documentation (quote from Qt 4.8):

    3. Installing an event filter on QCoreApplication::instance(). Such an event filter is able to process all events for all widgets, so it's just as powerful as reimplementing notify(); furthermore, it's possible to have more than one application-global event filter. Global event filters even see mouse events for disabled widgets. Note that application event filters are only called for objects that live in the main thread.