How to add style via setStyleSheet() without losing orignal style in Qt?

試著忘記壹切 提交于 2019-12-01 17:45:46

You can set stylesheets without QLabel tag:

setStyleSheet("color:red;");

After setting one stylesheet property, you can add another property like:

setStyleSheet( styleSheet().append(QString("border-image:url(……);")) );

This is in response to your comment on the accepted answer.

You can prevent overwriting stylesheets properties by setting the constant values to the parent (permitting that the parent's style isn't being changed dynamically as well). Only set the values that you change with C++ to the child item.

parentWidget->setStyleSheet( "QLabel#yourLabel { color:red; }" );
yourLabel->setStyleSheet( "QLabel { border-image:url(...) };" );

This will retain all of the parents properties that have been set on the widget when you change the widget's stylesheet.

Furthermore, this removes the case of a very large string, which is possible in the accepted answer. Frequent changes will inefficiently append the string with previously defined styles that will not be used.

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