Updating QWidget style after setting it readonly

此生再无相见时 提交于 2020-01-30 05:24:48

问题


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 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.



来源:https://stackoverflow.com/questions/48141205/updating-qwidget-style-after-setting-it-readonly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!