[PyQt] button color

Katja Wegner k.wegner at herts.ac.uk
Mon Jul 16 18:49:21 BST 2007

Hi David,

thanks for the fast answer. I checked the returned colors and all alpha values are 255, thus no semi-transparent colors.

I checked it in my program, again:
linux 4.2.1 (py2.4) = works fine
windows 4.2.2 (py2.5) = only the border is changed but not the whole button (see attachment).

And I use button.palette() now.


Date: Sun, 15 Jul 2007 20:41:56 +0200
From: David Boddie <david at boddie.org.uk>
Subject: Re: [PyQt] button color
To: pyqt at riverbankcomputing.com
Message-ID: <200707152041.56681.david at boddie.org.uk>
Content-Type: text/plain; charset=us-ascii

On Sun Jul 15 15:02:24 BST 2007, K.Wegner wrote:

> > Hi, in my program the user presses a button and gets the color dialog where
> > he can choose a color. after that I want to change the color of this button
> > to the new color but only the border is set to a new color and not the
> > background of the button. (PyQt4 and Qt 4.2.1, Linux and Windows)

I initially experienced incorrect behaviour with PyQt4 and Qt 4.2.2 (Linux)
but this was due to a misunderstanding.

> > newColor = QColorDialog.getColor(self.color)
> > if (newColor.isValid()):
> >    self.color = newColor
> >    palette = QPalette()
> >    palette.setColor(QPalette.Button, self.color)
> >    self.colorButton.setPalette(palette)
> >    self.colorButton.setAutoFillBackground(TRUE)
> >    Btw it worked fine with Qt3.

This should work. Are you sure you've got the colour you expect? I was
initially selecting a colour in the dialog then hitting return. It turns
out that you get a valid transparent colour in some situations, and that
means that the button's palette isn't updated, I think.

You might also want to obtain the button's existing palette rather than
creating a new one each time. Use self.colorButton.palette() to get it.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: buttons.pdf
Type: application/pdf
Size: 43891 bytes
Desc: not available
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20070716/b832ff69/buttons-0001.pdf

More information about the PyQt mailing list