如何在QCalendarWidget中禁用其他月份

时间:2018-12-11 07:46:59

标签: python python-3.x pyqt5 pyside2 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事件。例子:我在一个红色正方形上进行了购物,然后单击它,它将选择6月的第4个(即使在屏幕快照中是5月)。

Example

如何禁止那些日子成为不可选择的日子?

我在setDateRange事件上尝试过currentPageChanged,但无法按预期进行:

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事件。 setDateRange不起作用,因为如果我添加它以将选择范围仅限于本月,则日历顶部的“转到下个月或上个月”按钮将不起作用,并且我需要用户能够改变几个月。我只是不希望日历显示不属于该月页面的日期。

1 个答案:

答案 0 :(得分:1)

一种解决方案是过滤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_())