How to remove the distance between blocks in QGridLayout when Fixed / maximum size of widgets?

If you try to limit the size of the widget, then only the horizontal space disappears when spacing=0, is it possible to remove the vertical space using QGridLayout?

I guess that you can avoid this by using QVBoxLayout / QHBoxLayout, I didn't find any examples with QGridLayout:

def __init__(self):

    QtWidgets.QMainWindow.__init__(self)

    gridbox = QtWidgets.QGridLayout(spacing = 0)

    box1 = QtWidgets.QWidget(self)
    box2 = QtWidgets.QWidget(self)
    boxes = [box1, box2]
    for box in boxes:
        box.setStyleSheet("background-color: red;")
        box.setFixedSize(200, 100)

    gridbox.addWidget(box1, 0, 0)
    gridbox.addWidget(box2, 0, 1)

    widget =  QtWidgets.QWidget(self)
    mainbox = QtWidgets.QVBoxLayout(widget)
    mainbox.addLayout(gridbox)
    self.setCentralWidget(widget)
    self.setMinimumSize(500, 500)
Author: S. Nick, 2020-06-03

2 answers

You can set the distance between the elements in this layout as follows:

grid = QGridLayout() #QGridLayout(spacing = 0) тоже работает

# Установка вертикального и горисонтального расстояния между виджетами 
grid.setSpacing(0)

# Установка горизонтального расстояния между виджетами 
grid.setHorizontalSpacing(0)

# Установка вертикального расстояния между виджетами 
grid.setVerticalSpacing(0)

Sample code:

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        gridbox = QGridLayout(spacing = 0)

        box1 = QWidget(self)
        box2 = QWidget(self)
        box3 = QWidget(self)
        box4 = QWidget(self)
        box1.setStyleSheet("background-color: red;")
        box2.setStyleSheet("background-color: green;")
        box3.setStyleSheet("background-color: yellow;")
        box4.setStyleSheet("background-color: blue;")

        gridbox.addWidget(box1, 0, 0)
        gridbox.addWidget(box2, 0, 1)
        gridbox.addWidget(box3, 1, 0)
        gridbox.addWidget(box4, 1, 1)

        panel =  QWidget(self)
        panel.setLayout(gridbox)
        self.setCentralWidget(panel)


if __name__ == "__main__":
    app = QApplication([])
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

It is easier to fix/control the size of the widget-the panel on which all this is located (in the example, this is panel), than to simply set size restrictions on internal widgets and try to control the distance between them. It seems to me that it is possible to combine all this.

If there is no space between the internal widgets, the size of the panel widget is the sum of the internal sides.

For example:

box1.setFixedSize(200, 100)
box2.setFixedSize(200, 100)

Then the panel dimensions are:

panel.setFixedSize(QSize(400,200))
 0
Author: Alexander Chernin, 2020-06-10 06:17:15

QGridLayout::setColumnStretch(int column, int stretch)

Sets the stretch coefficient for the column. The first column is the number 0.

The coefficient of stretching relative to the other columns in this grid. Columns with a higher stretch factor take up more available space.

The default stretch factor is 0.

import sys
from PyQt5 import QtWidgets


class MyWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        widget =  QtWidgets.QWidget()
        self.setCentralWidget(widget)
#        self.setMinimumSize(500, 500)
        self.resize(500, 500)

        self.box1 = QtWidgets.QWidget(styleSheet="background-color: red;")
        self.box1.setFixedSize(200, 100)
        self.box2 = QtWidgets.QWidget(styleSheet="background-color: green;")
        self.box2.setFixedSize(200, 100)

        gridbox = QtWidgets.QGridLayout(widget, spacing=0)
        gridbox.addWidget(self.box1, 1, 1)
        gridbox.addWidget(self.box2, 1, 2)   
        gridbox.setColumnStretch(0, 1) 
        gridbox.setColumnStretch(3, 1)        
   

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    screen = MyWindow()
    screen.show()
    sys.exit(app.exec_())

enter a description of the image here

 0
Author: S. Nick, 2020-11-26 23:15:30