gpt4 book ai didi

python - 尝试为日期单元格着色时出现问题

转载 作者:行者123 更新时间:2023-12-01 06:05:48 25 4
gpt4 key购买 nike

我在尝试使用在 QT Designer 中创建并通过 pyuic 4 转换的日历对日期单元格进行选择后重新着色时遇到一些问题。

到目前为止,我已经看到了一些关于重新着色单元格或表格/树小部件行的类似问题 - 但这些示例源于在代码中实例化之前扩展基本 QCalendarWidget 或 Tree 小部件类...而我使用的是 QT设计器放置了通过 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 原则的背景。不过非常愿意学习。

最佳答案

我不知道你是否已经找到了这个问题的答案,但这里是日历小部件实际上并没有太多用于显示所选日期,但更多的是用于选择日期。我认为它也在寻找 qwidget 当前正在使用的绘制类。

但是您可以重新实现 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)])

关于python - 尝试为日期单元格着色时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8030612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com