Python Forum

Full Version: How to clip layout to sides and bottom of main window?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
A simple window (QWidget) with a status bar. I add some kind of layout to insert a textbox and a few labels in a horizontal line-up. But how to I attach the layout - haven't decided yet which one - to the left, right and bottom of the window? So that if the window resizes or even maximizes, the layout always stays at the bottom or at least attacked to the sides? (Wasn't this called "clipping"?).

I thought for a second that maybe there's a QWidget Resize Signal which I could attach to some slot to resize the frame and repaint the the frame and its contents or something, but such a signal doesn't exist.
Qt layouts grow to fill the window. What are you trying to do? You can achieve different affects by using layouts in layouts, layouts in widgets in layouts, different kinds of layouts, setting things like alignment for where widgets appear in layout, and setting stretch factors in a layout. If you can describe what you are trying to achieve I may be able to help you.
(Mar-24-2021, 02:28 AM)deanhystad Wrote: [ -> ]Qt layouts grow to fill the window. What are you trying to do? You can achieve different affects by using layouts in layouts, layouts in widgets in layouts, different kinds of layouts, setting things like alignment for where widgets appear in layout, and setting stretch factors in a layout. If you can describe what you are trying to achieve I may be able to help you.

This is in Qt Designer. But I don't mind if it's in code; I'm not too fond of the Designer anyway. I'm beginner (I'm used to C# code, I don't mean that, but Qt GUI development). So please explain the relevant properties, methods, instead of "only" providing a solution :)
[Image: Og0o2BA.png]

Final goal:
[Image: PSrdLEj.png]

Here's another example with a line edit. The right edge of the line edit needs to touch the edge of its parent on the right.
This is in a horizontal layout. So if the layout auto-sizes with the form, then the line edit must autosize with the layout.
[Image: xz6oz4G.png]
Use this simple program to get a feel for how the HBox and VBox layout widgets work. Start by commenting out the "setStretch" commands and see how the labels are positioned as you stretch the window. Then uncomment the "content.setStretch" command to pin the "Left" and "Right" labels to the bottom of the screen.
import sys
import PySide2.QtWidgets as Qt

class MyWindow(Qt.QWidget):
    def __init__(self):
        super().__init__()
        content = Qt.QVBoxLayout(self)
        content.addWidget(Qt.QLabel('This is the main body'))
        row = Qt.QHBoxLayout()
        content.addLayout(row)
        row.addWidget(Qt.QLabel('Left Side'))
        row.addWidget(Qt.QLabel('Right Side'))

        # Up to now the different screen regions all resize proportionally

        # This makes the row stick to the bottom of the window.  It does this
        # by giving all the vertical "stretch" to the "main body" label
        content.setStretch(0, 1)

        # This does the same thing for the horizontal stretch of the bottom row
        row.setStretch(1, 1)

app = Qt.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
If you use QGridLayout you can get the same kind of effect by setting setColumnStretch and setRowStretch.
Oh wait a minute, when I design this via Qt Designer, the IDE adds something called "central widget" to the MainWindow (QWidget). So first this central widget needs to be layed out first and then you can add additional layouts. What I wanted was actually easy. Just use alignment and layoutStretch, optionally with sizePolicy.

But that's the gist of it, I didn't know the centralWidget forced upon me by the IDE wasn't layed out automagically.

Thanks for your help nevertheless. I find out one minute (probably) before you posted.
I haven't used designer, but I am pretty sure you are not forced to use a main window. Mainwindow only makes sense to use if you want the menubar, statusbar or docking bits. For simple windows like dialogs it is massive overkill. I'm sure there is a way to tell designer to use a different base window class.
What I mean is that Qt Designer forces this upon me:
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1095, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.horizontalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
[Image: Imoy3eE.png]
I was just looking around for info and found this for designing dialogs. It appears you pick a template for designer to use, and MainWindow is just the default template.

https://www.learnpyqt.com/tutorials/crea......%20More
Ugh, now I'm fighting with keeping the aspect ratio during resizing of the window. What a nightmare.
I never do anything to try to maintain an aspect ratio. Why does it matter to you?