【发布时间】:2011-09-29 13:39:11
【问题描述】:
我有一个小部件(我们称之为主小部件),它应该显示另外两个小部件。我正在使用QGridLayout 将这两个小部件定位在主小部件中。 (这只是简化。我真的在主窗口中有 5 个小部件)
最初,这两个小部件的大小相同,但它们可以重新缩放。例如,第二个小部件应显示为第一个小部件的 2、3 或 4 倍,但两者都应显示。
类似这样的:
我尝试使用addWidget,将第二个小部件拉伸到行和列上,但这并没有按预期工作(小部件的大小保持不变)
那么,您建议我如何按照要求实现? 如果 QGridLayout 不是正确的类,我应该使用什么?
编辑:
一些类型:
...
结构信息
{
整数项目;
整数行;
整数列;
整数行跨度;
整数列跨度;
}
typedef std::vector 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 );
}
一切正常,直到我尝试设置 rowSpan 和 columnSpan。两个图像仍然占据相同的区域大小。
我也尝试过使用垫片,但没有奏效(不确定我是否做得正确)。
【问题讨论】:
-
你必须描述我认为你希望改变发生的过程。不过,这应该是可能的。
-
@TomKerr 为什么重要?假设某处有一个按钮,它应该将布局设置为顶部图像是底部图像的 1/4。还有另一个按钮,可设置 1:1 配给。第三个按钮,设置比例为 4:1
-
如果您想允许用户拖动分隔符,您可以使用主窗口中的停靠小部件。这将是一个非常不同的解决方案来调整从某些内部事件以编程方式生成的网格的大小策略和拉伸因子。