我有一个部分是顶部小部件,顶部小部件的颜色是灰色,并且我在顶部小部件内放置了几个小部件,QComboBox、QLineEdit 和 2 QButton。
但是当我右键单击 QLineEdit 时,窗口的默认上下文颜色为灰色,或者当我打开 QComboBox 时背景颜色为灰色。
我已将这两个小部件的背景颜色设置为白色,但不起作用。
我该如何解决这个问题?
样式表传播到所有子窗口小部件,因此您必须使用正确的选择器来限制它们的范围。由于上下文菜单是 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 }");
有关详细信息,请参阅样式表语法 - 选择器类型。