自定义Qt类的CSS选择器 [英] CSS selector for custom Qt class

查看:28
本文介绍了自定义Qt类的CSS选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个QWidget的"Slider"子类,希望能够用Qt的样式表设置它的样式。是否有办法将小工具声明为Qt应用程序,以便将应用程序样式表中的此设置应用于所有滑块?

Slider { background-color:blue; }

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

QWidget.slider { background-color:blue; }

推荐答案

小部件有一个可通过元对象访问的"类名称()"方法。在我的案例中是:

slider.metaObject()->className();
// ==> mimas::Slider

由于"Slider"类位于命名空间中,因此您必须使用样式的全限定名称(将‘::’替换为‘--’):

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

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

.slider { background-color:blue; }

C++Slider类:

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

在主题上,要使用在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

最后,关于来自css的QPalet值的说明:

color                      = QPalette::WindowText
background                 = QPalette::Window
alternate-background-color = QPalette::AlternateBase
selection-background-color = QPalette::Highlighted
selection-color            = QPalette::HighlightedText

这篇关于自定义Qt类的CSS选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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