样式QProgressBar值为16时

时间:2015-04-07 19:30:47

标签: qt qtstylesheets qprogressbar

当值为QProgressBar示例时,可以仅使用QSS设置16的样式吗?

ui->progresso->setValue(16);

使用像这样的QSS:

QProgressBar {
    //Default QSS
    ...
}

QProgressBar:value(16) {
    background-color: #fc0;
}

我的目标是: - 当QProgressBar为0时:它将使用background-color: transparent - 当QProgressBar大于0时:显示灰色条和" chunk"将是蓝色的 - 当QProgressBar大于89时:显示" chunk"红色。

我可以使用QT + C++执行此操作,但想知道是否可以仅使用QSS执行此操作?

像这样(这段代码不存在,只是一个例子):

QProgressBar {
    background-color: gray;
}

QProgressBar:value(0) {
    background-color: transparent;
}

QProgressBar::chunk {
    background-color: blue;
}

QProgressBar::chunk:minValue(90) {
    background-color: red;
}

2 个答案:

答案 0 :(得分:1)

我认为有可能在Property Selector的帮助下,但仅限于exect值,即:

QProgressBar[value = 16]::chunk{
    background-color: red;
}

但您可以在代码中为每个值生成这样的stilesheet

QString styleSheet;
for(int i = 0; i < 101; i++)
{
    styleSheet.append(QString("QProgressBar[value = \"%1\"]::chunk{background-color: %2;}").arg(QString::number(i), (i < 17 ? "red" :"blue")));
}
myProgressBar->setStyleSheet(styleSheet);

我不尝试。它只是一个基于文档的理论 更新1

Warning: If the value of the Qt property changes after the style sheet has been set, it might be necessary to force a style sheet recomputation. One way to achieve this is to unset the style sheet and set it again. 

答案 1 :(得分:0)

这是不可能的。

唯一有效的扩展名在documentation,而且在此处发布的时间太长了。

但是你可以处理valueChanged( int )的{​​{1}}信号 并使用QProgressBar相应地设置样式表,但我想你已经知道了。