当值为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;
}
答案 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
相应地设置样式表,但我想你已经知道了。