I have a label, and I set both padding in the style sheet and a margin using setMargin()
.
ui->label->setPixmap(redRectWithGreenBorder(80, 40));
ui->label->setStyleSheet("QLabel {border: 1px solid gray;border-radius: 2px;background-color: white;padding: 0px 5px 10px 15px;}");
ui->label->setMargin(5);
But this is how it looks in reality:
The visible margins are 5, 10, 15 and 20 (I checked with an image editor). That is, they are equal to the value of the style sheet for this side plus the value of margin()
, i.e. 0+5, 5+5, 10+5, 20+5.
However, the documentation says this about setMargin()
:
margin : int This property holds the width of the margin.
The margin is the distance between the innermost pixel of the frame and the outermost pixel of contents.
The default margin is 0.
If I were taking the documentation at face value, I would have expected the margin to be the real distance between the content and the border. But it is not. Instead, the sum of the margin and the style sheet padding is the real margin.
My question is, where in the documentation is this behavior described? Is there some other place in the documentation where this behavior is specified that I have missed?
Stylesheet padding applies to all controls, even those that are not based on QLabel
. It exists in addition to any control-specific mechanisms. The margin
is a QLabel
-specific property that predates stylesheets; it was available in Qt 4.0 (at least). The stylesheets came later, in Qt 4.2.
Another way to think about it: The behavior can be inferred from the fact that there's only one margin
property, not four. This property has nothing to do with stylesheet padding, since the latter give you 4 separate values of padding. There's no way to reconcile those other than adding them up.
Also, note that the margin
and padding
are different terms. The stylesheet specifies the padding. The QLabel
specifies a margin. They may be synonyms, but that's irrelevant here. They are still different concepts within the setting of QLabel
. The visual outcome is due to both. The 4.0 margin's behavior had to be preserved in Qt 4.2, so as not to break existing code.