我有一个QWidget (窗口)和QHBoxLayout,其中包含两个QPushButtons。如果我把窗户变大(非常宽),就会发生两件事:
但我需要另一种行为:
如何达到上述行为?
UPD:
我建议采用以下守则:
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QWidget wgt;
QPushButton* button1 = new QPushButton("Button1");
QPushButton* button2 = new QPushButton("Button2");
button1->setMinimumSize(150, 100);
button1->setMaximumSize(250, 100);
button2->setMinimumSize(150, 100);
button2->setMaximumSize(250, 100);
QHBoxLayout* pLayout = new QHBoxLayout(&wgt);
pLayout->addWidget(button1);
pLayout->addWidget(button2);
wgt.setLayout(pLayout);
wgt.setGeometry(400, 400, 800, 300);
wgt.show();
return app.exec();
}我需要的布局应该限制从最小到最大(不能少于分钟,不能大于最大)+不拉伸空间(它必须有固定的大小)之间和周围的按钮。
发布于 2018-10-02 14:06:42
原因
当窗口被调整大小时,某些东西必须占用可用的空间。由于按钮本身的大小受到限制,它们之间的空间也随之增大。
解决方案
我建议您添加一个不可见的小部件作为占位符。然后相应地调整布局的间距。
示例
下面是我为您准备的如何更改代码以达到预期效果的示例:
QHBoxLayout* pLayout = new QHBoxLayout(&wgt);
pLayout->addWidget(button1);
pLayout->addSpacing(6);
pLayout->addWidget(button2);
pLayout->addWidget(new QWidget());
pLayout->setSpacing(0);替代解
为了限制小部件的大小,使用QWidget::setMinimumSize和QWidget::setMaximumSize
wgt.setMinimumSize(button1->minimumWidth()
+ button2->minimumWidth()
+ pLayout->contentsMargins().left()
+ pLayout->contentsMargins().right()
+ pLayout->spacing(),
button1->minimumHeight()
+ pLayout->contentsMargins().top()
+ pLayout->contentsMargins().bottom()
+ pLayout->spacing());
wgt.setMaximumSize(button1->maximumWidth()
+ button2->maximumWidth()
+ pLayout->contentsMargins().left()
+ pLayout->contentsMargins().right()
+ pLayout->spacing(),
button1->maximumHeight()
+ pLayout->contentsMargins().top()
+ pLayout->contentsMargins().bottom()
+ pLayout->spacing());如果事先知道确切的尺寸,可以将其简化为:
wgt.setMinimumWidth(324);
wgt.setMaximumWidth(524);
wgt.setFixedHeight(118);https://stackoverflow.com/questions/52597899
复制相似问题