在我的应用程序中,我有一个顶部小部件的部分,顶部小部件的颜色是灰色的,我已经将severl小部件放在顶部小部件上,如QComboBox,QLineEdit和2 QButton,但我有一个问题当我右键单击QLineEdit时,如下图所示,窗口默认上下文的颜色为灰色,或者当我打开QComboBox时,背景颜色为灰色。我将两个这些小部件的背景颜色设置为白色但不起作用。那么,我该如何解决这个问题?
示例以便更好地理解:
http://0000.4.img98.net/out.php/i52512_problem.png
请帮帮我
答案 0 :(得分:1)
样式表传播到所有子窗口小部件,因此您必须使用正确的选择器限制其范围。由于上下文菜单是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 Syntax - Selector Types"。