用于定制Qt类的CSS选择器

前端之家收集整理的这篇文章主要介绍了用于定制Qt类的CSS选择器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我创建了一个“Slider”子类的QWidget,并希望能够使用Qt的样式表进行风格化.有没有办法将小部件声明到Qt应用程序,以便将应用程序样式表中的此设置应用于所有滑块?
  1. Slider { background-color:blue; }

或者如果这是不可能的,我可以使用这样的类吗?

  1. QWidget.slider { background-color:blue; }

解决方法

这些小部件有一个可以通过元对象访问的“className()”方法.在我的情况下,这是:
  1. slider.MetaObject()->className();
  2. // ==> mimas::Slider

由于“Slider”类在命名空间中,因此您必须使用完全限定名称进行样式化(用’ – ‘替换’::’):

  1. mimas--Slider { background-color:blue; }

另一个解决方案是定义一个类属性并使用它与一个前导点:

  1. .slider { background-color:blue; }

C滑块类:

  1. Q_PROPERTY(QString class READ cssClass)
  2. ...
  3. QString cssClass() { return QString("slider"); }

在这个问题上,要绘制CSS中定义的颜色和样式的滑块,这就是你如何得到它们(link text):

  1. // background-color:
  2. palette.color(QPalette::Window)
  3.  
  4. // color:
  5. palette.color(QPalette::WindowText)
  6.  
  7. // border-width:
  8. // not possible (too bad...). To make it work,you would need to copy paste
  9. // some headers defined in qstylesheetstyle.cpp for QRenderRule class inside,// get the private headers for QStyleSheetStyle and change them so you can call
  10. // renderRule and then you could use the rule to get the width borders. But your
  11. // code won't link because the symbol for QStyleSheetStyle are local in QtGui.
  12. // The official and supported solution is to use property:
  13.  
  14. // qproperty-border:
  15. border_width_ // or whatever stores the Q_PROPERTY border

最后,来自CSS的QPalette值的注释:

  1. color = QPalette::WindowText
  2. background = QPalette::Window
  3. alternate-background-color = QPalette::AlternateBase
  4. selection-background-color = QPalette::Highlighted
  5. selection-color = QPalette::HighlightedText

猜你在找的CSS相关文章