title: How to correctly attach an eventFilter with installEventFilter to a QtWidget?
body: <p>I am writing Python code around a C++ application based on Qt5. My Python code is passed an instance / object of the <code>QMainWindow</code> class - which is implemented in C++. I can neither derive from this class nor modify it - I am just receiving an existing object.</p>
  
  <p>I receive the object (or a handle on it) while the window is still being initialized. By the time my code sees it, it has not been displayed yet. My code is supposed to run a function once the <code>QMainWindow</code> object has actually been displayed.</p>
  
  <p>From what I gather, a Qt widget will <a href="https://forum.qt.io/topic/6386/how-can-i-check-if-qmainwindow-is-fully-loaded/10" rel="nofollow noreferrer">emit <code>QEvent.Polish</code> once it has been fully displayed</a>, also see <a href="https://doc.qt.io/qt-5/qevent.html" rel="nofollow noreferrer">here</a>. <code>QMainWindow</code> is derived from <code>QWidget</code>. This is what I intend to grab and use as a trigger for my code. Based on <a href="https://stackoverflow.com/q/45368148/1672565">this question and its answers</a>, I can achieve this by registering an <code>eventFilter</code> on the <code>QMainWindow</code> object, see <a href="https://doc.qt.io/qt-5/qobject.html#installEventFilter" rel="nofollow noreferrer">Qt's documentation on <code>installEventFilter</code></a>.</p>
  
  <p><code>installEventFilter</code> expects a <code>QObject</code> (or something derived from it). I found a small / minimal example <a href="https://riverbankcomputing.com/pipermail/pyqt/2009-April/022596.html" rel="nofollow noreferrer">here</a>. The following snipped is based on it:</p>
  
  <pre class="lang-py prettyprint-override"><code>class something:
  
      def __init__(self, some_mainwindow_obj):
          "some_mainwindow_obj is an instance of QMainWindow written in C++"
  
          self.some_mainwindow_obj = some_mainwindow_obj
          self.wait_for_mainwindow = True
          delay_on_load(self.my_func, some_mainwindow_obj)
  
      def my_func(self):
          "MUST ONLY RUN WHEN some_mainwindow_obj HAS BEEN FULLY DISPLAYED"
  
          if not self.wait_for_mainwindow:
              return
          self.wait_for_mainwindow = False
  
          # do something useful NOW ...
  
  def delay_on_load(func, qt_widget):
  
      from PyQt5.QtCore import QObject, QEvent
      from PyQt5.QtWidgets import QMessageBox
  
      QMessageBox.warning(None, 'TEST', 'Installing filter ...')
  
      def wrapper():
          qt_widget.removeEventFilter(filter_obj)
          func()
  
      class filter_class(QObject):
          def eventFilter(self, obj, event):
              QMessageBox.warning(None, 'TEST', 'Got event type "%s"' % str(event.type())) # DOES NOT TRIGGER AT ALL
              if event.type() == QEvent.Polish: # and obj is qt_widget:
                  wrapper()
                  return True
              # return QObject.eventFilter(obj, event)
              return super().eventFilter(obj, event)
  
      filter_obj = filter_class()
      qt_widget.installEventFilter(filter_obj)
  
      QMessageBox.warning(None, 'TEST', '... install done.')
  </code></pre>
  
  <p>As far as documentation goes and other examples show, this should work. However, <strong>my <code>eventFilter</code> routine is not even triggered a single time</strong>. What am I doing wrong?</p>
  
  <hr>
  
  <p>An earlier, <a href="https://stackoverflow.com/q/57526855/1672565">closed question</a> on the same subject - inspired by this <a href="https://stackoverflow.com/q/14356121/1672565">question</a> and one of its <a href="https://stackoverflow.com/a/39731872/1672565">answers</a>.</p>