[PyKDE] AttributeError: setBackgroundColor

Paul Evans pevans at catholic.org
Sat Dec 13 22:40:01 GMT 2003


On December 13, 2003 11:50 am, Stefan Seefeld wrote:
> I'v been playing with pyqt a bit, and I run into
> a problem with some of the examples. the QWidget's
> 'setBackgroundColor' method doesn't seem to be
> accessible:
>
> bash-2.05b$ examples2/splitter.py
> Traceback (most recent call last):
>    File "examples2/splitter.py", line 39, in ?
>      t1.setBackgroundColor(Qt.blue.light(180))
> AttributeError: setBackgroundColor

You need to use colour groups instead like this:

        self.cg = self.cus_listView.colorGroup()
        self.bgcolour = self.cg.color(self.cg.Highlight)
        self.fgcolour = self.cg.color(self.cg.HighlightedText)

        self.todayBtn.setPaletteBackgroundColor(self.bgcolour)
        self.todayBtn.setPaletteForegroundColor(self.fgcolour)


I grabbed the cg from an existing object, but you could use the default cg 
instead. The above is just swapping some colours around.

BTW, it's also how you get custom highlights or text colours in listviews, if 
you subclass the QListViewItem. That might look like this:

class TodoChildItem(QListViewItem):
    def paintCell(self, painter, qg, column, width, align):
        #c = qg.base()
        #colour = qg.color(qg.Background) #Qt.red #qg.color(qg.BrightText)
        #qg.setColor(QColorGroup.Base, colour)
        #QListViewItem.paintCell(self, painter, qg, column, width, align)
        #qg.setColor(QColorGroup.Base, c)
        c = qg.text()
        colour = qg.color(qg.Dark)
        qg.setColor(QColorGroup.Text, colour)
        QListViewItem.paintCell(self, painter, qg, column, width, align)
        qg.setColor(QColorGroup.Text, c)

I chose the above, because it was the first time I did it and you can still 
see where I was playing with things.

Hope that helps.

-- 
Regards, Paul Evans





More information about the PyQt mailing list