<html theme="default-light" iconset="color"><head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body
text="#000000">
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>
</body>
</html>