QComboBox text colour won't change with style sheet

微笑、不失礼 提交于 2019-12-08 23:49:48

问题


I'm trying to style a combobox in QT5. I'm using QT Creator for the layout and loading an app-wide style sheet at start up.

The css I have related to my combobox is as follows:

QComboBox
{
    color:white;
    background-color: qlineargradient(x1:0, y1:0, x2:1,y2:1, stop: 1 rgba(228, 41, 81, 100), stop: 0 rgba(234, 107, 101, 100));
    border-color: rgba(255,255,255,200);
    border-width: 1px;
    border-style: solid;
}

QComboBox QListView
{
    border-style: none;
    background-color: qlineargradient(x1:0, y1:0, x2:1,y2:0, stop: 1 rgba(228, 41, 81, 100), stop: 0 rgba(234, 107, 101, 100));
}

QComboBox::drop-down
{
    width: 20px;
    border: 1px;
    border-color:white;
    border-left-style:solid;
    border-top-style: none;
    border-bottom-style: none;
    border-right-style: none;
}

QComboBox::down-arrow
{
    image: url(:/ArrowImages/images/whitearrowdown16.png);
    width: 16px;
    height: 16px;
}

But the text colour in the combo box remainds as the default (black) colour. The colour in the drop down is white. The border colour and styling all work correctly. Is the label on the combobox some sort of sub-control I need to style separately? Or am I missing something else?

Thanks.

Edit:

Added screenshots for clarity

Edit 2: It looks like this only occurs when the combobox is set to not be editable (which is the correct behaviour for my program, so doesn't really help me.) When the combobox is set to editable, it obeys styles correctly. I've tried adding

QCombobox:!editable
{
    color:white;
}

but it doesn't fix the problem.


回答1:


Only just resolved this. It seems setting the padding property (with any value) on the combobox in the style sheet makes it properly obey the colour styling. I'm assuming it's down to some sort of bug that might only arise on certain set ups, but if anyone else is having the same problem, the following code would work (when compared with that in the original question):

QComboBox
{
    color:white;
    background-color: qlineargradient(x1:0, y1:0, x2:1,y2:1, stop: 1 rgba(228, 41, 81, 100), stop: 0 rgba(234, 107, 101, 100));
    border-color: rgba(255,255,255,200);
    border-width: 1px;
    border-style: solid;
    padding: 1px 0px 1px 3px; /*This makes text colour work*/
}



回答2:


The View "inside" is a QListView.

QListView
{
  color: white;
}

should do the trick.




回答3:


Using padding as a workaround might introduce some other problems. Perhaps setting selection-color should resolve this issue.

QComboBox
{
   selection-color: white;
}


来源:https://stackoverflow.com/questions/24447276/qcombobox-text-colour-wont-change-with-style-sheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!