我正在尝试使用动态属性更改 QLabel 的样式,因为我们可以像这样在 QSS 中定位此属性:
QLabel[foo = "warning"]{ color: red; }
QLabel[foo = "success"]{ color: green; }
当我使用QApplication::setStyleSheet()
时,我的标签的麦粒肿确实会更新,但当我更改属性的值时,它似乎不起作用。
label = new QLabel( this );
label->setText( "some text" );
label->setProperty( "foo", "warning");
// after some event
label->setProperty( "foo", "success" );
// the label's color should be green now
我是否错过了什么,或者样式更改不能以这种方式工作?
最佳答案
无论我们在哪里更改属性,都需要添加以下代码来触发更新
label->setProperty("foo", "success");
label->style()->unpolish(label);
label->style()->polish(label);
label->update();
关于qt - 动态改变样式表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22562716/