我在使用在QT Designer中创建并通过pyuic 4转换的日历选择选择后重新着色日期单元时遇到了一些问题。
到目前为止,我已经看到了一些关于重新着色单元格或表/树小部件行的类似问题 - 但是这些示例源于在代码实例化之前扩展基础QCalendarWidget或Tree小部件类...而我正在使用QT Designer通过pyuic转换了日历小部件,并在转换后的python脚本中实例化。
以下是我的窗口主文件示例,我正在尝试使用QCalendarWidget的paintCell函数更改日期选择的颜色:
import os, sys
from PyQt4 import QtCore, QtGui
from calanderTestWindow import Ui_calanderTestWindow
class Main(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui = Ui_calanderTestWindow()
self.ui.setupUi(self)
self.conncectSignals()
def conncectSignals(self):
QtCore.QObject.connect(self.ui.testCalander, QtCore.SIGNAL('selectionChanged()'), self.clickDate)
def clickDate(self):
painter = QtGui.QPainter()
painter.setPen(QtGui.QColor(0,255,255))
date = self.ui.testCalander.selectedDate()
cellRect = QtCore.QRect(0,0,10,10)
self.ui.testCalander.paintCell(painter, cellRect, date)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = Main()
window.show()
sys.exit(app.exec_())
这是puic转换的Qt Designer脚本:
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_calanderTestWindow(object):
def setupUi(self, calanderTestWindow):
calanderTestWindow.setObjectName(_fromUtf8("calanderTestWindow"))
calanderTestWindow.resize(262, 203)
calanderTestWindow.setWindowTitle(QtGui.QApplication.translate("calanderTestWindow", "Calendar Test Window", None, QtGui.QApplication.UnicodeUTF8))
self.centralwidget = QtGui.QWidget(calanderTestWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.testCalander = QtGui.QCalendarWidget(self.centralwidget)
self.testCalander.setGeometry(QtCore.QRect(0, 0, 256, 155))
self.testCalander.setGridVisible(True)
self.testCalander.setVerticalHeaderFormat(QtGui.QCalendarWidget.NoVerticalHeader)
self.testCalander.setNavigationBarVisible(True)
self.testCalander.setObjectName(_fromUtf8("testCalander"))
calanderTestWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(calanderTestWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 262, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
calanderTestWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(calanderTestWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
calanderTestWindow.setStatusBar(self.statusbar)
self.retranslateUi(calanderTestWindow)
QtCore.QMetaObject.connectSlotsByName(calanderTestWindow)
def retranslateUi(self, calanderTestWindow):
pass
当我运行它时,我收到以下日志消息,这些消息几乎告诉我出现了问题:
QPainter::setPen: Painter not active
QPainter::save: Painter not active
QPainter::setClipRect: Painter not active
QPainter::brushOrigin: Painter not active
QPainter::setBrushOrigin: Painter not active
QPainter::setBrushOrigin: Painter not active
QPainter::setPen: Painter not active
QPainter::pen: Painter not active
QPainter::save: Painter not active
QPainter::setBackgroundMode: Painter not active
QPainter::setBrush: Painter not active
QPainter::setBrushOrigin: Painter not active
QPainter::setPen: Painter not active
QPainter::drawRects: Painter not active
QPainter::drawRects: Painter not active
QPainter::drawRects: Painter not active
QPainter::drawRects: Painter not active
QPainter::restore: Unbalanced save/restore
QPainter::restore: Unbalanced save/restore
我是你可能认为的初级编码器(或者更少) - 我在Autodesk Maya中有很多python和一些QT的经验,并且有技术艺术背景 - 但可能还不够核心OOP原则的背景。非常愿意学习。 任何帮助将不胜感激。
干杯,d。
答案 0 :(得分:1)
我不知道你是否已经找到了这个问题的答案,但是现在这样 日历小部件对于显示所选日期并没有太大作用,但更多用于选择日期。它也在寻找qwidget目前使用的paint类,我想。
但您可以重新实现QCalendarWidget并覆盖paintCell调用以显示您选择的日期
from PyQt4 import QtCore, QtGui
class dateCalendar(QtGui.QCalendarWidget)
def __init__(self, parent = None):
super(calendar, self).__init__(parent)
self.color = QtGui.QColor(self.palette().color(QtGui.QPalette.Highlight))
self.color.setAlpha(150)
#self.selectionChanged.connect(self.updateCells)
self.dateList = []
def paintCell(self, painter, rect, date):
#calling original paintCell to draw the actual calendar
QtGui.QCalendarWidget.paintCell(self, painter, rect, date)
#highlight a particular date
if date in self.dateList:
painter.fillRect(rect, self.color)
def selectDates(self, qdatesList):
self.dateList = qdatesList
#this redraws the calendar with your updated date list
self.updateCells()
虽然现在您需要手动将其编码到您的小部件中,而不是在Qt Designer中使用它(除非您想将其变成插件)
希望如果你从未解决过它会有所帮助。
class widget(QtGui.QWidget):
def __init__(self, parent = None):
super(empty, self).__init__(parent)
self.setGeometry(300, 300, 280, 170)
#no layout
self.cal = calendar(self)
self.but = QtGui.QPushButton("Push", self)
self.but.clicked.connect(self.addDate)
def addDate(self):
self.cal.selectDates([QtCore.QDate(2012,10,8), QtCore.QDate(2012,10,5)])