Qt remove margins

懵懂的女人 提交于 2019-12-07 06:22:35

问题


I am currently using a QMainWindow widget and I would like to remove margins around the widget inside. I success to remove 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 :

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

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

Thank you.


回答1:


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

 this->mainLayout->setSpacing(0);


来源:https://stackoverflow.com/questions/20450309/qt-remove-margins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!