Using PyQt5, there are certain event handlers which I can bind to my object, and others that only work if I implement them as methods. changeEvent and event are examples of the later type.
You can see in my example below that I can add a keyPressEvent handler to my widget programmatically, but I can not do the same thing for changeEvent.
from PyQt5 import QtGui, QtWidgets, QtCore
import types
def keyPressEvent(self, key: QtGui.QKeyEvent) -> None:
#works
print(key.isAutoRepeat())
def changeEvent(self, a0: QtCore.QEvent) -> None:
#doesn't work
print("bound change event", a0.type())
bindable = [keyPressEvent, changeEvent]
def bind_key_functions(target):
for bound in bindable:
setattr(target, bound.__name__, types.MethodType(bound, target))
class my_widget(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle("w1")
bind_key_functions(self)
class my_widget2(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle("w2")
def changeEvent(self, a0: QtCore.QEvent) -> None:
#this does work
print("derived change event", a0.type())
app = QtWidgets.QApplication([])
mw1 = my_widget()
mw1.show()
mw2 = my_widget2()
mw2.show()
app.exec_()
What makes the changeEvent different? how can I force it to behave as I want?