文本颜色不会随样式表改变

文本颜色不会随样式表改变

本文介绍了QComboBox 文本颜色不会随样式表改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 QT5 中设置组合框的样式.我使用 QT Creator 进行布局并在启动时加载应用范围的样式表.

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.

与我的组合框相关的 css 如下:

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?

谢谢.

为清晰起见添加了屏幕截图

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;
}

但它并没有解决问题.

推荐答案

才刚刚解决了这个问题.似乎在样式表中的组合框上设置填充属性(具有任何值)使其正确遵守颜色样式.我假设这归结为可能仅在某些设置上出现的某种错误,但如果其他人遇到同样的问题,以下代码将起作用(与原始问题中的代码相比):

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*/
}

这篇关于QComboBox 文本颜色不会随样式表改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 04:18