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

后端 未结 1 679
执笔经年
执笔经年 2020-12-05 05:24

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 fil

相关标签:
1条回答
  • 2020-12-05 05: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)
    
    0 讨论(0)
提交回复
热议问题