PyQt question (colors)

Boudewijn Rempt boud at valdyas.org
Tue Apr 9 20:17:01 EDT 2002


Janos Blazi wrote:

> 
> Thank you for your answer.
> I tried to read the Trolltech manuals (and your book as well by the way)
> but to no avail. Ist it really so difficult to set the two colors of a
> progressbar? Does that require 20 lines of code?

Yes. If it isn't the backgroundcolor you want to set, (and you probably 
shouldn't want to do that, because should follow the user preferences, 
just like the face color of buttons and everything else), but the color of 
the bar itself -- by default black in some styles, blue in others, then I 
now remember I have come across the same problem when I wanted to build a 
unittest dialog for my book.

<rummages through code>

Yes, you can easily alter the bar color in PyQt, simply using a function 
like:

    def setForegroundColor(self, widget, color):
        palette=widget.palette()
        palette.setColor(QColorGroup.Highlight, color)
        widget.setPalette(palette)

that you then call with something like:

    self.setForegroundColor(self.mw.progressBar, QColor("green"))

If you really need to set the backgroundcolor (the part that is normally 
medium gray), then you're on your own still: you will have to experiment
with the various colors in QColorGroup:

const QColor & button () const
const QColor & light () const
const QColor & dark () const
const QColor & mid () const
const QColor & text () const
const QColor & base () const
const QColor & background () const
const QColor & midlight () const
const QColor & brightText () const
const QColor & buttonText () const
const QColor & shadow () const
const QColor & highlight () const
const QColor & highlightedText () const
const QColor & link () const
const QColor & linkVisited () const

And see which one is the one that fits your needs. The fiddling with the 
palette remains the same.

-- 
Boudewijn Rempt | http://www.valdyas.org



More information about the Python-list mailing list