2013-12-08 138 views
4

我目前正在使用QMainWindow小部件,我想删除小部件内部的边距。我成功删除窗口边界的边距,但不是窗口内部的窗口部件。Qt删除边距

这里是我的代码,例如:

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); 

这里是它呈现:

Window render

我想这两个部件之间取出白色部分。

你有想法如何做出这样的事情?

谢谢。

+0

旁注:你正在使用'本 - > ...'为您的所有成员访问,除了之一(setContentMargin对于mainLayout来说) - 要么全部都要做,要么全都做,但如果没有明确的限定条件,只留下那个,看起来很奇怪 - 这让你想知道我们是不是在谈论两件不同的事情。 – Mat

+0

另请参阅http://stackoverflow.com/questions/9129324/qt-widget-with-layout-space-what-is-it-how-to-remove和http://stackoverflow.com/questions/12017789/removing -extra-spacing-around-qwidget – Trilarion

回答

5

你只需要你的盒子布局spacing属性设置为零:

this->mainLayout->setSpacing(0); 
+0

谢谢你,它的工作......这是如此简单,我没有看到... –