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:
Hopefully that was clear. I am using Qt 5.11. Thanks ahead of time for the assistance.
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. :)