Python Forum
[PyQt] How to clip layout to sides and bottom of main window?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] How to clip layout to sides and bottom of main window?
#1
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.
Reply
#2
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.
Reply
#3
(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]
Reply
#4
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.
Reply
#5
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.
Reply
#6
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.
Reply
#7
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]
Reply
#8
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
Reply
#9
Ugh, now I'm fighting with keeping the aspect ratio during resizing of the window. What a nightmare.
Reply
#10
I never do anything to try to maintain an aspect ratio. Why does it matter to you?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGUI] How to reuse a window layout based on mysql rowcount? retroisbest 2 236 Nov-09-2021, 09:02 AM
Last Post: retroisbest
  [PyQt] Can't get MDIarea to resize automatically with Main Window JayCee 4 637 Aug-02-2021, 08:47 PM
Last Post: JayCee
  "tkinter.TclError: NULL main window" Rama02 1 1,764 Feb-04-2021, 06:45 PM
Last Post: deanhystad
  pyqt5 layout Nickd12 8 1,363 Jan-18-2021, 09:09 AM
Last Post: Axel_Erfurt
  [Tkinter] Hi, Keep postition of main window after iconify() delphinis 3 1,292 Jul-12-2020, 06:59 AM
Last Post: DT2000
  [Tkinter] Auto re-fit frames sizes in main window Gilush 2 1,202 Jun-06-2020, 03:14 AM
Last Post: Gilush
  [Tkinter] How to add multiple frames to main window Dandy_Don 13 3,922 Apr-29-2020, 09:21 PM
Last Post: Dandy_Don
  tkinter window and turtle window error 1885 3 3,511 Nov-02-2019, 12:18 PM
Last Post: 1885
  “main thread is not in main loop” in Tkinter Long_r 1 14,071 Jun-26-2019, 11:00 PM
Last Post: metulburr
  Python GUI layout off between different OS shift838 5 1,903 Jan-02-2019, 02:53 AM
Last Post: shift838

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020