[Development] New class for QtWidgets: ColumnResizer

André Somers andre at familiesomers.nl
Fri Jun 13 10:32:02 CEST 2014

Ziller Eike schreef op 13-6-2014 09:51:
> On Jun 12, 2014, at 6:51 PM, Oswald Buddenhagen <oswald.buddenhagen at digia.com> wrote:
>> On Thu, Jun 12, 2014 at 04:08:17PM +0200, Aurélien Gâteau wrote:
>>> Olivier Goffart wrote:
>>>> But just wondering if it would not be better to have that as an API within
>>>> QGridLayout such as
>>>>   QGridLayout::setAlignedWith(QGridLayout*)
>>> The class works with QFormLayout as well, so I don't think moving the
>>> feature into QGridLayout would be a good idea.
>> they have a common base class, you know ...
>>   QLayout::linkDimension(QLayout *other, Qt::Orientation orientation = Qt::Horizontal)
>> (the second parameter is actually a flag field).
>> and for more fine-grained control one could consider (not sure this makes
>> sense):
>>   QLayoutItem::linkDimension(QLayoutItem *other, Qt::Orientation orientation = Qt::Horizontal)
>> but then, i wonder whether you are fixing the right problem to start
>> with. usually, one would create a big master layout, and embed
>> sublayouts spanning multiple cells in the areas that are not supposed to
>> be contrained by the grid.
> In principle I like the idea of being able to work with a “master” grid. That’s something I wished e.g. for Qt Creator’s preferences dialog, where we use group boxes to group options, and these contain either something like what form layout does, or lists of checkboxes etc, or a mix.
> Currently it is basically impossible to align the content in the vertically layouted group boxes.
Indeed, and that's exactly what ColumnResizer addresses. Putting 
everything in one big layout works only for small, simple widgets. As 
soon as you start using widgets that contain other widgets (like group 
boxes, but also tab pages or just compound widgets) you're out of luck 
with what we currently have in Qt. I have used ColumnResizer 
successfully to work around these limitations in the past.

Ideally (to me), it would be possible to indeed have a single master 
grid for a window (or a subsection thereof), and be able to somehow 
'attach' other layouts inside containers widgets to this master grid, so 
the local layout would mirror (a part of) the master grid, and the 
master grid would be sized such that it takes into account these 
attached child grids.

So, more something like this:

//modeled after addWidget
QGridLayout::linkToParentLayout(QGridLayout *masterGrid, int fromRow, 
int fromColumn, int rowSpan, intColumnSpan);

Other layout types would get similar methods:
//a hbox is a single row by definition
QHBoxLayout::linkToParentLayout(QGridLayout *masterGrid, int row, int 
fromColumn, int columnSpan);
//a vbox is a single column by definition
QVBoxLayout::linkToParentLayout(QGridLayout *masterGrid, int fromRow, 
int column, int rowSpan);
//a form layout is just a two-column layout
QFormLayout::linkToParentLayout(QGridLayout *masterGrid, int fromRow, 
int fromColumn, int rowSpan); //columnSpan == 2 by definition

A downside to such an approach is of course that it takes more effort to 
think about the layout of the form, as you need to take all its 
containers into account as well. The margins of the container itself has 
will need additional rows and columns in such a master grid then. Also, 
adding items in one container may then influence the layout in another 
container. That could get quite complicated to work with. ColumnResizer 
is simpler in that sense.


More information about the Development mailing list