Search code examples
qtpyqt5qgraphicsview

What is limitting the minimum size of a QGraphicsView widget when in a layout?


If I have a QGraphicsView widget on its own, I can resize it down to nothing, however once I put it in a layout it does not want to resize smaller than 70x70.

This does not appear to be a problem with a generic QWidget.

from PyQt5.QtWidgets import *

app = QApplication([])
w = QWidget()

gv1 = QGraphicsView()
gv1.setMinimumSize(0,0)
gv2 = QGraphicsView()
gv2.setMinimumSize(0,0)

l = QGridLayout()
l.addWidget(gv1)
l.addWidget(gv2)

l.setContentsMargins(0,0,0,0)
l.setSpacing(0)
w.setLayout(l)
w.show()
w.resize(100, 1)

app.exec_()

Solution

  • As the minimumSize() documentation says:

    The minimum size set by this function will override the minimum size defined by QLayout. In order to unset the minimum size, use a value of QSize(0, 0).

    This means that if you use setMinimumSize(0, 0), you're resetting the minimum size, which will then default to the widget's (or layout) minimumSizeHint().

    A valid minimum size direction should always be at least 1.
    Change to:

    gv1.setMinimumSize(1, 1)
    gv2.setMinimumSize(1, 1)