【问题标题】:Qt Auto-arrange widgets in layoutQt 在布局中自动排列小部件
【发布时间】:2014-08-12 22:05:50
【问题描述】:

我是 Qt 新手,遇到了一个我无法解决的问题。

我拥有的是一个滚动区域,我可以在其中添加小部件(小部件是什么并不重要)。每个小部件都有一个静态大小,并且它们都具有相同的宽度(这可能很重要)。我要做的是进行布局/设置,以便所有这些小部件水平显示在滚动区域上,直到没有足够的空间容纳另一个小部件,此时它开始将小部件放在新行上,继续直到一个都没有。

我已经考虑过手动实现它的方法,但我觉得这是 Qt 已经支持的东西,我只是无法找到关于它的文档。

【问题讨论】:

    标签: c++ qt layout widget flowlayout


    【解决方案1】:

    您可以看到Flow Layout Example。它演示了从左到右和从上到下排列子小部件的自定义布局。当布局中的每一行空间不足时,项目首先水平布局,然后垂直布局。

    FlowLayout 类继承 QLayout。它是一个自定义布局类,可以水平和垂直排列其子小部件。您可以如链接中所示实现它并创建一个自定义小部件来保存流布局并将其设置为QScrollArea 的小部件。

    scrollArea->setWidgetResizable(true); // Important or else the widget won't expand to the size of the QScrollArea, resulting in the FlowLayout showing up as a vertical list of items rather than a flow layout
    scrollArea->setWidget(new CustomWidget);
    

    CustomWidget的构造函数中:

    // Create FlowLayout
    FlowLayout *flowLayout = new FlowLayout;
    
    // Populate FlowLayout with your widgets
    for (int i=0; i<n; i++) 
    {
        ...
        flowLayout->addWidget(widget);
    }
    
    setLayout(flowLayout);
    

    【讨论】:

      猜你喜欢
      • 2017-06-09
      • 2020-05-05
      • 2013-02-12
      • 1970-01-01
      • 2014-12-17
      • 1970-01-01
      • 1970-01-01
      • 2017-10-13
      • 1970-01-01
      相关资源
      最近更新 更多