在 Qt5 日历小部件中突出显示日期间隔 [英] Highlight date interval in a Qt5 Calendar Widget

查看:115
本文介绍了在 Qt5 日历小部件中突出显示日期间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 CalendarWidget 中突出显示所选开始日期和结束日期之间的每一天.我的问题是 CalendarWidget 只允许 QTCreator 中的 SingleSelection 但说其他事情可以通过编程方式更改.

I want to highlight every day in a CalendarWidget that is between a selected start and end date. My problem is that a CalendarWidget only allows SingleSelection in QTCreator but says that other things can be changed programmatically though.

我发现了一些使用 QPainter 和 paintCell() 方法的提示,但我直到不知道从哪里开始.互联网对我没有帮助.我尝试先在 buttonClick 上更改单个日期,但即使这样也不起作用,您能给我建议如何使用它吗?

I found some hints to use a QPainter and the paintCell() Method but i till dont know where to begin. The Internet wasn't helpful in my case. I tried to change a single date first on buttonClick but even this didnt work, can you give me an advice how to use this?

btn_test_pressed(self):
    painter = QPainter()
    painter.setPen(QtGui.QPen(QtCore.Qt.green))
    painter.fillRect(QtCore.QRectF(250, 250, 10, 10), 0, 5760)
    rect = QRect()
    date = datetime.datetime.now() - datetime.timedelta(1)
    self.calendarWidget.paintCell(painter, rect, date)

推荐答案

要更新单个日期的样式,您可以使用 QCalendarWidget.setDateTextFormat().这是如何使用它来突出显示一系列日期的基本实现,这些日期可以通过在按住 shift 键的同时选择开始和结束日期来选择.

To update the style of individual dates you can use QCalendarWidget.setDateTextFormat(). Here is a basic implementation of how this is used to highlight a range of dates which can be selected by selecting a begin and and end date while holding the shift key.

from PyQt5.QtGui import QPalette, QTextCharFormat
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QCalendarWidget

class MyCalendar(QCalendarWidget):
    def __init__(self):
        super().__init__()
        self.begin_date = None
        self.end_date = None

        self.highlight_format = QTextCharFormat()
        self.highlight_format.setBackground(self.palette().brush(QPalette.Highlight))
        self.highlight_format.setForeground(self.palette().color(QPalette.HighlightedText))

        self.clicked.connect(self.date_is_clicked)
        print(super().dateTextFormat())

    def format_range(self, format):
        if self.begin_date and self.end_date:
            d0 = min(self.begin_date, self.end_date)
            d1 = max(self.begin_date, self.end_date)
            while d0 <= d1:
                self.setDateTextFormat(d0, format)
                d0 = d0.addDays(1)

    def date_is_clicked(self, date):
        # reset highlighting of previously selected date range
        self.format_range(QTextCharFormat())
        if QApplication.instance().keyboardModifiers() & Qt.ShiftModifier and self.begin_date:
            self.end_date = date
            # set highilighting of currently selected date range
            self.format_range(self.highlight_format)
        else:
            self.begin_date = date
            self.end_date = None

if __name__ == "__main__":
    app = QApplication([])
    calendar = MyCalendar()
    calendar.show()
    app.exec()

截图:

这篇关于在 Qt5 日历小部件中突出显示日期间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆