Qt:如何在两个不同的 QGridLayouts 中对齐小部件? [英] Qt: How can I align widgets in two different QGridLayouts?

查看:79
本文介绍了Qt:如何在两个不同的 QGridLayouts 中对齐小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试排列来自两个不同 QGridLayouts 的小部件.想法如下:我在滚动框架的 QGridLayout 中有许多小部件行.每行可能包含 8 列其他小部件(因此,8xN 小部件的网格,N = # 行).该行中的第一个窗口小部件是一个显示/隐藏按钮,当检查时,将显示另一行的另一个布局的另一个小部件(即,所示的窗口小部件具有小部件的行,每行具有7列的小部件).因此,显示的小部件将在最初插入并在显示按钮切换时显示/隐藏.

I am trying to line up widgets from two different QGridLayouts. The idea is the following: I have many rows of widgets in the QGridLayout of a scroll frame. Each row contains maybe 8 columns of other widgets (so a grid of 8xN widgets, N = # of rows). The first widget in that row is a show/hide button that when checked, will show another widget with the same layout just below that row (i.e. that shown widget has rows of widgets, each row has 7 columns of widgets). So the shown widget will be inserted initially and be shown/hidden when the show button is toggled.

下图说明了我的设置.标记为A"的红色括号指示的小部件是我选中左侧复选框时显示的小部件(它们没有自己的显示按钮).

The image below illustrates my setup. The widgets indicated by the red bracket labelled "A" are the ones I show when checking the check box on the left (they do not have their own show button).

我需要解决的问题是如何让显示的小部件与所有其他小部件对齐.我在这里想到了两个选项:

The problem I need to solve is how to get the shown widgets to line up with all of the others. I thought of two options here:

  • 将所有小部件放在相同的布局中,然后循环浏览我需要显示/隐藏的小部件.这样做的好处是所有小部件都排列在一起,因为它们位于相同的布局中.不好的是,如果我有数百行小部件(这很常见),则需要很长时间才能将它们全部显示出来.
  • 将子小部件放在它们自己的容器小部件中,并让显示/隐藏按钮简单地显示/隐藏那个小部件.好处是这比显示/隐藏每个单独的小部件快得多,但不好的一面是小部件现在没有排列,因为子小部件处于不同的布局.我认为这是更好的选择.我只需要弄清楚如何对齐所有类似的小部件.

希望这很清楚.我正在使用 Qt 5.11.提前感谢您的帮助.

Hopefully that was clear. I am using Qt 5.11. Thanks ahead of time for the assistance.

推荐答案

好吧,如果您有太多的小部件,以至于在现代计算机上加载速度很慢,您可能想要完全采用不同的策略来加载实际的小部件懒洋洋."即使是隐藏的,它们也需要时间来生成、存储等等.OTOH 之类的模型/视图框架会在请求编辑之前显示相对轻量级的仅显示标签,然后才绘制(相对)复杂的编辑器小部件.并且视图提供了按需加载额外数据(延迟加载)的规定,这正是您所需要的.

Well, if you have so many widgets that it is slow loading on a modern computer, you may want to go a different strategy altogether where you load the actual widgets "lazily." Even hidden, they take time to generate, memory to store, and so on. OTOH something like the model/view framework shows relatively light-weight display-only labels until an edit is requested, and only then draws the (relatively) complicated editor widget. And the views have provisions for loading additional data on demand (lazy loading), which is what you're looking for.

但无论如何要回答您的问题,您基本上需要跟踪参考"网格(可能是您的第一个)的宽度,并手动在任何其他网格上设置网格列.这并不难,因为您可以轻松地遍历列并调用 QGridLayout::setColumnMinimumWidth().替代参考网格"的另一种选择是自己计算列宽(当然,如果您想适应内容宽度,这会更复杂).

But anyway to answer your question, you'll basically need to track the widths of a "reference" grid (maybe your first one), and set the grid columns on any additional grids manually. Which isn't that hard since you can easily loop over the columns and call QGridLayout::setColumnMinimumWidth(). Another option instead of a "reference grid" would be to calculate the column widths yourself (though of course that's more involved if you want to adapt to content width).

QGridLayout gl1;
gl1.setSizeConstraint(QLayout::SetMinimumSize);  // maybe

gl1.addWidget(..., 0, 0);
gl1.addWidget(..., 0, 1);
gl1.addWidget(..., 0, 2);
gl1.activate();  // or show it or something... it needs to calculate its sizeHint().

QVector<int> colWidths;
for (int i=0; i < gl1.columnCount(); ++i)
  colWidths << gl1.columnMinimumWidth(i);

QGridLayout gl2;
gl2.addWidget(..., 0, 0);
gl2.addWidget(..., 0, 1);
gl2.addWidget(..., 0, 2);

for (int i : colWidths)
  gl2.setColumnMinimumWidth(i);

这可能很简单,但本质上您需要一种蛮力"方法来使用单独的布局.如果布局中的小部件具有固定宽度,这将有很大帮助,如果您事先知道它们,那么它会变得更加容易.对布局类型进行子类化、推出自己的布局类型或从单独的 H/V 框布局中组装网格也可能会有所帮助.

That's simplistic perhaps but essentially you'll need a "brute force" approach to use separate layouts. It will help a lot if the widgets in the layouts have fixed widths, and if you know them in advance then it becomes even easier. It may also be helpful to subclass a layout type, roll your own, or assemble a grid out of separate H/V box layouts.

但实际上,只要您说数百个小部件",我就会想不".BTDT.我会看看模型/视图框架,如果情况真的那么绝望,也许会考虑实现我自己的 QAbstractItemView.:)

But really, as soon as you say "hundreds of widgets" I just think "no." BTDT. I'd take a look at the model/view framework and perhaps consider implementing my own QAbstractItemView if the situation is really that desperate. :)

这篇关于Qt:如何在两个不同的 QGridLayouts 中对齐小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆