如何为包含的小部件设置与容器不同的样式表

发布于 2024-12-04 08:50:38 字数 206 浏览 2 评论 0原文

我有一个部分是顶部小部件,顶部小部件的颜色是灰色,并且我在顶部小部件内放置了几个小部件,QComboBox、QLineEdit 和 2 QButton。

但是当我右键单击 QLineEdit 时,窗口的默认上下文颜色为灰色,或者当我打开 QComboBox 时背景颜色为灰色。

我已将这两个小部件的背景颜色设置为白色,但它不起作用。

我该如何解决这个问题?

I have a section that is a top widget, the color of the top widget is gray, and I've put several widgets inside the top widget, QComboBox, QLineEdit and 2 QButton.

But when I right click on QLineEdit, the color of default context of window is gray, or when I open the QComboBox the color of background is gray.

I've set the background color of these two widgets to white but it doesn't work.

How can I fix this?

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

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

发布评论

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

评论(1

甜尕妞 2024-12-11 08:50:38

样式表传播到所有子窗口小部件,因此您必须使用正确的选择器来限制它们的范围。由于上下文菜单是 QLineEdit 的子菜单,因此它也会受到影响。

// What you have probably done:
myLineEdit->setStyleSheet("background-color: gray");

// What you should have done:
myLineEdit->setStyleSheet("QLineEdit { background-color: gray }");      

// What you should do if there might be child widgets of the same type 
// but for which you don't want the style to apply:
myLineEdit->setObjectName("myLineEdit");
myLineEdit->setStyleSheet("QLineEdit#myLineEdit { background-color: gray }");

有关详细信息,请参阅样式表语法 - 选择器类型

The style sheet propagates to all the child widgets, so you have to limit their range by using the right selectors. Since the context menu is a child of the QLineEdit it is also affected.

// What you have probably done:
myLineEdit->setStyleSheet("background-color: gray");

// What you should have done:
myLineEdit->setStyleSheet("QLineEdit { background-color: gray }");      

// What you should do if there might be child widgets of the same type 
// but for which you don't want the style to apply:
myLineEdit->setObjectName("myLineEdit");
myLineEdit->setStyleSheet("QLineEdit#myLineEdit { background-color: gray }");

See The Style Sheet Syntax - Selector Types for details.

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