保持圆角的样式按钮背景 [英] Style button background keeping rounded corners

查看:39
本文介绍了保持圆角的样式按钮背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个绿色按钮.我是这样做的:

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

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

这是在样式之前:

之后:

如您所见,颜色发生了变化,但圆角丢失了.

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?

其次,有没有办法获得主题通用的ok"按钮颜色?这在其他平台上看起来不一样.

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

推荐答案

如果您希望在所有平台上获得完全一致的结果,请不要使用样式表.原因在这个答案中有解释.

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.

编辑:

我认为调色板方法是可靠的跨平台解决方案似乎是错误的.QPalette 的 Qt 文档包含以下内容:

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:

警告:有些样式不使用所有绘图的调色板,因为例如,如果他们使用原生主题引擎.这是这种情况适用于 Windows XP、Windows Vista 和 OS X 样式.

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.

这篇关于保持圆角的样式按钮背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆