Qt: How can I align widgets in two different QGridLayouts?

偶尔善良 提交于 2021-01-29 09:30:21

问题


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.

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:

  • Put all widgets in the same layout and just loop through the widgets that I need to show/hide to do so. The benefit to this is all the widgets are lined up since they are in the same layout. The bad thing is if I have hundreds of rows of widgets (which is common), it takes a very VERY long time to show them all.
  • Put the subwidgets in their own container widget and have the show/hide button simply show/hide THAT widget. The benefit is that this is much MUCH faster than showing/hiding each individual widget, but the bad side is the widgets are now not lined up because the subwidgets are in a different layout. I'm thinking this is the better path to take. I just need to figure out how to align all the like widgets.

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


回答1:


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.

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

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.

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. :)



来源:https://stackoverflow.com/questions/58139967/qt-how-can-i-align-widgets-in-two-different-qgridlayouts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!