Python Forum
[PyQt] QVBoxLayout default (minimum) size
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] QVBoxLayout default (minimum) size
#1
I noticed that the minimum size of a layout is related to its contents margins. All layouts have contentsMargins(9, 9, 9, 9) by default. So that means a minimum size of QSize(18, 18). Oddness is that that is true for QGridLayoyts, but for QVBoxLayout's, the default minimum size is QSize(22, 22). Where does the extra 4 for width and height come from? I assume it is left 2, top 2, right 2, and bottom 2. Where are those 2 pixels coming from? I haven't checked a QHBoxLayout, or QBoxLayout yet, but they should fall under being the sum of it margins, or close to it.
Reply
#2
According to the Qt documentation, layouts contentsMargins are usually 11, not 9.

https://doc.qt.io/qt-6/qlayout.html#setContentsMargins

Quote:By default, QLayout uses the values provided by the style. On most platforms, the margin is 11 pixels in all directions.
This is inherited from the base class QLayout, so I would expect it to be the same for all layout managers unless changed by a style.

I wrote a little test and this appears to be true for PySide6.
from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QGridLayout, QPushButton

class Box(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        layout = QVBoxLayout(self)
        print(layout.contentsMargins(), layout.minimumSize())


class Grid(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        layout = QGridLayout(self)
        print(layout.contentsMargins(), layout.minimumSize())

app = QApplication()
box = Box()
grid = Grid()
box.show()
grid.show()
app.exec()
Output:
<PySide6.QtCore.QMargins(11, 11, 11, 11) at 0x000001F1DEB4E380> PySide6.QtCore.QSize(22, 22) <PySide6.QtCore.QMargins(11, 11, 11, 11) at 0x000001F1DEB4E440> PySide6.QtCore.QSize(22, 22)
Reply
#3
You can adjust the margins

layout.setContentsMargins(1,2,3,4)
Reply
#4
Odd, in my testing QGridLayout and QVBoxLayout had contents margins of 9 all around. Must be the style I'm using. It's the default style, I haven't changed it.

Thanks though.
Reply
#5
I set margins to 0 if I am making a layout (or window) that is added to a layout, otherwise use the defaults.
Reply
#6
In the app I'm working on, I set margins to 0. I was just doing some research; trying to see what's what. The window/application size tends to grow as you add layouts (and of course, widgets). You can reduce that by cuttings margins. I also found that Qt tends to choose the minimum size of the layouts when working with large widgets (such as large pixmaps). Naturally, it can all be bent to your needs, but that's the way Qt does it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Trying to change font size w/o changing button size python63 3 9,728 Aug-05-2020, 01:04 AM
Last Post: Larz60+
  PyGtk3 why is Locale Folder Size Half of Module Size ? harun2525 1 3,582 Mar-09-2017, 03:46 AM
Last Post: Analyser

Forum Jump:

User Panel Messages

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