Python Forum
[PyQt] QVBoxLayout default (minimum) size - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [PyQt] QVBoxLayout default (minimum) size (/thread-38407.html)



QVBoxLayout default (minimum) size - malonn - Oct-09-2022

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.


RE: QVBoxLayout default (minimum) size - deanhystad - Oct-10-2022

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)



RE: QVBoxLayout default (minimum) size - Axel_Erfurt - Oct-10-2022

You can adjust the margins

layout.setContentsMargins(1,2,3,4)


RE: QVBoxLayout default (minimum) size - malonn - Oct-10-2022

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.


RE: QVBoxLayout default (minimum) size - deanhystad - Oct-10-2022

I set margins to 0 if I am making a layout (or window) that is added to a layout, otherwise use the defaults.


RE: QVBoxLayout default (minimum) size - malonn - Oct-13-2022

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.