<div dir="ltr"> [ "crossposted" as John (who I hope doesn't mind) did reply privately, but I believe that this might be useful to others]<br><div><div dir="ltr"><br></div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Il giorno sab 5 nov 2022 alle ore 19:48 John F Sturtz <<a href="mailto:john@sturtz.org">john@sturtz.org</a>> ha scritto:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">

<div>Your method of installing an event filter worked of course.  But 
continuing down the path of idle intellectual curiosity ...<br>
<br>
Now knowing that the calendar widget has an underlying QTableView, I 
might have thought I could get the same effect by reassigning the view's
 keyPressEvent() method.  Something like this:<br>
<br>
<span style="font-family:monospace">class Calendar(QCalendarWidget):</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace">    def __init__(self, 
parent=None):</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace">        
super().__init__(parent)</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace">        
self.setDateEditEnabled(False)</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace">#       
self.findChild(QAbstractItemView).installEventFilter(self)</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace">        
self.findChild(QAbstractItemView).keyPressEvent = self.key_press</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace">#   def eventFilter(self, 
obj, event):</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace">#       if (</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace">#           event.type() ==
 event.KeyPress</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace">#           and event.key()
 == Qt.Key_Space</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace">#       ):</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace">#           print('space 
pressed!')</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace">#           return True</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace">#       return 
super().eventFilter(obj, event)</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace">    def key_press(self, 
event):</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace">        key = event.key()</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace">        mod = 
int(event.modifiers())</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace">        handled = False</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace">        print('key 
pressed!')</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace">        if key == 
Qt.Key_Space:</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace">            print('space 
pressed!')</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace">            handled = True</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace">        if not handled:</span><span style="font-family:monospace"><br>
</span><span style="font-family:monospace">            
super().keyPressEvent(event)</span><br>
<br>
I realize that brute-force reassigning the keyPressEvent() method (as 
opposed to subclassing and re-implementing) is ugly.  And I probably 
wouldn't do it for real.  But I have done it on occasion just for 
purposes of mucking around (including just now, in another test file 
that I have).  And it usually works.  I'm a little surprised that it 
doesn't here.<br clear="all"></div></blockquote></div></div><div><br></div><div>I cannot provide a complete explanation about the exact reason for which it doesn't work, as my knowledge of C++ is limited to reading and understanding what code does. Also, I don't know how exactly sip works with overridden C++ functions.</div><div><br></div><div>That said, the reason is caused by the fact that the view used in QCalendarWidget is actually a private subclass (named QCalendarView) which on its own overrides keyPressEvent.<br></div><div>My understanding is that, since that class is not publicly declared, you cannot override any of its methods, and you can only use its functions by explicitly calling them (like installing the event filter).<br></div><div>It's possible that this is related to the "function reference caching" (I don't know the exact term) that happens when you try to override a function *after* it's been already called: if you overwrite mousePressEvent *after* the default implementation has been called, it will not work. This is obviously not the case, since you're overwriting it before it can be called, but it's possible that it has a similar cause.<br><br></div><div>If Phil or anybody with enough knowledge of C++/sip would like to provide more precise explanations about this, I'll be glad to read it.<br></div><div><br></div><div>Hope this helps.</div><div>Maurizio<br></div><div><br>-- <br><div dir="ltr" class="gmail_signature">È difficile avere una convinzione precisa quando si parla delle ragioni del cuore. - "Sostiene Pereira", Antonio Tabucchi<br><a href="http://www.jidesk.net" target="_blank">http://www.jidesk.net</a></div></div></div>