I have a simple QGridLayout of different QGroupBox widgets. These QGroupBox Widgets could have variable wides (depending on how much content goes in each one.
When stacking the groupboxes into a grid, the next column starts at the right of the longest groupbox, but if there are shorter groupboxes in the same column, there will be empty space.
Is there an approach to basically force every single widget in a column to be the width of the maximum width widget?
Basically I want a column to go from this:
<===== Widget 1 Width =======>
<== Widget 2 Width ==> (empty)
<=Widget 1 Width=> (empty )
To this:
<===== Widget 1 Width =======>
<===== Widget 2 Width =======>
<===== Widget 3 Width =======>
Instead of leaving empty space before the next column, force the groupbox (or whatever widget is in the grid) to fill to the end. Right now the column just ends at whatever the largest width is, I still want that to happen, but I want the smaller widgets to fill to that size as well
Thanks!
did you try to change the size policy of widgets in the layout to expanding?
according to the document :
QSizePolicy::Expanding : The sizeHint() is a sensible size, but the widget can be shrunk and still be useful. The widget can make use of extra space, so it should get as much space as possible (e.g. the horizontal direction of a horizontal slider).
Related
I have a small program which contains a QGroupBox with other widgets like this:
I tried many ways to manage the size of the QGroupBox to make the height as the same as the rest of the parts. Except for the way of using setMaximumHeight, because I want the size to change dynamically with the window size too. what else can I do to manage the layout?
Right now there are three items in a layout. The layout will try to fill the available space. QLineEdit and QSpinBox (or whatever your second widget is) have SizePolicy.vertical == fixed, so all extra space goes to the QGroupBox.
You have these choices:
Add a vertical spacer as fourth item below the groupbox to your layout.
Set maximum height of your groupbox - then the remaining space will be evenly spaced between the items.
Adjust the size of your window / widget / dialog (in Qt Designer or via code).
For exemple, I want the value of the layoutLeftMargin property to be equal to 1/3 of the parent widget. So when I will resize the windows, the ratio of the widget will still stay the same.
Else, if it's not possible with QtDesigner, how can I do it with code ?
No, Margins are specified in pixels, they can't be relative to the parent widget's size.
However, You can do that in the designer by putting your whole current layout in a Horizontal Layout, add a Horizontal Spacer to the left of it, assign suitable layoutStretch values in the horizontal layout (In your example, this should be 1,2, meaning the original layout will take up twice the space taken up by the spacer, so that the spacer gets 1/3 of the parent widget).
I'm developing an app with a complex hierarchy of widgets and layouts, but in short it has a central widget with a formulary as upper widget and a QScrollArea as buttom widget (by means of a QVBoxLayout).
That QScrollArea represents a list (grid layout indeed) of QPushButtons which can contain a huge number of buttons (or not).
I want my app fits the following constraints:
Both (form and list) consume all available horizontal space, redistributing its contents to fill all horizontal space (nor SpaceItems neither contents margins).
Both must save as vertical space as possible, in order to make "lines" close to each other.
I've solve partially my problem making use of setSizeConstraint(QLayout::SetFixedSize) on the form, which shrinks it vertically, but also horizontally, causing that both, list and form, have different widths, wich doesn't look like very well.
How can I achieve that? I mean, how can specify something like grow horizontally to fill the widget but shrink vertically has much as possible?
Add a spacer as the last item to the layout:
gridLayout->addItem(new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Expanding), lastrow, 0);
I think this is what you want:
If you know how many columns you will have (and it doesn't change), insertStretch() in the last column (although it might give you the same effect as using a spacer).
int columnCount = gridLayout()->columnCount();
gridLayout->insertStretch( columnCount(), 1 ); // Default stretch for other
Note that this will resize your buttons to the size Qt thinks they should be unless you are explicitly changing their widths.
I have a QGridLayout and a QScrollArea inside one of the columns. Also, I have a QGroupBox inside the QScrollArea, where I list a number of combo boxes. Basically, what I want to do is avoid having horizontal scroll bars in the QScrollArea, and only have the vertical bars if the number of combo boxes is large. This can be done by fixing the width of the QGroupBox.
However, I don't have the size hardcoded, and rather allow the QScrollArea to grow as much as the column allows. Once the elements are drawn, they're fixed (no resizing).
So, basically, how can I find the size of a column in QGridLayout? Once I find that, I can limit the widths accordingly.
Thanks!
If all you want is to avoid displaying horizontal scroll bars within a QScrollArea, simply call
QScrollArea::setHorizontalScrollBarPolicy()
with Qt::ScrollBarAlwaysOff. To get the width and height of a QGridLayout cell, use the following code:
QSize getLayoutCellSize(QGridLayout *layout, int row, int column)
{
QLayoutItem *item = layout->itemAtPosition(row, column);
if (item)
return (item->sizeHint());
return (QSize());
}
You may can also use QLayoutItem::geometry() instead of QLayoutItem::sizeHint().
I have two widgets, one arbitrary (usually a QLineEdit), and one QLabel which displays a Pixmap. They are placed next to each other with a QHBoxLayout. The widget with this layout is in turn placed in another layout.
Now, what I want is that the label with the pixmap is automatically resized so that it is as high as the arbitrary widget next to it. However, even when I set the label's sizePolicy to Maximum, it still seems to expand to the original pixmap size, instead of resizing the pixmap and shrinking to match the other widget. Instead of having two equally large widgets I have the arbitrary one which is smaller than the pixmap next to it.
Any ideas how to get the size of the pixmap label to match the size of the widget next to it?
How about:
int height = arbitraryWidget->height(); // get desired height.
label->setSizeHint(QSize(label->width(), height); // set size hint to current width and desired height.
label->setSizePolicy(QSizePolicy::Fixed); // optional, but this ensures size is desired.