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