In qt designer, I added a grid layout. then I made the main window to grid layout. see fig below.
then I added 2 vertical layouts inside the grid layout. see fig below
now the problem is that i can not change the size of 2 vertical layouts. I just wanted width of one of them to be 1/3 and the other to be 2/3. but the back to origin and they are always half.
you can change the way the layout is
stretched or distributed, for that there is the property layoustretch, in the doc especified https://doc.qt.io/qt-5/layout.html as stretch factor
\
you can give a try and set that 1,2 and see that the whole layout is distributed to a proportion of 33% to the left part and 66% for the right part.
Related
I am trying to have a QTreeView fit into a horizontal layout perfectly.
My layout setup looks like this:
I want the horizontal width of Vertical Layout 1 to be defined by the required widths of the QTreeView Column Headers. In other words the Vertical Layout 1 should stretch horizontally to make sure its width always exactly matches the width required by QTreeView column headers. Therefore the layout stretch factors of the topmost horizontal layout are set to (0,1) so that the Vertical Layout 1 does not stretch and Vertical Layout 2 does.
This is what it looks like in action:
The good news is that when QTreeView items are expanded, the headers also expand and in turn the Vertical Layout 1 stretches accordingly.
The problem is however highlighted on this picture:
For some reason there is redundant unused space within QTreeView itself, which has constant width in both collapsed and expanded states.
All the headers within the QTreeView are set to Resize to Contents, the Size policy of the QTreeView is set to 'AdjustToContents' and last header section stretch is set to False, which I think is correct:
self.accountsTree.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.SizeAdjustPolicy.AdjustToContents)
self.accountsTree.header().setStretchLastSection(False)
self.accountsTree.header().setSectionResizeMode(
AccountTreeColumns.COLUMN_NAME,
QHeaderView.ResizeMode.ResizeToContents,
)
self.accountsTree.header().setSectionResizeMode(
AccountTreeColumns.COLUMN_BALANCE,
QHeaderView.ResizeMode.ResizeToContents,
)
self.accountsTree.header().setSectionResizeMode(
AccountTreeColumns.COLUMN_BALANCE_BASE,
QHeaderView.ResizeMode.ResizeToContents,
)
self.accountsTree.header().setSectionResizeMode(
AccountTreeColumns.COLUMN_SHOW,
QHeaderView.ResizeMode.ResizeToContents,
)
I am using PyQt 6.4 and Qt Designer.
I tried playing around with stretch factors, layout constraints and different header section resize modes, but everything I tried only led to worse results. I don't understand where the extra space comes from, other than that the setup works exactly how I want it to. I must be missing some setting somewhere but I can't seem to figure it out.
Thanks for any tips
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'd appreciate advise on to how create a Qt UI consisting of four layouts and has the following properties.
Any increases in the height of the UI is absorbed by the layouts, as shown below
Any further decreases in the height of the UI is absorbed by the contents of a specific layout, e.g. the two large buttons as shown below
1# Create new UI form base on QWidget: File -> New file or project -> Qt -> Qt Designer form class -> select Widget form templates, next, next, select project and finish
2# Add Vertical layout from left bar
3# After that click right somewhere on UI form, where is not just added layout, "Lay out" -> "Lay out in a grid"
4# You can adjust layout margin on right menu (I'm always setting 5 points)
5# Add four Horizontal layout
6# Add Button and text and what you need
7# Add Vertical spacers between Horizontal layout
8# Final result:
I prepared quick code what fits your problem, please take a look: https://github.com/troyane/StackOverflow-pro/tree/master/creating-auto-scaling-qt-ui-using-layouts
Grab that code and take a look at next moments (you can open mainwindow.ui in QtCreator):
centralWidget has next layoutStretch param: 1,2,1,1 -- it means, that we'll have next correlation among all items placed into this vertical layout.
TextLabel and both SmallButtons has Fixed VerticalPolicy
Both BigButtons has Minimum vertical policy and set minimumSize's Height to 100. UPD: Also maximumSize->height parameter is 250 px. So, it is guarantee that both BigButtons will not grow more than 250 px on height.
Take a look at another answer, there you can find lots of literature to read about Layouts.
You can put two vertical spacers in each layout. One should be placed at top most and the other one at the bottom :
I had a layout all nicely designed in Qt, but as soon as I clicked on the parent window and set it to a grid layout, things got all wonky. I've read every tutorial I can find as well as the Qt designer manual and just cannot figure out why this is happening. I have attached a screenshot to show the problem:
As you can see, the vertical layout on the left insists on being wider than the children it contains. Both the label and the treeview are set to sizePolicy maximum, and the maximum width is set to 260px. The children themselves stay the correct size, but the vertical layout that contains them doesn't.
The vertical layout in the middle is set to expanding, and the one on the far right is setup the same as the one on the left, only that one appears to work. How do I make the first vertical layout conform to it's children's width?
Also, if I may sneak a second question in, I have a QTextEdit inside the tab widget in the lower right, but it will not fill to take up the space of the full tab view. You can't see that in the screenshot, but if I pull the tabview up, the textedit within it doesn't stretch with it. How do I make it conform to the size of the tab? It's already set to sizePolicy expanding, but that doesn't seem to help.
The problem is most likely that you need to experiment with "stretching" the layout. Stretch sets the size of layout cells in relation to each other. The default is 0, which means no stretching occurs.
In your case, I believe you want to set the stretch of the first column (column 0) to 0, and the stretch of the second and third columns to 1. This means that the first column will always be as small as possible, and the second and third columns will try to be equally wide.
You can set the stretch programmatically quite easily; for example, to set the first column to stretch 0:
layout->setColumnStretch(0, 0);
In Qt Designer you can access column and row stretches as any normal properties.
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.