我正在尝试使用动态属性来更改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();
http://qt-project.org/wiki/DynamicPropertiesAndStylesheets
关于qt - 动态更改样式表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22562716/