我有一个小部件(让我们称之为主小部件),它应该显示另外两个小部件。我使用QGridLayout将这两个小部件放置在主小部件中。 (这只是简化,我真的在主窗口中有5个小部件)如何重新缩放布局中的小部件?
最初,这两个小部件的大小相同,但可以重新缩放。例如,第二个小部件应显示为第一个小部件的2,3或4倍,但都应显示。
事情是这样的:
我试着用addWidget,与绵延的行和列第二小部件,但预计没有工作(Widget的尺寸保持不变)
那么,你如何建议我按要求实现? 如果QGridLayout不是正确的类,我应该使用什么?
编辑:
有些类型:
... 结构信息 { INT项; int row; int列; int rowSpan; int columnSpan; } typedef std :: vector < info> LayoutDefinition; ...
类WindowsContainer包含此:
vector< QWidget* > containers;
这里是更新的布局方法:
void WindowsContainer::SetNewLayout(const LayoutDefinition & newLayoutDef)
{
// hide all containers
for (std::vector< QWidget* >::iterator it = containers.begin(); containers.end() != it; ++ it)
{
(*it)->hide();
}
// remove all items in the layout, and release the layout
QLayout * currentLayout(layout());
if (0 != currentLayout)
{
// remove all items in the layout
// (this code is taken from the reference page for the QLayout::takeAt() function)
QLayoutItem *child;
while ((child = currentLayout->takeAt(0)) != 0)
{
delete child;
}
}
delete(currentLayout);
// form the new layout
QGridLayout *newLayout = new QGridLayout(this);
newLayout->setSpacing(0);
newLayout->setContentsMargins(0, 0, 0, 0);
for (LayoutDefinition::const_iterator it = newLayoutDef.begin(); newLayoutDef.end() != it; ++ it)
{
QWidget * w(containers.at(it->item));
newLayout->addWidget(w,
it->row,
it->column,
it->rowSpan,
it->columnSpan
);
w->show();
}
// update the layout of this object
setLayout(newLayout);
}
所有的工作,直到我尝试设置行跨度和columnSpan。两幅图像仍然占用相同的面积。
我也试过用垫片,并没有工作(不知道如果我做得对了)。
你将不得不描述你想如何改变发生的过程。但这应该是可能的。 –
@TomKerr它为什么重要?假设某处有一个按钮,它应该以这样的方式设置布局,即顶部图像是底部图像的1/4。还有另一个按钮,它设置1:1口粮。第三个按钮设置比率4:1 –
如果您想允许用户拖动分隔符,可以使用主窗口中的对接小部件作为示例。这将是一个非常不同的解决方案,可以调整由内部事件编程生成的网格的大小策略和拉伸因子。 –