是否可以在 Qt Creator 中将 Qt 样式表与升级的小部件一起使用?
我正在尝试使用 Qt 样式表对标准小部件进行一些重大的重新设计。因此,在通过 #objectName
选择器手动为不同的小部件完成大部分工作后,我决定以某种方式对类似的小部件进行分组。
例如,我有多个 QFrame,其作用类似于内部表单中的标题。我希望它们都具有相同的样式表。一种方法是使用命名约定(这是我的后备变体),即 QFrame[objectName|="prefix_"]
。但我想按类别对我的小部件进行分组。因此,我创建了简单的占位符类:
class HeaderFrame: public QFrame
{
public:
HeaderFrame(QWidget *parent = NULL): QFrame(parent) {}
};
这使我能够将所有这些 QFrames
提升为 HeaderFrames
。之后,我尝试将
HeaderFrame { background-color: red; }
样式表设置为 MainWindow 对象本身(以使其作用于所有 HeaderFrame),但它不起作用。 QtCreator 表单设计器中没有任何更改,编译应用程序后也没有任何更改。我尝试过此样式表的不同变体,但没有任何效果。
那么,是否只有 Qt 小部件(如 QLabel、QFrame 等)可用于这种方式的样式设置?或者有什么方法可以为您升级的小部件编写样式表?
I'm trying to do some heavy redeisgning of standard widgets using Qt Style Sheets. So after doing most of it manually for different widgets by #objectName
selectors I've decided to group similar widgets in some way.
For example, I have multiple QFrames
which act like headers in inner forms. And I want all of them to have the same stylesheet. One way of doing that is using naming convention (this is my fallback variant), i.e. QFrame[objectName|="prefix_"]
. But I wanted to group my widgets by class. So I created simple placeholder class:
class HeaderFrame: public QFrame
{
public:
HeaderFrame(QWidget *parent = NULL): QFrame(parent) {}
};
Which allowed me to promote all these QFrames
to HeaderFrames
. After that I've tried setting
HeaderFrame { background-color: red; }
stylesheet to MainWindow object itself (to make it act on all HeaderFrames) but it won't work. Nothing is changed in QtCreator form designer and nothing is changed after compiling an application. I've tried different variants of this stylesheet but nothing works.
So, is only Qt widgets (like QLabel, QFrame, etc.) are available for styling this way? Or there is some way to write stylesheet for your promoted widget?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这是可能的。您应该记住的唯一一件事 - 派生小部件的基础应该支持样式表,并仔细重新实现它们的 PaintEvent。
样式表:
yes,it is possible. The only thing you should keep in mind - base for your derived widgets should support style sheets, and reimplement their PaintEvent carefully.
Stylesheet:
您始终可以将
class-name
添加到对象的类属性(即 QStringList),并使用.class-name
选择器。You can always add
class-name
to object's class property, which is QStringList, and use.class-name
selector.