keyPressEvent() with QCalendarWidget

Maurizio Berti maurizio.berti at gmail.com
Sat Nov 5 01:01:40 GMT 2022


QCalendarWidget actually uses a private QTableView to show the dates.

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.
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.

Also, QCalendarWidget uses the `dateEditEnabled` property to show a simple
popup allowing easy date input for selection (which is what you're seeing).

So, you need to take two measures:

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);
2. install an event filter on the view in order to capture its key events;

class Calendar(QCalendarWidget):
    def __init__(self):
        super().__init__()
        self.setDateEditEnabled(False)
        self.findChild(QAbstractItemView).installEventFilter(self)

    def eventFilter(self, obj, event):
        if (
            event.type() == event.KeyPress
            and event.key() == Qt.Key_Space
        ):
            print('space pressed!')
            return True
        return super().eventFilter(obj, event)

Regards,
Maurizio

Il giorno ven 4 nov 2022 alle ore 21:54 John F Sturtz <john at sturtz.org> ha
scritto:

> Hi.  Some time ago, I posted quite a few questions to this forum.  Folks
> were greatly helpful, and I thank you all.
>
> 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.
>
> I've got an app that uses the QCalendarWidget, and I got it in my head to
> redefine some keys.  So I subclassed QCalendarWidget and defined a custom
> keyPressEvent().  I can see that some key events are received by the
> custom method.  But many not -- including the Space character (the one I
> originally intended to redefine), and all the alphabetic keys.
>
> Here's a small example to demonstrate:
>
> from PyQt5.QtWidgets import QApplication, QCalendarWidget
> from PyQt5.QtCore import Qt
> import sys
>
> class Calendar(QCalendarWidget):
>
>     def __init__(self, parent=None):
>         super().__init__(parent)
>
>     def keyPressEvent(self, event):
>         key = event.key()
>         mod = int(event.modifiers())
>         handled = False
>
>         print(key, mod)
>
>         if key == Qt.Key_Space:
>             print('[space]')
>             handled = True
>
>         if not handled:
>             super().keyPressEvent(event)
>
>
> app = QApplication(sys.argv)
> window = Calendar()
> window.show()
>
> app.exec()
>
> (File also attached).
>
> If I run this, the first print() statement executes if I hit (for
> example) the Escape or Delete key.  But if I hit the Space key, all that
> happens is that the widget displays a tooltip showing the currently
> selected date.
>
> How can this even happen?  How is this widget stopping my custom
> keyPressEvent() method from receiving some keystrokes?  And more to the
> point, I suppose:  Is there a way I can make it otherwise?
>
> Thanks again!
>
> /John
>


-- 
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20221105/79df0667/attachment-0001.htm>


More information about the PyQt mailing list