QLayout:尝试添加QLayout“".到已经具有布局的QWidget“" [英] QLayout: Attempting to add QLayout "" to QWidget "", which already has a layout

查看:535
本文介绍了QLayout:尝试添加QLayout“".到已经具有布局的QWidget“"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一些标签,然后阅读以下答案:如何在PySide中添加标签页

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

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

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_()

它确实显示了我的标签.但是,我遇到了警告: QLayout:试图将QLayout"添加到已经具有布局的QWidget"中 由于此选项卡代码只是整个代码的一部分,因此该问题将阻止数据流.而且我不知道这是怎么回事.我搜索了答案,但其他答案不是用python编写的.

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.

推荐答案

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

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)

在这里,您将wid设置为grid_inner的父级. Qt希望将grid_inner设置为wid的布局,但是wid已经具有上面设置的布局.将以上两行更改为此将解决您的问题.您可以删除对setLayout()的呼叫,因为它们是多余的.

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.

将小部件指定为父级:

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

明确设置布局:

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

这篇关于QLayout:尝试添加QLayout“".到已经具有布局的QWidget“"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆