Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set or change specific style sheet properties in Qt for a QWidget, without resetting the whole style sheet?

Tags:

qt

I've been searching for a while for an answer to this problem and I'm rather surprised that I have not found a solution. I'm working with Qt on Mac and would like to customize QPushbuttons and other QWidgets, but I want to maintain the native look somewhat as well. For example, if I want to remove the margins of a QButton using style sheets, I do:

QPushButton btn(this);
...
btn.setStyleSheet("margin:0;");

This indeed removes the margins, but it also removes the native style already set for default buttons. Of course, I just want to modify margins using style sheets, how do I do this?

Also, I would expect that btn.styleSheet() would return the native style sheet, but it is blank by default. Only when I set my own style sheet, does it return a valid value, but only for the property I set. I get that setStyleSheet would reset the style sheet, but how do I modify certain properties and leave everything else as is?

like image 265
Jacob Bisher Avatar asked Jun 30 '15 19:06

Jacob Bisher


1 Answers

TL;DR: It can't be done that way.

The native style cannot be generally expressed as a CSS style sheet, thus styleSheet() is empty by default on all styles. Thus, unfortunately, it's not possible to change native style elements one-by-one, since typically they are drawn by the platform APIs that allow little if any customization.

For examples, a QPushButton is drawn by the native calls on both OS X and Windows.

In the specific case of the margin, though, you can easily work around by creating a proxy style that returns smaller control rectangle and crops and transforms the painter before passing it to the base style. This also works for colorization/color substitution etc. You basically have to accept that the base style has to do the drawing, and then it's up to you to tweak it.

like image 175
Kuba hasn't forgotten Monica Avatar answered Nov 15 '22 05:11

Kuba hasn't forgotten Monica