问题描述
我想创建一个Qt窗口,其中包含两种布局,一种固定高度,其顶部包含一列按钮,而另一种高度则用下面的图像将小部件垂直和水平居中的布局填充剩余的空间.
I want to create a Qt window that contains two layouts, one fixed height that contains a list of buttons at the top and one that fills the remaning space with a layout that centers a widget vertically and horizontally as per the image below.
我应该如何布局我的布局/小部件.香港专业教育学院尝试了一些嵌套的水平和垂直布局的选项,但无济于事
How should i be laying out my layouts/widgets. Ive tried a few options with nested horizontal and vertical layouts to no avail
推荐答案
尝试使粉红色框成为带有QHBoxLayout的QWidget(而不只是使其成为布局).原因是QLayouts不提供固定大小的功能,而QWidgets提供了固定大小的功能.
Try making the pink box a QWidget with a QHBoxLayout (rather than just making it a layout). The reason is that QLayouts don't provide functionality to make fixed sizes, but QWidgets do.
// first create the four widgets at the top left,
// and use QWidget::setFixedWidth() on each of them.
// then set up the top widget (composed of the four smaller widgets):
QWidget *topWidget = new QWidget;
QHBoxLayout *topWidgetLayout = new QHBoxLayout(topWidget);
topWidgetLayout->addWidget(widget1);
topWidgetLayout->addWidget(widget2);
topWidgetLayout->addWidget(widget3);
topWidgetLayout->addWidget(widget4);
topWidgetLayout->addStretch(1); // add the stretch
topWidget->setFixedHeight(50);
// now put the bottom (centered) widget into its own QHBoxLayout
QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->addStretch(1);
hLayout->addWidget(bottomWidget);
hLayout->addStretch(1);
bottomWidget->setFixedSize(QSize(50, 50));
// now use a QVBoxLayout to lay everything out
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(topWidget);
mainLayout->addStretch(1);
mainLayout->addLayout(hLayout);
mainLayout->addStretch(1);
如果您真的想拥有两种单独的布局-一种用于粉红色框,一种用于蓝色框-想法基本相同,只是您将蓝色框放入其自己的QVBoxLayout中,然后使用:
If you really want to have two separate layouts--one for the pink box and one for the blue box--the idea is basically the same except you'd make the blue box into its own QVBoxLayout, and then use:
mainLayout->addWidget(topWidget);
mainLayout->addLayout(bottomLayout);
这篇关于创建具有固定高度的Qt布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!