Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 885
Violet Giraffe Avatar asked Oct 25 '11 06:10

Violet Giraffe


2 Answers

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)

like image 169
Kamil Klimek Avatar answered Sep 29 '22 03:09

Kamil Klimek


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;
 }
like image 31
RLT Avatar answered Sep 29 '22 02:09

RLT