I am trying to enable the delete key in my treeview. This is what I have so far:
class delkeyFilter(QObject):
delkeyPressed = pyqtSignal()
def eventFilter(self, obj, event):
if event.type() == QEvent.KeyPress:
if event.key() == Qt.Key_Delete:
self.delkeyPressed.emit()
print 'delkey pressed'
return True
return False
I connect the eventfilter like this:
filter = delkeyFilter(self.dataTreeView)
self.dataTreeView.installEventFilter(filter)
Why do I need to pass self.dataTreeview when I create the filter? It doesn’t work without it.
@balpha is correct. The simple answer is that if you don’t pass in a parent or otherwise ensure that the
filterinstance has a live reference, it will be garbage collected.PyQt uses SIP to bind to Qt’s C++ implementation. From the SIP documentation:
The above implies that if you pass a Python object to a Qt object that takes ownership, everything will also work, even though you haven’t guaranteed that a reference to the specific object was maintained.
So, to restate what @balpha said in his comment, here’s one workaround for the case when you don’t want to pass in an object to the constructor: