【问题标题】:How to rescale widgets in a layout?如何在布局中调整小部件的大小?
【发布时间】: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
  • 如果您想允许用户拖动分隔符,您可以使用主窗口中的停靠小部件。这将是一个非常不同的解决方案来调整从某些内部事件以编程方式生成的网格的大小策略和拉伸因子。

标签: c++ layout qt4 widget


【解决方案1】:

是的,这可以通过布局、间隔和尺寸政策来实现。这是来自 QtCreator 的屏幕截图,它重现了您正在寻找的效果:

“TopWidget”将根据其包含的小部件的要求占用尽可能少的空间,并且“BottomWidget”将在调整应用程序窗口大小时扩展以占据所有剩余空间。

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

【讨论】:

    【解决方案2】:

    我想出了怎么做:设置拉伸因子就足够了。

    在上面的例子中:

    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 );
    

    这将导致第二行比第一行大两倍。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 编写自己的布局。 - 你的意思是继承自 QLayout 的新布局类吗?
      • 没错。或者你可以从 QGridLayout 继承并只重新定义那些足以达到预期效果的方法。
      • 我认为这根本不准确。
      • 这不是真的。虽然布局的一部分工作确实是管理它包含的小部件的大小,但这些小部件的位置可以通过使用组合布局、间隔和小部件的大小策略等来影响。@V Jovic:如果可能的话,从长远来看,熟悉 Qt Designer 来设计您的界面将为您节省大量时间。它使 UI 的设计和维护变得更加容易和快捷。
      猜你喜欢
      • 1970-01-01
      • 2020-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-30
      • 2012-02-20
      • 2023-03-07
      相关资源
      最近更新 更多