<div dir="ltr"><div>QCalendarWidget actually uses a private QTableView to show the dates.</div><div><br></div><div>When focused, that view handles key events as a normal view, and by default views handle many conventional keys for keyboard navigation and item search.</div><div>When keys used for these purposes are captured, their events are accepted, meaning that they will not be propagated to the parent, so you will never receive it.</div><div><br></div><div>Also, QCalendarWidget uses the `dateEditEnabled` property to show a simple popup allowing easy date input for selection (which is what you're seeing).</div><div><br></div><div>So, you need to take two measures:</div><div><br></div><div>1. disable the date edit popup, so that it doesn't interfere with your needs (since it also appears when typing other standard keys, as the edit uses the current locale format and might require alphanumeric input for locales that use letters even for short date formats);</div><div>2. install an event filter on the view in order to capture its key events;</div><div><br></div><div><span style="font-family:monospace">class Calendar(QCalendarWidget):<br>    def __init__(self):<br>        super().__init__()<br>        self.setDateEditEnabled(False)<br>        self.findChild(QAbstractItemView).installEventFilter(self)<br><br>    def eventFilter(self, obj, event):<br>        if (<br>            event.type() == event.KeyPress<br>            and event.key() == Qt.Key_Space<br>        ):<br>            print('space pressed!')</span></div><div><span style="font-family:monospace">            return True<br></span></div><div><span style="font-family:monospace">        return super().eventFilter(obj, event)</span><br></div><div><br></div><div>Regards,</div><div>Maurizio<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Il giorno ven 4 nov 2022 alle ore 21:54 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>
Hi.  Some time ago, I posted quite a few questions to this forum.  Folks
 were greatly helpful, and I thank you all.<br>
  <br>
This is sort of a silly, frivolous question.  In that it wouldn't really
 be a big deal if I couldn't do what I was trying to do.  Mostly, it 
just really puzzles me.  And it bugs me when I don't understand.<br>
  <br>
I've got an app that uses the <span style="font-family:monospace">QCalendarWidget</span>,
 and I got it in my head to redefine some keys.  So I subclassed <span style="font-family:monospace">QCalendarWidget</span> and defined a 
custom <span style="font-family:monospace">keyPressEvent()</span>.  I 
can see that some key events are received by the custom method.  But 
many not -- including the <span style="font-family:monospace">Space</span>
 character (the one I originally intended to redefine), and all the 
alphabetic keys.<br>
  <br>
Here's a small example to demonstrate:<br>
  <br>
  <div style="margin-left:40px"><span style="font-family:monospace">from
 PyQt5.QtWidgets import QApplication, QCalendarWidget</span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace">from PyQt5.QtCore import Qt</span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace">import sys</span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace">class Calendar(QCalendarWidget):</span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace">    def __init__(self, parent=None):</span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace">        super().__init__(parent)</span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace">    def keyPressEvent(self, event):</span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace">        key = event.key()</span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace">        mod = int(event.modifiers())</span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace">        handled = False</span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace">        print(key, mod)</span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace">        if key == Qt.Key_Space:</span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace">            print('[space]')</span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace">            handled = True</span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace">        if not handled:</span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace">            super().keyPressEvent(event)</span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace">        </span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace">app = QApplication(sys.argv)</span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace">window = Calendar()</span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace">window.show()</span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace"></span><br>
    <span style="font-family:monospace"></span><span style="font-family:monospace">app.exec()</span><br>
  </div>
  <br>
(File also attached).<br>
  <br>
If I run this, the first <span style="font-family:monospace">print()</span>
 statement executes if I hit (for example) the <span style="font-family:monospace">Escape</span> or <span style="font-family:monospace">Delete</span>
 key.  But if I hit the <span style="font-family:monospace">Space</span>
 key, all that happens is that the widget displays a tooltip showing the
 currently selected date.<br>
  <br>
How can this even happen?  How is this widget stopping my custom <span style="font-family:monospace">keyPressEvent()</span> method from 
receiving some keystrokes?  And more to the point, I suppose:  Is there a
 way I can make it otherwise?<br>
  <br>
Thanks again!<br>
  <br>
/John<br>
</div>

</blockquote></div><br clear="all"><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>