我创建了一个“Slider”子类的QWidget,并希望能够使用Qt的样式表进行风格化.有没有办法将小部件声明到Qt应用程序,以便将应用程序样式表中的此设置应用于所有滑块?
Slider { background-color:blue; }@H_403_3@或者如果这是不可能的,我可以使用这样的类吗?
QWidget.slider { background-color:blue; }@H_403_3@
解决方法
这些小部件有一个可以通过元对象访问的“className()”方法.在我的情况下,这是:
slider.MetaObject()->className(); // ==> mimas::Slider@H_403_3@由于“Slider”类在命名空间中,因此您必须使用完全限定名称进行样式化(用’ – ‘替换’::’):
mimas--Slider { background-color:blue; }@H_403_3@.slider { background-color:blue; }@H_403_3@C滑块类:
Q_PROPERTY(QString class READ cssClass) ... QString cssClass() { return QString("slider"); }@H_403_3@在这个问题上,要绘制CSS中定义的颜色和样式的滑块,这就是你如何得到它们(link text):
// background-color: palette.color(QPalette::Window) // color: palette.color(QPalette::WindowText) // border-width: // not possible (too bad...). To make it work,you would need to copy paste // some headers defined in qstylesheetstyle.cpp for QRenderRule class inside,// get the private headers for QStyleSheetStyle and change them so you can call // renderRule and then you could use the rule to get the width borders. But your // code won't link because the symbol for QStyleSheetStyle are local in QtGui. // The official and supported solution is to use property: // qproperty-border: border_width_ // or whatever stores the Q_PROPERTY border@H_403_3@最后,来自CSS的QPalette值的注释:
color = QPalette::WindowText background = QPalette::Window alternate-background-color = QPalette::AlternateBase selection-background-color = QPalette::Highlighted selection-color = QPalette::HighlightedText@H_403_3@