如何使 Qt 小部件忽略父小部件上设置的样式表?

发布于 2024-09-01 06:42:36 字数 389 浏览 2 评论 0原文

在 Qt Designer 中添加 QComboBox 控件时,我得到一个看起来很糟糕的非本机控件:

在此处输入图像描述

进一步挖掘,发现两个父控件 QParentWindow 和 QStackedWidget 具有 QComboBox 的样式表正在继承。如果我删除自定义样式,那么我会得到一个本机 QComboBox,如左侧所示。

我怎样才能让 QComboBox (以及一般的小部件)不继承父样式?或者,我如何为 QParentWindow 创建样式,并使其仅是本地的并且不会级联?

When adding a QComboBox control in Qt Designer, I get a terrible looking, non-native control:

enter image description here

On digging further, it turns out that two of the parent controls, QParentWindow and QStackedWidget, have style sheets that QComboBox is inheriting. If I delete the custom styles, then I get a native QComboBox like the one on the left.

How can I have QComboBox (and widgets generally) NOT inherit parent styles? Or, how can I create a style for, say, QParentWindow, and do it so that it's local only and does not cascade?

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

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

发布评论

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

评论(2

不语却知心 2024-09-08 06:42:36

我认为你无法阻止它级联。但是通过在样式表中使用更具体的选择器,也许您可以仅为您的 QParentWindow 类或特定对象定义属性。

I don't think you can prevent it from cascading. But by using more specific selectors in your stylesheet, maybe you could define properties only for your QParentWindow class or specific object.

烂人 2024-09-08 06:42:36

您需要定义一个样式,然后将其分配给该对象:

QString settingStyle = " QGroupBox#groupBoxSettings {\
        background-color: rgb(248,248,248);\
        border: 1px solid rgb(170, 170, 255);\
        border-radius: 3px;\
        border-color:rgb(170, 170, 255);\
}";


ui->groupBoxSettings->setStyleSheet(settingStyle);

这里“groupBoxSettings”是对象名称。这样,组框中的任何内容都会有自己的风格。

you need to define a style and then assign it to that object:

QString settingStyle = " QGroupBox#groupBoxSettings {\
        background-color: rgb(248,248,248);\
        border: 1px solid rgb(170, 170, 255);\
        border-radius: 3px;\
        border-color:rgb(170, 170, 255);\
}";


ui->groupBoxSettings->setStyleSheet(settingStyle);

here "groupBoxSettings" is the object name. This way any thing inside the groupbox they'll have their own style.

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