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).
Related
When I maximize my window, I want to restrict a vertical layout (and the entire row below it also) so that it has a specific size (lets say a width of 200). How can I achieve this? Below is what I get now. The buttons are stretched too far. I want them to keep a width of 200.
To stop the buttons stretching, use the following steps in Qt Designer:
click on scrollArea in the Object Inspector
click on Break Layout on the toolbar
click on scrollArea in the Object Inspector
click on Lay Out in a Grid on the toolbar
click on scrollAreaWidgetContents in the Object Inspector
scroll down to the bottom of the Property Editor
change layoutColumnStretch to 0,1
These steps should remove an empty column from the scroll-area grid-layout, and make the second column stretch to take up the available space when the window is resized.
You just need to restrict the maximum width of all widgets (in this case the buttons) within the layouts of this grid column to the expected size, else they'll just keep expanding. You may also have to fiddle the horizontal size policy; I seem to remember that buttons were a bit tricky in this regard (or was that the height?), but can't test it right now.
The layout size contraint you tried only applies to the layout's direct parent widget, if it has one, which isn't the case for the vertical layouts here.
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 QTableWidget, QTextEdit, and cancel & okay buttons.
I want these widgets to stay in the same position relative to each other, and the QTableWidget to expand if the dialog window is expanded or size changed...
How can I do that?
You need to look into Qt's layout system - using layouts will handle automatically resizing your objects based on the size of their parent.
A combination of using QWidget::setSizePolicy() and QBoxLayout::setStretch() (or more likely QBoxLayout::insertWidget(..., int stretch = 0, ...)) will allow you to acheive the behaviour you refer to where only certain objects expand to fill available space, while others remain a constant size.
Addressing the image you've given above as an example:
Aside from dragging and dropping objects into the form, to achieve this solution I have:
Set the vertical sizePolicy of textEdit to Fixed.
Set a height in textEdit's minimumSize for the sizePolicy to use.
Set layoutStretch in centralWidget to 1,0, i.e. assign the minimum possible space for the elements contained in horizontalLayout and give any remaining space to tableWidget.
I'm having trouble using the layout manager system with Qt. This is going to be a Symbian app, so it should resize to different devices.
This is done by using the Layouts.
On the image below I used the Vertical Layout, but I don't understand how I can decide how much each cell should take in width and height.
I want the blue to be a top label background, but I don't want it to be as high as it is now.
Does anyone know how I can do this? (I'm new to Qt :))
You can set the maximum size for a widget by right clicking it and selecting 'Size Constraints'. Under that menu you can find actions that allow you to set the current displayed size as the maximum / minimum size for vertical / horizontal or both directions.
You can also set the numbers by hand by selecting the widget and by setting the number in the 'Property Editor'. They should be under the QWidget properties.
You cannot set the Height of a vertical layout directly, but you can set the height of the widget in which the vertical layout is.
If you want to split your Widgets so that the top widget takes 33.33% of the space, use the Stretch values. Set the top widget to 1 and the bottom widget to 2.