问题
I have Qt style sheet (qss
) for QLineEdit
, using different styles for readonly and editable. Works fine, but if I toggle a QLineEdit
to readonly (at runtime) the style does not change.
Is there a way to force a stylesheet update of such a line edit?
As requested, the stylesheet:
QLineEdit {
background: transparent;
border: 1px solid green;
border-radius: 5px;
}
QLineEdit[readOnly="true"] {
background: rgba(40,40,40);
border: 1px solid rgba(50,50,50);
}
回答1:
After change edit's state try next code:
qApp->style()->unpolish(this);
qApp->style()->polish(this);
Where "this" current QMainWindow or QDialog.
回答2:
Here my own findings:
- The
polish
/unpolish
thing works - However, it is somehow inconvenient as I have to apply it for each
QLineEdit
object, it does not work for me if I do it on the parent level (e.g. on a dialog with multipleQLineEdit
s)
What works for me is to force an update like this
widget->setStyleSheet(widget->styleSheet());
, by just setting the same stylesheet. I works also on the top level widget, updating multiple child elements.
来源:https://stackoverflow.com/questions/48141205/updating-qwidget-style-after-setting-it-readonly