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);
這裏是它呈現:
我想這兩個部件之間取出白色部分。
你有想法如何做出這樣的事情?
謝謝。
旁註:你正在使用'本 - > ...'爲您的所有成員訪問,除了之一(setContentMargin對於mainLayout來說) - 要麼全部都要做,要麼全都做,但如果沒有明確的限定條件,只留下那個,看起來很奇怪 - 這讓你想知道我們是不是在談論兩件不同的事情。 – Mat
另請參閱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