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

waterd picture waterd · May 6, 2014 · Viewed 12.1k times · Source

I Konw I can use setStyleSheet() to set style in Qt.But I encountered a problem,when I used setStyleSheet() twice first styles lost,which are set by first use of setStyleSheet().

For exmaple,

setStyleSheet("QLabel{color:red;}");

…………

setStyleSheet("QLabel{border-image:url(……)}")

When I set border-image,the red color property lost.

I tried to solve it by using

setStyleSheet(styleSheet()+QString("QLabel{border-image:url(……)}"));

but it was the same that only the border-image property existed.

Must I add every style property when I use setStyleSheet(),although that I set it before.

Thanks for bearing my poor written English.Any tips will be appreciated.

Answer

Nejat picture Nejat · May 6, 2014

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(……);")) );