Important: Please read the Qt Code of Conduct -

Qt Stylesheet: Margins of childwidgets

  • Hi!

    I'm using qss to style my ui. I have a QMainWindow with a centralWidget and the centralwidget has a layout that contains widgets. If i set margins in qss for centralwidget, the widgets in the layout of the centralwidget do not recognize the margins. Whats wrong?

    I see that the middle has the margins, but not the headerpanel i created. Some code:
    @void MainCentralWidget::setupUi()
    mainLayout = new QVBoxLayout(this);
    headerPanel = new BackgroundPanel(this);
    mainLayout->addWidget(headerPanel, 1, Qt::AlignTop);

    And this is the css:

    @MainCentralWidget {
    background-image: url(styles/default/images/bgmain.jpg);
    background-repeat: no-repeat;
    background-position: bottom right;
    margin: 10px;

    Now, the header panel is at full size, no margins.

    And here's a screen:


    Any ideas? Or is it true that i have to set the margin for each widget

  • no ideas? Do i really have to set the margins for each widget? Also if it is a child of a widget with margins?

  • Lifetime Qt Champion


    If I am not mistaken your style sheet is only for your MainCentralWidget so It won't apply to other widgets.

    You have to make a more generic style sheet for QWidget for example and prepend it to your current style sheet.

    Hope it helps

  • So...child widgets are not part of the style? I expected that if i set the margin of a widget, the child widget in the parent layout also has the margins.

    It works if i set the the margins of the child-widget. I dont want to set the margin of QWidget because every Qwidget then has the margin...that's not what i want.

  • BTW: Thank you for your answer :-)

  • Lifetime Qt Champion

    You're welcome :)

    Basically no, each widget is independent for that.

    You could however use this "technique": for your child widgets

Log in to reply