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

1

33

I want to create some tabs, and I read this answer: How to add a tab in PySide

I use the code in the answer and made some changes. Cause my code has to read some files and get the name of my tabs from those file, so that I add a for loop in my code. And here is my code.

from PySide import QtCore, QtGui
import sys
import dflash_controller as con

if __name__ == "__main__":
    list = [['a', 3], ['b', 4], ['c', 5], ['d', 6]]
    app = QtGui.QApplication(sys.argv)
    wid = QtGui.QWidget()
    grid = QtGui.QGridLayout(wid)
    wid.setLayout(grid)

    # setting the inner widget and layout
    grid_inner = QtGui.QGridLayout(wid)
    wid_inner = QtGui.QWidget(wid)
    wid_inner.setLayout(grid_inner)

    # add the inner widget to the outer layout
    grid.addWidget(wid_inner)

    # add tab frame to widget
    wid_inner.tab = QtGui.QTabWidget(wid_inner)
    grid_inner.addWidget(wid_inner.tab)

    # create tab


    for i, index in enumerate(list[0:]):
        new_tab = QtGui.QWidget(wid_inner.tab)
        grid_tab = QtGui.QGridLayout(new_tab)
        grid_tab.setSpacing(10)
        wid_inner.tab.addTab(new_tab, index[0])
        new_tab.setLayout(grid_tab)


    wid.show()
    app.exec_()

It really shows up my tabs. However, I met a warning: QLayout: Attempting to add QLayout "" to QWidget "", which already has a layout Since this tab code just a part of the whole code, the problem will block the data flow. And I have no idea what's wrong with it. I searched for answers, but other answer aren't written in python.

If anyone can help me, thanks in advance.

Bonaventura answered 22/8, 2014 at 15:30 Comment(0)
I
50

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)
Inapprehensible answered 22/8, 2014 at 16:12 Comment(3)
Thanks. I think you mean use one of those two ways can deal with the problem. But I tried both, the problem still exist. Do I still have anything wrong inside?Bonaventura
By the way, I noticed that the code in: How to add a tab in python can run perfectly. Is that the for loop part which is I changed is the source of the problem?Bonaventura
After a second look I've realised your problem occurs because you are parenting wid instead of wid_inner to grid_inner, which is attempting to re-set the layout for wid. I had corrected this when I tested your code. I've updated my answer accordingly.Inapprehensible

© 2022 - 2024 — McMap. All rights reserved.