Claudiu Claudiu - 1 month ago 6
CSS Question

Style button background keeping rounded corners

I want to make a green button. I did this:

self.detectButton.setStyleSheet("background: #00ff00")


Here it was before the styling:

enter image description here

And after:

enter image description here

As you can see, the color changed, but the rounded corners were lost.

What's the proper way to style the color so the corners are not lost?

Secondarily, is there any way to get a theme-generic "ok" button color? This won't look the same on other platforms.

Answer

Do not use stylesheets if you want completely consistent results on all platforms. The reasons for this are explained in this answer.

If you only need to set the background colour, use the widget's palette:

palette = self.detectButton.palette()
palette.setColor(QtGui.QPalette.Button, QtGui.QColor('#00ff00'))
self.detectButton.setPalette(palette)

It seems unlikely that there's a systematic way to generate colours that work equally well on all platforms. Probably the best you can do is try to tweak one of the system colours that are accessible via the palette color roles.

EDIT:

It seems I was wrong to assume that the palette method is a reliable cross-platform solution. The Qt docs for QPalette has the following:

Warning: Some styles do not use the palette for all drawing, for instance, if they make use of native theme engines. This is the case for both the Windows XP, Windows Vista, and the OS X styles.

So it seems there isn't even a completely consistent, cross-platform way to do something as simple as changing the background colour.