自定义 Qt 类的 CSS 选择器
我创建了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
小部件有一个“className()”方法,可以通过元对象访问。在我的例子中,这是:
由于“Slider”类位于命名空间中,因此您必须使用完全限定的名称进行样式设置(将“::”替换为“--”):
另一个解决方案是定义一个类属性并使用它带有一个前导点:
C++ Slider 类:
在主题上,要使用 CSS 中定义的颜色和样式绘制滑块,这就是获取它们的方式(链接文本):
最后,关于 CSS 中的 QPalette 值的注释:
The widgets have a "className()" method that is accessible via the meta object. In my case this is:
Since the "Slider" class is in a namespace, you have to use the fully qualified name for styling (replacing '::' with '--'):
Another solution is to define a class property and use it with a leading dot:
C++ Slider class:
While on the subject, to draw the slider with colors and styles defined in CSS, this is how you get them (link text):
And finally, a note on QPalette values from CSS: