Damo
Damo

Reputation: 11

issues trying to color date cell

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.

Upvotes: 1

Views: 3605

Answers (1)

ooklah
ooklah

Reputation: 511

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

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

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.

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

Upvotes: 2

Related Questions