Search code examples
pythonpyqtpyqt5qsplitter

Resizing QSplitter doesn't respect QSizePolicy or setStretchFactor?


I set the size policy of items in a QSplitter but it doesn't seem to affect anything. When I resize the window in the example below I want my left widget to stop growing once it reaches its size hint (QSizePolicy.Preferred) and the right widget to expand as big as possible (QSizePolicy.Expanding).


It works if I put them in a layout:

from PyQt5 import QtWidgets, QtCore

app = QtWidgets.QApplication([])


class ColoredBox(QtWidgets.QFrame):
    def __init__(self, color):
        super().__init__()
        self.setStyleSheet(f'background-color: {color}')

    def sizeHint(self) -> QtCore.QSize:
        return QtCore.QSize(200, 200)


box1 = ColoredBox('red')
box2 = ColoredBox('green')

splitter = QtWidgets.QHBoxLayout()
splitter.setContentsMargins(0, 0, 0, 0)
splitter.setSpacing(0)

splitter.addWidget(box1)
splitter.addWidget(box2)

box1.setSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
box2.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)

container = QtWidgets.QWidget()
container.setLayout(splitter)
container.show()

app.exec_()

But it doesn't work if I put them in a QSplitter, the splitter just splits space between them evenly:

from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import Qt

app = QtWidgets.QApplication([])


class ColoredBox(QtWidgets.QFrame):
    def __init__(self, color):
        super().__init__()
        self.setStyleSheet(f'background-color: {color}')

    def sizeHint(self) -> QtCore.QSize:
        return QtCore.QSize(200, 200)


box1 = ColoredBox('red')
box2 = ColoredBox('green')

splitter = QtWidgets.QSplitter(Qt.Horizontal)
splitter.setStretchFactor(0, 0)
splitter.setStretchFactor(1, 1)

splitter.addWidget(box1)
splitter.addWidget(box2)

box1.setSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
box2.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)

container = QtWidgets.QMainWindow()
container.setCentralWidget(splitter)
container.show()

app.exec_()

Layout vs Splitter:

enter image description here enter image description here


Solution

  • The problem comes from the fact that you're setting the stretch factor too early.

    • setStretchFactor() is ignored if done on a widget index that doesn't exist yet (see the source code for its implementation);
    • QSplitter resizes the widget proportions based on their size policy; the size policy also includes the horizontal and vertical stretch factors, and they're reset to the 0 default value whenever set again with the basic setSizePolicy(horizontalPolicy, verticalPolicy) (which means that you can specify the stretch in the QSizePolicy, which is the same as using setStretchFactor).

    The solution is simple: move the setStretchFactor lines after adding the widgets and setting their policies.

    Obviously, a possible alternative is to set the maximum dimension (based on the QSplitter orientation), but that is not exactly the same thing.
    If all widgets in the splitter have a maximum size (including situations for which only one widget exists), the result is that the splitter will have a maximum size based on those widgets, depending on other widgets in (or "above") its layout, but if the splitter is the top level window then there will be some blank space remaining whenever it's resized to a size bigger than the maximum of its child[ren].