8

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!

Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335

2 Answers2

8

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)

Kamil Klimek
  • 12,884
  • 2
  • 43
  • 58
  • I believe it should be QWidget, without the dot. – Violet Giraffe Oct 25 '11 at 10:57
  • 6
    QWidget without the dot will indicate ale widgets that inherit QWidget. with dot it includes only instances of class QWidget itself. http://doc.qt.nokia.com/4.7/stylesheet-syntax.html#selector-types – Kamil Klimek Oct 25 '11 at 11:07
6

Style sheets will work recursively. If you apply style sheet to a Application it will be applied to all widgets within it. So you may have to specify to what you want to apply style sheet?

logic should be something like this..

QHBoxLayout#layoutbox {
     background-color: red;
     border-style: outset;
     border-width: 2px;
     border-radius: 10px;
     border-color: beige;
     font: bold 14px;
     min-width: 10em;
     padding: 6px;
 }
RLT
  • 4,219
  • 4
  • 37
  • 91
  • 2
    how can I set the stylesheet? In Qt4.8 the QHBoxLayout class has no function called `setStyleSheet()`. Should I do it in a different way, or does your answer only apply for Qt5 ? – Wim Aug 16 '16 at 14:30