Search code examples
qtqwidgetqtstylesheets

Updating QWidget style after setting it readonly


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);
}

Solution

  • 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 multiple QLineEdits)

    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.