问题
I want to make a green button. I did this:
self.detectButton.setStyleSheet("background: #00ff00")
Here it was before the styling:
And after:
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.
回答1:
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.
回答2:
I'm not familiar with Python, but I am assuming it removed all the other styles when you manually set that one.
I wonder if something like the following would work?
self.detectButton.setStyleSheet("background: #00ff00; border:1px solid #ccc; border-radius: 3px;")
It may have different syntax for multiple styles though.
来源:https://stackoverflow.com/questions/40294339/style-button-background-keeping-rounded-corners