Style button background keeping rounded corners

倾然丶 夕夏残阳落幕 提交于 2019-12-25 08:17:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!