pythonpyqt5qwidgetqvboxlayout

How to make a layout fill the entire window in PyQt5?


I'm creating an app in PyQt5 and I want a layout that fills the entire window, except the title bar.

When I try to set a QVBoxLayout to a QWidget, there remains a gap, as seen below:

App

This is my code:

def main():
    app = QApplication(sys.argv)
    win = QWidget()

    win.setFixedSize(225,150)

    label = QLabel("Some Label")
    label.setAlignment(QtCore.Qt.AlignCenter)
    label.setStyleSheet('background: red')

    layout = QVBoxLayout()
    layout.addWidget(label)

    win.setLayout(layout)

    win.show()
    sys.exit(app.exec_())

main()

So how do I remove the gap?


Solution

  • If I understand you correctely, just add

    layout.setContentsMargins(0,0,0,0)
    

    anywhere after creating layout object and before sys.exit(app.exec_()).