2011-11-06 34 views
1

我有一些问题想重新色色日期电池使用Qt Designer创建和4PyQt的日历 - 问题试图

到目前为止,通过pyuic转换日历其选择的选择之后的日期格老子已经看到了一些关于重新着色单元格或行/表格/树部件的类似问题 - 但这些示例源于在代码中实例化之前扩展基本QCalendarWidget或树部件类...而我正在使用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设计脚本:

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。

回答

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的使用它的设计师(除非你想把它变成一个插件)

希望如果你从来没有解决它的帮助。

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)])