2013-12-08 134 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

謝謝你,它的工作......這是如此簡單,我沒有看到... –