qtresizeqmainwindowqlayoutqsizepolicy

Qt: HBoxLayout - stop MainWindow from resizing to contents


It seems most people are asking how to make their QMainWindow resize to its contents - I have the opposite problem, my MainWindow does resize and I don't know why.

When I set my QLabel to a longer text, my mainwindow suddenly gets bigger, and I can't find out why that happens.

The following example code basically contains:

The LabelBar is a QWidget which in turn contains:

After a second, a QTimer sets the label to a longer text to demonstrate the issue.

PyQt example code:

from PyQt5.QtWidgets import (QApplication, QHBoxLayout, QLabel, QWidget,
                             QMainWindow, QVBoxLayout, QSizePolicy)
from PyQt5.QtCore import QTimer

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        cwidget = QWidget(self)
        self.setCentralWidget(cwidget)
        self.resize(100, 100)

        vbox = QVBoxLayout(cwidget)
        vbox.addWidget(QWidget())
        self.bar = LabelBar(self)
        vbox.addWidget(self.bar)

        timer = QTimer(self)
        timer.timeout.connect(lambda: self.bar.lbl2.setText("a" * 100))
        timer.start(1000)

class LabelBar(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        hbox = QHBoxLayout(self)
        self.lbl1 = QLabel(text="eggs")
        hbox.addWidget(self.lbl1)
        self.lbl2 = QLabel(text="spam")
        hbox.addWidget(self.lbl2)

if __name__ == '__main__':
    app = QApplication([])
    main = MainWindow()
    main.show()
    app.exec_()

Solution

  • Main window grows because it's the goal of using layouts. Layouts make size requirements for their widgets to ensure that all content is displayed correctly. Requirements depend on child widgets. For example, QLabel by default grows horizontally and require space to fit its content. There are many ways to prevent window growing, and the resulting behavior varies: