How to delete the space between QLabel and QLineEdit - qt

I have a QDialog window like this and I want to delete the space between 'Length', 'n', 'm' and corresponding QLineEdit input boxes. How can I achieve that ?

If you use gridlayout, I am not sure why your output looks like that. Generally Qt will not leave huge empty space like that, There are three possibility I can think of:
You have many SPACE after Length:, M: or N:
The layoutHorizontalSpacing is too large in your grid manager.
The layoutColumnStretch was set to in favor of label in your grid manager, should be "0,0", not "1,0". I mean, stretch of Label should not be higher than lineedit.
Still, I would use a simple form layout in your application.

All you need to do is reset the alignment of the labels:
label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlighVCenter)
This can also be done in via the Property Editor in Qt Designer.

You have to decide what to do with the space you want to remove :
Reduce the whole widget : it depends on the surrounding layout, adding an horizontal layout on right or left of it will put the space out of your widget.
Space on the left of your labels : you can align the label text to the right as ekhumoro suggested, or directly reduce and align to the right the whole frame by adding an horizontal spacer on its left (in the surrounding layout).
Space on the right of your line-edits : like above you can add horizontal spacers or reduce and align the frame.
Expand the line-edits : remove their fixed width (default horizontal policy is expanding) or set a bigger one.
The point is : the space has to be somewhere except if you reduce the parent widget or enlarge some the inner widgets. Size-policy is useful to tell which widget should take the available space, spacers are useful to let empty space between widgets.

Related

How to make a QTreeView fit in QHBoxLayout perfectly?

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

Qt layouting: default size constraints vertically, setFixedSize horizontally

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.

Setting a widget's SizeHint

I have a QtGui.QVBoxLayout widget which exists inside a main QtGui.QGridLayout layout, which extends and centers the items all over the place (The left side)
I want the items to get stacked up at the top of the widget (The right side)
How can I achieve this? I know it has something to do with SizeHint, but I'm not sure how to use it.
A QBoxLayout will distribute the widgets evenly over the available space. The easiest way to keep all the widgets packed together at the top of a QVBoxLayout, is to add a stretchable spacing at the bottom that will push all the widgets up.
You can add a stretch that will take up as much space as it can with QBoxLayout::addStretch().

Qt layout is larger than it should be

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.

qt unexpandable layout?

Ok, here is my problem:
I have a vertical layout which contains a QPlainTextEdit and a horizontal layout (containing 2 QPushButtons) below the text edit.
The vertical layout is just a part of GUI, and gets resized depending on screen resolution. Btw. it is a mobile app, so I don't have a lot of space on screen.
Push buttons have some text which is dynamically set, I don't know it from the beginning to code it manually.
My problem occurs when the text in push buttons is big, and my whole vertical layout is expanded to fit the buttons.
How can I make the vertical layout unexpandable? note, that this is different from "fixed" because of different screen resoulutions.
I'd just like the clip the buttons if they do not fit, but keep the layout width untouched.
Anyway to do this?
You'll need to set the maximum width for the buttons, not the layout, which is only widening to fit the wider buttons. Check out the docs on QPushButton and look for QWidget inherited functions called setMaximumSize or setMaximumWidth.
You can always GetWidth() on the button when it is an appropriate size, then setMaximumWidth using that value since you wouldn't ordinarily know this. Pick an appropriate default text size/val and use that to create your "dynamic" default since this is going on screens of varying size.

Resources