[PyQt] button color

David Boddie david at boddie.org.uk
Sun Jul 15 19:41:56 BST 2007

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.


More information about the PyQt mailing list