自定义 Qt 类的 CSS 选择器

发布于 2024-10-10 11:24:50 字数 257 浏览 0 评论 0原文

我创建了 QWidget 的“Slider”子类,并希望能够使用 Qt 的样式表对其进行样式设置。有没有办法向 Qt 应用程序声明小部件,以便应用程序样式表中的此设置应用于所有滑块?

Slider { background-color:blue; }

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

QWidget.slider { background-color:blue; }

I created a "Slider" subclass of QWidget and would like to be able to style it with Qt's stylesheets. Is there a way to declare the widget to Qt application so that this setting in the application stylesheet is applied to all sliders ?

Slider { background-color:blue; }

Or if this is not possible, can I use a class like this ?

QWidget.slider { background-color:blue; }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

錯遇了你 2024-10-17 11:24:55

小部件有一个“className()”方法,可以通过元对象访问。在我的例子中,这是:

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 中定义的颜色和样式绘制滑块,这就是获取它们的方式(链接文本):

// 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 中的 QPalette 值的注释:

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

The widgets have a "className()" method that is accessible via the meta object. In my case this is:

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

Since the "Slider" class is in a namespace, you have to use the fully qualified name for styling (replacing '::' with '--'):

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

Another solution is to define a class property and use it with a leading dot:

.slider { background-color:blue; }

C++ Slider class:

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

While on the subject, to draw the slider with colors and styles defined in CSS, this is how you get them (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

And finally, a note on QPalette values from CSS:

color                      = QPalette::WindowText
background                 = QPalette::Window
alternate-background-color = QPalette::AlternateBase
selection-background-color = QPalette::Highlighted
selection-color            = QPalette::HighlightedText
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文