QCalendarWidget - How to disable dates

I am trying to use a QCalendarWidget with enabled or disabled dates (and not just range dates), but this seems impossible ( https://bugreports.qt.io/browse/QTBUG-198 ).

Do you know if this can be done using the paintEvent method (or something else)?

Thanks for reading:)

+4
source share
1 answer

I did some research. The calendar widget contains a regular QTableView subclass ( QCalendarView ) with a regular model ( QCalendarModel ). My attempt was to add a proxy model for the view and change the data() and flags() functions to return the disabled state for the dates I wanted to disable. But I needed to know what date is displayed in the cell. There is a QCalendarModel::dateForCell method for this, but it is not available for code. This is quite complicated and depends on the implementation of QCalendarModel .

Therefore, I think this is hardly possible. The only way I can see is to copy the QCalendarWidget source to your project and rename the classes to avoid conflicts in existing Qt classes. Then you can change the implementation. Anywhere where the minimum and maximum dates are used (for example, in the QCalendarModel::flags function), you should replace it with a list of enabled / disabled classes.

If you find something useful in my attempt, I posted the code here . The proxy model is headed, and the background of all cells turns green.

+3
source

Source: https://habr.com/ru/post/1492912/


All Articles