Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an event filter

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.

like image 477
crabman Avatar asked Jul 22 '10 10:07

crabman


People also ask

How do you filter an event?

To register a filter, use the addEventFilter() method. This method takes the event type and the filter as arguments. In Example 3-1, the first filter is added to a single node and processes a specific event type. A second filter for handling input events is defined and registered by two different nodes.

What is a WMI event filter?

An event filter is a WMI class that describes which events WMI delivers to a physical consumer. For example, an event filter can instruct WMI to deliver to a consumer all power management events, or all system reboot events. An event filter also describes the conditions under which WMI delivers the events.


1 Answers

@balpha is correct. The simple answer is that if you don't pass in a parent or otherwise ensure that the filter instance has a live reference, it will be garbage collected.

PyQt uses SIP to bind to Qt's C++ implementation. From the SIP documentation:

When a C++ instance is wrapped a corresponding Python object is created. The Python object behaves as you would expect in regard to garbage collection - it is garbage collected when its reference count reaches zero. What then happens to the corresponding C++ instance? The obvious answer might be that the instance’s destructor is called. However the library API may say that when the instance is passed to a particular function, the library takes ownership of the instance, i.e. responsibility for calling the instance’s destructor is transferred from the SIP generated module to the library.

Ownership of an instance may also be associated with another instance. The implication being that the owned instance will automatically be destroyed if the owning instance is destroyed. SIP keeps track of these relationships to ensure that Python’s cyclic garbage collector can detect and break any reference cycles between the owning and owned instances. The association is implemented as the owning instance taking a reference to the owned instance.

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:

self.filter = delkeyFilter()
self.dataTreeView.installEventFilter(self.filter)
like image 62
Kaleb Pederson Avatar answered Nov 13 '22 06:11

Kaleb Pederson