QCalendarWidget - 读取所选日期的背景颜色
QCalendarWidget - Reading background color of selected date
我正在尝试创建一个可以在点击时切换日期颜色的日历。如果当前背景为白色,则将其设置为绿色。如果它是绿色的,请将其设置为红色。如果它是红色的,请将其设置回白色。但是我不知道如何获得选择背景颜色。有人可以帮忙吗?
样式表已设置为摆脱默认选择颜色,它会阻止我想要显示的颜色。
import sys
from PySide.QtGui import (QCalendarWidget, QApplication, QBrush)
from PySide.QtCore import Qt
class UserCalendar(QCalendarWidget):
def __init__(self, parent=None):
super(UserCalendar, self).__init__(parent)
style = 'QTableView{selection-background-color: white;' \
'selection-color: black;}'
self.setStyleSheet(style)
self.clicked.connect(self.onClick)
def onClick(self, date):
brush = QBrush()
brush.setColor(Qt.green)
charformat = self.dateTextFormat(date)
charformat.setBackground(brush)
self.setDateTextFormat(date, charformat)
style = 'QTableView{selection-background-color: green;' \
'selection-color: black;}'
self.setStyleSheet(style)
if __name__ == '__main__':
app = QApplication(sys.argv)
cal = UserCalendar()
cal.show()
cal.raise_()
sys.exit(app.exec_())
与您使用 dateTextFormat(...)
设置背景颜色的方式相同,您可以使用 background()
获得颜色 returns QBrush(...)
,然后使用它的颜色(...) 方法 。默认情况下,颜色是黑色而不是观察到的白色。
import sys
from PySide.QtGui import (QCalendarWidget, QApplication, QBrush, QColor)
from PySide.QtCore import Qt, Slot, QDate
class UserCalendar(QCalendarWidget):
def __init__(self, parent=None):
super(UserCalendar, self).__init__(parent)
self.set_selection_color(QColor("white"))
self.clicked.connect(self.onClick)
@Slot(QDate)
def onClick(self, date):
color = self.get_next_color(date)
charformat = self.dateTextFormat(date)
charformat.setBackground(QBrush(color))
self.setDateTextFormat(date, charformat)
self.set_selection_color(color)
def set_selection_color(self, color):
style = 'QTableView{{selection-background-color: {color};' \
'selection-color: black;}}'.format(color=color.name())
self.setStyleSheet(style)
def get_next_color(self, date):
color = self.dateTextFormat(date).background().color()
# by default background color is Qt.black
if color in (QColor(Qt.black), QColor(Qt.white)) :
return QColor(Qt.green)
if color == QColor(Qt.green):
return QColor(Qt.red)
return QColor(Qt.white)
if __name__ == '__main__':
app = QApplication(sys.argv)
cal = UserCalendar()
cal.show()
cal.raise_()
sys.exit(app.exec_())
我正在尝试创建一个可以在点击时切换日期颜色的日历。如果当前背景为白色,则将其设置为绿色。如果它是绿色的,请将其设置为红色。如果它是红色的,请将其设置回白色。但是我不知道如何获得选择背景颜色。有人可以帮忙吗?
样式表已设置为摆脱默认选择颜色,它会阻止我想要显示的颜色。
import sys
from PySide.QtGui import (QCalendarWidget, QApplication, QBrush)
from PySide.QtCore import Qt
class UserCalendar(QCalendarWidget):
def __init__(self, parent=None):
super(UserCalendar, self).__init__(parent)
style = 'QTableView{selection-background-color: white;' \
'selection-color: black;}'
self.setStyleSheet(style)
self.clicked.connect(self.onClick)
def onClick(self, date):
brush = QBrush()
brush.setColor(Qt.green)
charformat = self.dateTextFormat(date)
charformat.setBackground(brush)
self.setDateTextFormat(date, charformat)
style = 'QTableView{selection-background-color: green;' \
'selection-color: black;}'
self.setStyleSheet(style)
if __name__ == '__main__':
app = QApplication(sys.argv)
cal = UserCalendar()
cal.show()
cal.raise_()
sys.exit(app.exec_())
与您使用 dateTextFormat(...)
设置背景颜色的方式相同,您可以使用 background()
获得颜色 returns QBrush(...)
,然后使用它的颜色(...) 方法 。默认情况下,颜色是黑色而不是观察到的白色。
import sys
from PySide.QtGui import (QCalendarWidget, QApplication, QBrush, QColor)
from PySide.QtCore import Qt, Slot, QDate
class UserCalendar(QCalendarWidget):
def __init__(self, parent=None):
super(UserCalendar, self).__init__(parent)
self.set_selection_color(QColor("white"))
self.clicked.connect(self.onClick)
@Slot(QDate)
def onClick(self, date):
color = self.get_next_color(date)
charformat = self.dateTextFormat(date)
charformat.setBackground(QBrush(color))
self.setDateTextFormat(date, charformat)
self.set_selection_color(color)
def set_selection_color(self, color):
style = 'QTableView{{selection-background-color: {color};' \
'selection-color: black;}}'.format(color=color.name())
self.setStyleSheet(style)
def get_next_color(self, date):
color = self.dateTextFormat(date).background().color()
# by default background color is Qt.black
if color in (QColor(Qt.black), QColor(Qt.white)) :
return QColor(Qt.green)
if color == QColor(Qt.green):
return QColor(Qt.red)
return QColor(Qt.white)
if __name__ == '__main__':
app = QApplication(sys.argv)
cal = UserCalendar()
cal.show()
cal.raise_()
sys.exit(app.exec_())