QWidget::setLayout: Attempting to set QLayout “” on MainWindow “”, which already has a layout

You can’t set a QLayout directly on the QMainWindow. You need to create a QWidget and set it as the central widget on the QMainWindow and assign the QLayout to that.

wid = QtGui.QWidget(self)
self.setCentralWidget(wid)
layout = QtGui.QVBoxLayout()
wid.setLayout(layout)

NOTE: This is for Qt4 — see the other answer on this question for the Qt5 updated code.

Leave a Comment