QLayout: Attempting to add QLayout "" to QWidget "", which already has a layout QLayout: Attempting to add QLayout "" to QWidget "", which already has a layout python python

QLayout: Attempting to add QLayout "" to QWidget "", which already has a layout


When you assign a widget as the parent of a QLayout by passing it into the constructor, the layout is automatically set as the layout for that widget. In your code you are not only doing this, but explicitly calling setlayout(). This is no problem when when the widget passed is the same. If they are different you will get an error because Qt tries to assign the second layout to your widget which has already had a layout set.

Your problem lies in these lines:

grid_inner = QtGui.QGridLayout(wid)wid_inner = QtGui.QWidget(wid)

Here you are setting wid as the parent for grid_inner. Qt wants to set grid_inner as the layout for wid, but wid already has a layout as set above. Changing the above two lines to this will fix your problem. You can remove calls to setLayout() as they are redundant.

wid_inner = QtGui.QWidget(wid)grid_inner = QtGui.QGridLayout(wid_inner)

Use one method or the other for setting the layout to avoid confusion.

Assigning widget as parent:

widget = QtGui.QWidget()layout = QGridLayout(widget)

Explicitly setting the layout:

widget = QtGui.QWidget()layout = QGridLayout()widget.setLayout(layout)