如何在 QCalendarWidget 中禁用其他月份

How to disable other months days in QCalendarWidget

我的目标是禁止用户点击 QCalendarWidget 中不是当月的日期,因此我将小部件子类化以实现它。到目前为止,我可以让那些日子根本不呈现任何文本(太棒了)。这是代码:

class QCustomCalendar(QCalendarWidget):
    """Create my own Calendar with my own options."""

    def __init__(self, parent=None):
        """Initializing functions"""
        QCalendarWidget.__init__(self, parent)
        self.setEnabled(True)
        self.setGeometry(QRect(0, 0, 320, 250))
        self.setGridVisible(False)
        self.setHorizontalHeaderFormat(QCalendarWidget.SingleLetterDayNames)
        self.setVerticalHeaderFormat(QCalendarWidget.NoVerticalHeader)
        self.setNavigationBarVisible(True)
        self.setDateEditEnabled(True)
        self.setObjectName("calendarWidget")

    def paintCell(self, painter, rect, date):
        """Sub-class this and repaint the cells"""
        # Render only this-month days
        month = "{0}-{1}".format(str(self.yearShown()), str(self.monthShown()).zfill(2))
        day = str(date.toPython())
        if not day.startswith(month):
            return
        QCalendarWidget.paintCell(self, painter, rect, date)

但是,如果我点击一个非呈现的日期,它仍然计数并触发 clicked 事件。示例:我对一个红色方块进行了 Photoshop,点击它,它会 select 6 月 4 日(即使我们在屏幕截图中是在 5 月)。

如何禁用那些日子而不是 selectables?

我在 currentPageChanged 事件上尝试了 setDateRange,但它没有按预期工作:

def __init__(self, parent=None):
    # some code
    self.currentPageChanged.connect(self.store_current_month)
    self.clicked.connect(self.calendar_itemchosen)

def store_current_month(self):
    self.CURRENT_MONTH = "{0}-{1}".format(str(self.yearShown()), str(self.monthShown()).zfill(2))

def calendar_itemchosen(self):
    day = str(self.selectedDate().toPython())
    print(day)
    if day.startswith(self.CURRENT_MONTH):
        selection = self.selectedDate()
        # some code
        self.close()

用这段代码点击那个红色方块的结果是:

2018-06
2018-06-04

所以我猜 Qt 首先会触发 currentPageChanged 事件,当你 select 另一个月的日期。 setDateRange 将不起作用,因为如果我添加它以将 selections 限制为仅本月,那么日历顶部的 "go to next or previous month" 按钮将不起作用,我需要用户能够更改月份。我只是不想让日历显示不属于本月页面的日子。

一种解决方案是过滤QCalendarWidget内部具有的QTableView的mousePressEvent事件。为此,我们使用事件过滤器:

from PyQt5 import QtCore, QtWidgets

class CalendarWidget(QtWidgets.QCalendarWidget):
    def __init__(self, parent=None):
        super(CalendarWidget, self).__init__(parent, gridVisible=False,
            horizontalHeaderFormat=QtWidgets.QCalendarWidget.SingleLetterDayNames,
            verticalHeaderFormat=QtWidgets.QCalendarWidget.NoVerticalHeader,
            navigationBarVisible=True,
            dateEditEnabled=True)       
        self.setEnabled(True)
        self.setGeometry(QtCore.QRect(0, 0, 320, 250))
        self.clicked.connect(print)

        self.table_view = self.findChild(QtWidgets.QTableView, "qt_calendar_calendarview")
        self.table_view.viewport().installEventFilter(self)
        self.setFirstDayOfWeek(QtCore.Qt.Monday)

    def referenceDate(self):
        refDay = 1
        while refDay <= 31:
            refDate = QtCore.QDate(self.yearShown(), self.monthShown(), refDay)
            if refDate.isValid(): return refDate
            refDay += 1
        return QtCore.QDate()

    def columnForDayOfWeek(self, day):
        m_firstColumn = 1 if self.verticalHeaderFormat() != QtWidgets.QCalendarWidget.NoVerticalHeader else 0
        if day < 1 or day > 7: return -1
        column = day - int(self.firstDayOfWeek())
        if column < 0:
            column += 7
        return column + m_firstColumn

    def columnForFirstOfMonth(self, date):
        return (self.columnForDayOfWeek(date.dayOfWeek()) - (date.day() % 7) + 8) % 7

    def dateForCell(self, row, column):
        m_firstRow = 1 if self.horizontalHeaderFormat() != QtWidgets.QCalendarWidget.NoHorizontalHeader else 0
        m_firstColumn = 1 if self.verticalHeaderFormat() != QtWidgets.QCalendarWidget.NoVerticalHeader else 0
        rowCount = 6
        columnCount = 7
        if row < m_firstRow or row > (m_firstRow + rowCount -1) or column < m_firstColumn or column > (m_firstColumn + columnCount -1):
            return QtCore.QDate()
        refDate = self.referenceDate()
        if not refDate.isValid():
            return QtCore.QDate()
        columnForFirstOfShownMonth = self.columnForFirstOfMonth(refDate)
        if (columnForFirstOfShownMonth - m_firstColumn) < 1:
            row -= 1
        requestedDay = 7*(row - m_firstRow) +  column  - columnForFirstOfShownMonth - refDate.day() + 1
        return refDate.addDays(requestedDay)

    def eventFilter(self, obj, event):
        if obj is self.table_view.viewport() and event.type() == QtCore.QEvent.MouseButtonPress:    
            ix = self.table_view.indexAt(event.pos())
            date = self.dateForCell(ix.row(), ix.column())
            d_start = QtCore.QDate(self.yearShown(), self.monthShown(), 1)
            d_end = QtCore.QDate(self.yearShown(), self.monthShown(), d_start.daysInMonth())
            if d_start > date or date > d_end:
                return True
        return super(CalendarWidget, self).eventFilter(obj, event)

    def paintCell(self, painter, rect, date):
        d_start = QtCore.QDate(self.yearShown(), self.monthShown(), 1)
        d_end = QtCore.QDate(self.yearShown(), self.monthShown(), d_start.daysInMonth())
        if d_start <= date <= d_end:
            super(CalendarWidget, self).paintCell(painter, rect, date)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = CalendarWidget()
    w.show()
    sys.exit(app.exec_())