In this article we will see how we can set the custom shortcuts for the QCalendarWidget to go to the specific month for example when user press the ‘O’ key calendar should show the October similarly for other months as well. And for months like January, June and July who share same initial then when the ‘J’ key is pressed it should the next month to the current month for example if current month is June and ‘J’ key is pressed it should show the July month and if present month was July then it should show the January.
1. Create a main window
2. Create a QCalendarWidget
3. Set various properties to the calendar
4. Grab the keyboard of the calendar so that default keyboard function could not take place
5. Override the keyPressEvent
6. Inside the override method create a list of keys for all the months
7. Get the current month and year of the calendar
8. Check if the ‘J’ key is pressed then check if current month is January then set current month as June, if current month is June set current month July else set current month as January
9. Check if the ‘ M’ key is pressed then check if the current month is March then set current month as May else set current month as March
10 Similarly, check if the ‘A’ key is pressed then check if the current month is April set current month as August else set current month as April
11. If any other key is pressed check if the key is present in the month list then get the index of the key in the list then set current month as index + 1
Below is the implementation
A key pressed A key pressed J Key Pressed J Key Pressed J Key Pressed 10 Month Key Pressed 12 Month Key Pressed 2 Month Key Pressed 11 Month Key Pressed J Key Pressed A key pressed J Key Pressed A key pressed J Key Pressed 10 Month Key Pressed
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.