Qt 样式表;褪色、2D 渐变和 CSS 类
我对 Qt 的样式表功能有一些疑问。这确实很棒,但感觉这个功能还没有出现太久,是吗?不过,这是迄今为止设计 GUI 样式的最简单方法。
是否可以在样式表中添加颜色渐变?每当鼠标悬停在某个小部件上时,我不希望它突然改变背景颜色,只需在 200 毫秒左右淡入新颜色即可。有没有一个好的方法,或者必须以代码方式完成?
我可以有二维渐变吗?我现在知道如何使用一维渐变,您可以在一个轴上逐渐改变颜色(通常是水平或垂直)。我想在其之上添加一个第二渐变,例如,它的 alpha 值较低。因此,如果您的渐变从绿色(顶部)到红色(底部),我也希望它从透明(左)到白色(右)。
Qt 有用于类型(例如 QPushButton)和 ID(例如 #mywidgetname)的 CSS 选择器,但我还没有找到选择或设置类的方法。例如,我有许多 QFrame,并且对于某个子集,我想添加一种特定的样式。我应该为我的框架命名为相同的名称(相同的 ID)吗?听起来很糟糕。但是选择它们的类型(QFrame)也不正确...
谢谢!
I'm having some questions about Qt's stylesheet functionality. It's really great, but it feels like the feature isn't around for too long yet, true? It's by far the easiest way to style my GUI though.
Is it possible to add color fading in the stylesheets? Whenever the mouse hovers over a certain widget, I don't want it abruptly changing background color, just fade into the new color in 200ms or something. Is there a nice way for this, or must this be done code-wise?
Can I have a 2D gradient? I know how to use the 1D gradient now, you can change colour gradually over one axis (often either horizontally or vertically). I would like to add a second gradient on top of that, that has a has a low alpha value for example. So if your gradient goes from green (top) to red (bottom), I would also like it to go from transparent (left) to white (right).
Qt has CSS selectors for types (e.g. QPushButton) and for ID's (e.g. #mywidgetname), but I haven't found a way to select or set classes. I have a number of QFrames for example, and to a certain subset I would like to add one particular style. Should I name my frames all the same (same ID)? Sounds bad. But selecting on their type (QFrame) isn't right either...
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有使用据我所知的 CSS。然而,Qt 有几个很好的演示,使用不同的技术(使用动画框架),例如,请参阅演示/示例浏览器。
通过使用端点的相对坐标使用以下构造,您可能可以在 #2 中实现您想要的效果:
Qt Designer 有一个很好的渐变编辑器(以及一般的 CSS),您可能想使用它,看看它会产生什么,并使用它来获取灵感。
不太确定如何最好地解决这个问题,但您可以在同一规则之前放置多个匹配项,这样您就可以:
Not using CSS that I know of. However, Qt has several nice demos using different techniques (using the animation framework), see for example the demos/examples browser.
You can probably achieve what you want in #2 by using the following construction using relative coordinates of the endpoints:
Qt Designer has a nice editor for gradients (and for CSS in general), you may want to play with this and see what it comes up with and use that for inspiration.
Not quite sure how to best solve this, but you can put multiple matches before the same rule, so you can have:
我知道这是一个很老的问题,但我最近遇到了类似的问题。
不管怎样,为了“3”。我发现您可以在 qss 中使用“属性选择器”,只需在相关小部件上设置属性值即可。
例如,在您的 C++ 代码中:
然后在 qss 中:
然后,任何“StyleClass”属性设置为“MyCustomLAF”的 QWidget(或派生类)实例都将应用 color:purple 样式。如果以下解释有点令人困惑或技术上不正确,请原谅我,但我的意思是属性“StyleClass”是 Qt 所谓的“动态属性”,(用我的话来说)意味着添加到 QObject 实例的属性在运行时,无需使用 Q_PROPERTY 宏在“元内容”中注册。
I know this is quite an old question, but I recently had a similar problem.
Anyway, for "3." I found you can use a "property selector" in the qss and just set the property value on the relevant widgets.
e.g. in your C++ code:
then in the qss:
Then any QWidget (or derived class) instance with the "StyleClass" property set to "MyCustomLAF" will have the color:purple style applied. Forgive me if the following explanation is a bit confusing or technically incorrect, but I'm implying that the property "StyleClass" is what Qt calls a "dynamic property" which (in my words) means a property that is added to a QObject instance at runtime, without being registered in the "meta stuff" using the Q_PROPERTY macro.
您还可以选择特定父级的所有 QFrame
#ParentName > QFrame
将选择 #ParentName 的子级的所有 QFrame#ParentName QFrame
将选择 #ParentName 及其子级中包含的所有 QFrameAlso you can select all QFrames for particular parent
#ParentName > QFrame
will select all QFrames that are children of #ParentName#ParentName QFrame
will select all QFrames contained in #ParentName and it's children