Qt 4: How to set outer border for QWidget so that its inner widgets are unaffected?

Violet Giraffe picture Violet Giraffe · Oct 25, 2011 · Viewed 20.9k times · Source

I have quite a strange problem. I have a QWidget with QHBoxLayout on it. The layout contains two QLabels. I want to set a border for this whole widget. I'm using style sheet:

 "padding: 10px;"
 "border-style: solid;"
 "border-width: 3px;"
 "border-radius: 7px;"

But here's the problem: this style is applied to both QLabels and completely breaks the layout. I only need the outer window to have the border, not the labels. Any ideas?

Thanks in advance!

Answer

Kamil Klimek picture Kamil Klimek · Oct 25, 2011

Use

.QWidget
{
    // your css rules
}

.QWidget will apply CSS only to classes that are EXACTLY QWidget and not inheriting QWidget

You can also use object name selector

#YourWidgetObjectName
{
    // your css rules
}

Both solutions wont apply rules to other widgets (even those inside)