如何获取 QCalendarWidget 中显示的所有日期

发布于 2024-08-22 06:08:33 字数 272 浏览 2 评论 0原文

我在我的应用程序中使用 QCalendarWidget,并且重载了 updateCells 方法,以便在满足特定条件的每个日期上放置红色背景。

我的问题是我不知道如何获取日历中显示的第一个日期(不是该月的第一个日期)和显示最后一个日期。 示例:在 2 月,显示的第一个日期是 1 月 25 日,最后显示的日期是 3 月 7 日。

QCalendarWidget 中没有任何有用的方法,我想不出一个算法。 你知道如何做到这一点吗?

I am using a QCalendarWidget in my application, and I overloaded the updateCells method to put a red background on every dates which meet certain conditions.

My problem is I don't know how to get the first date displayed in the calendar (not the first date in the month), and the last date displayed.
Example: in february, the first date displayed is january the 25th, and the last date displayed is march the 7th.

There isn't any useful method in QCalendarWidget and I can't think of an algorithm for this.
Do you have any idea how to do this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉栀 2024-08-29 06:08:33

由于您可以访问当前显示的月份和年份,因此可以使用 QDate: :dayOfWeek 显示内容的第一个和最后一个日期。考虑到 QCalendarWidget::firstDayOfWeek ,您应该能够决定你必须来回走多远。

As you've got access to the currently shown month and year, you can use the QDate::dayOfWeek on the first and last date of the shown contents. Taking QCalendarWidget::firstDayOfWeek into account, you aught to be able to decide how far back and forth you have to go.

叫思念不要吵 2024-08-29 06:08:33

QCalendarWidget类是更简单的小部件的组合,您可以查看Qt源代码以获得帮助。只需使用循环即可获取以下日期之间的所有日期:

class myQCalendar(QCalendarWidget):
    """custum QCalendarWidget"""
    def __init__(self, parent=None):
        self.table = self.findChild(QTableView)

    def return_first_last_dates(self) -> Tuple[QDate, QDate]:
        # first row(0) and col(0) - headers, so we use second(1,1)
        first_date = self.date_by_index(
            self.table.model().index(1, 1)) 
        last_date = self.date_by_index(
            self.table.model().index(6, 7))
        return first_date, last_date
        # self.table.model().dateForCell(1,1) didn't work in python 
        # maybe it will work in c++


    def date_by_index(self, index: QModelIndex) -> QDate:
        """ Return QDate by index of model of QTableView """
        date = self.selectedDate()
        day = int(index.data())
        mnth = date.month()  # current month always the same as current date
        if day > 15 and index.row() < 3:  # qcalendar always display 6 rows( and 0 - is header)
            mnth = date.month() - 1
        if day < 15 and index.row() > 4:
            mnth = date.month() + 1
        return QDate(date.year(), mnth, day)

QCalendarWidget class is a combination of more simple widgets, you can see Qt source code for help. Just use loop to get all dates between:

class myQCalendar(QCalendarWidget):
    """custum QCalendarWidget"""
    def __init__(self, parent=None):
        self.table = self.findChild(QTableView)

    def return_first_last_dates(self) -> Tuple[QDate, QDate]:
        # first row(0) and col(0) - headers, so we use second(1,1)
        first_date = self.date_by_index(
            self.table.model().index(1, 1)) 
        last_date = self.date_by_index(
            self.table.model().index(6, 7))
        return first_date, last_date
        # self.table.model().dateForCell(1,1) didn't work in python 
        # maybe it will work in c++


    def date_by_index(self, index: QModelIndex) -> QDate:
        """ Return QDate by index of model of QTableView """
        date = self.selectedDate()
        day = int(index.data())
        mnth = date.month()  # current month always the same as current date
        if day > 15 and index.row() < 3:  # qcalendar always display 6 rows( and 0 - is header)
            mnth = date.month() - 1
        if day < 15 and index.row() > 4:
            mnth = date.month() + 1
        return QDate(date.year(), mnth, day)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文