2011-09-29 57 views
1

我有一个小部件(让我们称之为主小部件),它应该显示另外两个小部件。我使用QGridLayout将这两个小部件放置在主小部件中。 (这只是简化,我真的在主窗口中有5个小部件)如何重新缩放布局中的小部件?

最初,这两个小部件的大小相同,但可以重新缩放。例如,第二个小部件应显示为第一个小部件的2,3或4倍,但都应显示。

事情是这样的:
enter image description here

我试着用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。两幅图像仍然占用相同的面积。

我也试过用垫片,并没有工作(不知道如果我做得对了)。

+0

你将不得不描述你想如何改变发生的过程。但这应该是可能的。 –

+0

@TomKerr它为什么重要?假设某处有一个按钮,它应该以这样的方式设置布局,即顶部图像是底部图像的1/4。还有另一个按钮,它设置1:1口粮。第三个按钮设置比率4:1 –

+0

如果您想允许用户拖动分隔符,可以使用主窗口中的对接小部件作为示例。这将是一个非常不同的解决方案,可以调整由内部事件编程生成的网格的大小策略和拉伸因子。 –

回答

-4

这是不可能的。布局是应该自动管理小部件的布局和大小的东西。编写你自己的布局。

+0

*编写你自己的布局。* - 你的意思是一个从QLayout继承的新布局类吗?确切地说,是 –

+0

。或者你可以从QGridLayout继承并重新定义那些足以达到预期效果的方法。 – GreenScape

+0

我不认为这是准确的。 –

1

是的,这可以通过布局,间隔符和尺寸策略来完成。下面是从QtCreator屏幕截图再现您正在寻找的效果:

enter image description here

的“TopWidget”将占有为必要的,因为决定由它所包含的小部件和“BottomWidget”较小的空间将扩大在应用程序窗口调整大小时占用所有剩余空间。

“BottomWidget”的水平和垂直尺寸策略均设置为“展开”。

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); 
      w->show(); 
    } 
    newLayout->setRowStretch(0, 1); 
    newLayout->setRowStretch(1, 2); 

这将导致第二行是大两倍,则第一行。