尝试为日期单元格着色时出现问题
我在尝试使用在 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 原则背景。不过非常愿意学习。
I'm having some issues trying to re-color a date cell after its selection selection using a calendar created in QT Designer and converted via pyuic 4.
So far I have seen some similar questions about re-coloring cells or rows of tables/tree widgets - but these examples stem from extending base QCalendarWidget or Tree widget class before instantiating in code... whereas I'm using a QT Designer placed calendar widget converted via pyuic and instantiated in the converted python script.
Here is an example of my window main file where I'm trying to change the color of the date selection using the paintCell function of QCalendarWidget:
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_())
and here is the puic converted Qt Designer script:
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
When I run this, I'm getting the following log messages that pretty much tell me that something is going wrong:
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
I'm what you might consider a Junior Level coder (..or less) - I have a good deal of experience with python and a bit of QT inside Autodesk Maya and have a background in Technical Art - but probably not enough background in core OOP principles. Very willing to learn though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道你是否已经找到了这个问题的答案,但这里是
日历小部件实际上并没有太多用于显示所选日期,但更多的是用于选择日期。我认为它也在寻找 qwidget 当前正在使用的绘制类。
但是您可以重新实现 QCalendarWidget 并覆盖 PaintCell 调用来为您显示选定的日期
尽管现在您需要将其手动编码到您的小部件中,而不是在 Qt Designer 中使用它(除非您想将其变成插件) )
如果您从未解决它,希望对您有所帮助。
I don't know if you've found the answer for this ever/yet but here goes
The calendar widget doesn't really do much for showing selected dates, but more for selecting dates. It's also looking for the paint class that the qwidget is currently using, I think.
But you can re-implement the QCalendarWidget and overwrite the paintCell call to show selected dates for you
Though now you'll need to manually code it into your widget instead of using it in the Qt Designer (unless you feel like turning it into a plugin)
Hope that helps if you never solved it.