Qt remove margins
Asked Answered
V

1

8

I am currently using a QMainWindow widget and would like to remove margins around the widget inside. I succeeded at removing margins for window borders, but not for the widgets inside my window.

Here is my code, for example :

this->mainWidget = new QWidget(this);
this->mainLayout = new QHBoxLayout;
QLabel *foo = new QLabel("foo", this);
QLabel *bar = new QLabel("bar", this);

mainLayout->setContentsMargins(0, 0, 0, 0); // Remove margins for window borders

this->setWindowFlags(Qt::FramelessWindowHint);

foo->setStyleSheet("background-color: green");
bar->setStyleSheet("background-color: red");
foo->setContentsMargins(0, 0, 0, 0); // Has no effect
bar->setContentsMargins(0, 0, 0, 0); // Has no effect

this->mainLayout->addWidget(foo);
this->mainLayout->addWidget(bar);

this->mainWidget->setLayout(mainLayout);
this->setCentralWidget(mainWidget);

And here is what it rendered :

Window render

I would like to remove the white part between the two widgets.

Have you an idea how to make that kind of thing?

Viborg answered 8/12, 2013 at 5:41 Comment(2)
Side note: you're using this->... for all your member accesses except one (the setContentMargin for the mainLayout) - either do it for all, or for none, but leaving just that one without the explicit qualification looks weird - makes you wonder whether we're talking about two different things or not.Orazio
see also #9129824 and #12018289Abscess
O
10

You simply need to set the spacing attribute of your box layout to zero:

this->mainLayout->setSpacing(0);
Orazio answered 8/12, 2013 at 5:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.