PDA

View Full Version : QFormLayout with QTableView that expands vertically when window resizes



LynneV
17th December 2014, 18:28
Hello! I have a window formatted using a QFormLayout that contains a set of field/label pairs, a QTableView, and then a set of buttons in a QHBoxLayout. When the window resizes, I would like the QTableView to expand in both directions - vertically to show more rows, as well as horizontally to show hidden columns.

I have the QTableView set to horizontalHeader()->setStretchLastSection( True ); and setSizePolicy( QSizePolicy::Expanding,QSizePolicy::Expanding ); I get the horizontal resize behavior I want, but not the vertical resize behavior.

If I use a QHBoxLayout, a QVBoxLayout or no layout, the resizing in both directions works nicely. If I have just the QTableView in a QFormLayout, it doesn't expand vertically. Do I have to switch the window's top level layout manager to a QGridLayout and then manage the row height manually, or is there something I'm missing in QFormLayout that will do the trick? Or some other approach that's better when you have a window with a lot of different widget types?

Thanks in advance for any help!

anda_skoa
17th December 2014, 22:11
Hmm.

Have you tried setting a different field growth policy?

Or putting a the tableview together with a vertical spacer into the form layout cell?

Cheers,
_

LynneV
17th December 2014, 22:58
Thanks for the response! Yes, I have fieldGrowthPolicy set to AllNonFixedFieldsGrow and I've tried ExpandingFieldsGrow. But I don't see that either affects the QTableView. I kind of assumed based on the names that those properties were specific to "fields", like QLineEdit. I will try a vertical spacer... though if it works, I'd like to understand why this layout would need one, where the other layout types don't!

d_stranz
18th December 2014, 02:11
How about making the top level layout a QVBoxLayout? Remove the table view from the form, then stack the form layout on top of the table view in the vbox.

Or if that isn't appropriate, some other arrangement that removes the table view from the form layout and puts both of them under the control of some other layout manager?

LynneV
18th December 2014, 02:47
That's my basic question. If QFormLayout just doesn't support (or support well, at any rate) a mix of widgets, then yes, I have to make some other layout manager the primary parent, and then put widgets in matching layout managers - ie. QFormLayout for items with labels and other kinds for QTableView or RadioButtons or whatever. It sounds as if that's the course I'll have to take. Thanks for your help!

anda_skoa
18th December 2014, 08:32
then put widgets in matching layout managers - ie. QFormLayout for items with labels and other kinds for QTableView or RadioButtons or whatever.

This seems to be a bit contradictory.
If the tableviiew is an item without an associated label, why would you put it into a form layout?

Cheers,
_

LynneV
18th December 2014, 14:31
I'm writing a translation script for an existing application and I don't have an indicator when creating the layouts of what kind of widgets will be populating the window. It was easier to choose QFormLayout and not care what the widgets were. But I was assuming QFormLayout had the same behaviors as the more generic layout managers. I just wanted to know if there was some setting I was missing...and you were kind enough to let me know. So, off to rework it!

d_stranz
18th December 2014, 15:04
I'd suggest trying a grid layout instead of a form layout, and making sure that things you want to grow occupy cells at one or more edges of the grid. Be sure that the grid is the primary layout for the widget so it will resize as the widget does. It may be necessary to put a phantom row or column in the grid and stretch the table to span the extra cells. These extra cells will resize without affecting the sizes of other cells in the grid (which might contain labels / buttons, etc. that you don't want to grow along with the table). Grid layouts are very adaptable, but you sometimes must trick them into a pleasing layout.