Python QLayout: 试图将 QLayout "" 添加到已经有布局的 QWidget ""

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25450598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:13:59  来源:igfitidea点击:

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

pythontabspysideqwidgetqlayout

提问by EricBkc

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

我想创建一些选项卡,我阅读了这个答案:如何在 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.

我在答案中使用了代码并进行了一些更改。因为我的代码必须读取一些文件并从这些文件中获取我的选项卡的名称,以便我在我的代码中添加一个 for 循环。这是我的代码。

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 layoutSince 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.

它真的显示了我的标签。然而,我遇到了一个警告:QLayout: Attempting to add QLayout "" to QWidget "",它已经有了布局由于这个标签代码只是整个代码的一部分,所以问题会阻塞数据流。我不知道它有什么问题。我搜索了答案,但其他答案不是用 python 编写的。

If anyone can help me, thanks in advance.

如果有人可以帮助我,请提前致谢。

采纳答案by user3419537

When you assign a widget as the parent of a QLayoutby 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.

当您通过将小部件QLayout传递给构造函数来将其分配为 a 的父级时,布局将自动设置为该小部件的布局。在您的代码中,您不仅执行此操作,而且显式调用setlayout(). 当传递的小部件相同时,这没有问题。如果它们不同,您将收到错误消息,因为 Qt 尝试将第二个布局分配给已经设置了布局的小部件。

Your problem lies in these lines:

你的问题在于这些行:

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

Here you are setting widas the parent for grid_inner. Qt wants to set grid_inneras the layout for wid, but widalready 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.

在这里,您将设置widgrid_inner. Qt 想设置grid_inner为 的布局wid,但wid已经有如上设置的布局。将以上两行更改为此将解决您的问题。您可以删除对的调用,setLayout()因为它们是多余的。

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)