<html theme="default-light" iconset="color"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head><body text="#000000"><span>Maurizio Berti wrote on 11/5/2022 6:09
PM:</span><br>
<blockquote type="cite"
cite="mid:CAPn+-XQ-zYP0Qj2eh-RSYdSCRHT10-WtNS6M=JRkTgD2jwojow@mail.gmail.com">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<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>
</blockquote>
Don't mind at all. :-)<br>
<blockquote type="cite"
cite="mid:CAPn+-XQ-zYP0Qj2eh-RSYdSCRHT10-WtNS6M=JRkTgD2jwojow@mail.gmail.com">
<div dir="ltr">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>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>
</blockquote>
This makes sense (from 10,000 feet; I'm not a C++ programmer).<br>
<br>
--------<br>
<br>
Here's one more thing for you, or anyone else who might be interested,
to ponder. It seems there's one small problem with the date that the
date edit popup initially displays.<br>
<br>
If I call <span style="font-family: monospace;">setSelectedDate()</span>
when the QCalendarWidget is first displayed and set the selected date
to something other than today's date, that date will correctly show in
the calendar. But then if I hit a character key to display the popup,
it will display today's date, not the one selected in the calendar.<br>
<br>
Note that if I then click on (or arrow to) some other date in the
calendar to select it, then display the popup, it will display the
correct (newly-selected) date. So it seems the popup must respond to
the <span style="font-family: monospace;">selectionChanged</span>
signal. But evidently not the first time. (I'm guessing maybe the <span
style="font-family: monospace;">selectionChanged</span> signal isn't
connected until after the popup widget is created the first time? I
spent a bit of time looking through the source code, but it's a little
over my head ...)<br>
<br>
<span style="font-style: italic;"></span><span style="font-style:
italic;"></span>This isn't really a big huge deal. Mostly just a
curiosity. I tried just about everything I could think of to get around
it, but couldn't find anything.<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 QDate</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;"> self.setDateEditEnabled(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;"> def showEvent(self, event):</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;"> # This sets the calendar's selected date 4 days
ahead of today's date.</span><span style="font-family: monospace;"></span><br>
<span style="font-family: monospace;"></span><span style="font-family:
monospace;"> # The calendar will correctly display it, but the
first time you display</span><span style="font-family: monospace;"></span><br>
<span style="font-family: monospace;"></span><span style="font-family:
monospace;"> # the date edit popup, it will initially display
today's date, not the</span><span style="font-family: monospace;"></span><br>
<span style="font-family: monospace;"></span><span style="font-family:
monospace;"> # date selected in the calendar.</span><span
style="font-family: monospace;"></span><br>
<span style="font-family: monospace;"></span><span style="font-family:
monospace;">
self.setSelectedDate(QDate.currentDate().addDays(4))</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;"> # I tried emitting the selectionChanged signal
explicitly, but that</span><span style="font-family: monospace;"></span><br>
<span style="font-family: monospace;"></span><span style="font-family:
monospace;"> # didn't work either</span><span
style="font-family: monospace;"></span><br>
<span style="font-family: monospace;"></span><span style="font-family:
monospace;"> # self.selectionChanged.emit()</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;"></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><span style="font-family: monospace;"></span><br>
<span style="font-family: monospace;"></span></div>
<br>
/John<br>
</body></html>