pyqt:如何从 QVBoxLayout 中删除元素? [英] pyqt: how to remove elements from a QVBoxLayout?

查看:30
本文介绍了pyqt:如何从 QVBoxLayout 中删除元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个多色选择小部件.我这样做的方式是有一个+"按钮和一个最初为空的 vbox.当按下 + 时,它会将 QHBoxLayout 添加到包含-"按钮和 3 个旋转框的 vbox.当按下-"按钮时,我希望该行消失,并且一切都回到添加该行之前的样子.我目前拥有的代码是:

I want a multi-color selection widget. The way I'm doing it is having a "+" button, and an initially empty vbox. When + is pressed, it adds a QHBoxLayout to the vbox containing a "-" button and 3 spinboxes. When the "-" button is pressed I want that row to disappear and everything to go back to looking like it did before that row was added. The code I currently have is:

    vbox = self.ui.color_layout #from QtDesigner

    hbox = QtGui.QHBoxLayout()
    remove = QtGui.QPushButton("-", parent=self)

    remove.clicked.connect(lambda: vbox.removeItem(hbox))

    rspin = QtGui.QSpinBox(parent=self)
    gspin = QtGui.QSpinBox(parent=self)
    bspin = QtGui.QSpinBox(parent=self)

    hbox.addWidget(remove)
    hbox.addWidget(QtGui.QLabel("R:", parent=self))
    hbox.addWidget(rspin)
    hbox.addWidget(QtGui.QLabel("G:", parent=self))
    hbox.addWidget(gspin)
    hbox.addWidget(QtGui.QLabel("B:", parent=self))
    hbox.addWidget(bspin)

    vbox.addLayout(hbox)

添加小部件工作正常.但是,删除它们会导致看起来非常混乱,其中行实际上并未删除,但间距全部混乱.

Adding widgets works fine. However, removing them results in a really messed-up looking thing where the row isn't actually removed, but the spacing is all messed up.

我做错了什么?

文档说,对于 removeWidget:

在此调用之后,调用者有责任为小部件提供合理的几何形状或将小部件放回布局中.

After this call, it is the caller's responsibility to give the widget a reasonable geometry or to put the widget back into a layout.

我该怎么做?(我来自 GTK 背景...)

How do I do that? (I come from a GTK background...)

编辑 2:我什至跟踪行并调用 takeAt 函数将其删除,但它仍然被搞砸了.是什么赋予了?看起来布局已被删除,但没有一个小部件...

EDIT 2: I even kept track of the rows and called the takeAt function to remove it, but it still gets messed up. What gives? It looks like the layout is removed but none of the widgets are...

编辑 3:这也不起作用,只是以类似的方式把事情搞砸了:

EDIT 3: this also doesn't work, just messes things up in a similar way:

    vbox = self.ui.color_layout

    hbox = QtGui.QHBoxLayout()

    row_widget = QtGui.QWidget(parent=self) #dummy widget to hold this stuff

    remove = QtGui.QPushButton("-", parent=self)

    def remove_func():
        vbox.removeWidget(row_widget)

    remove.clicked.connect(remove_func)

    rspin = QtGui.QSpinBox(parent=self)
    gspin = QtGui.QSpinBox(parent=self)
    bspin = QtGui.QSpinBox(parent=self)

    hbox.addWidget(remove)
    hbox.addWidget(QtGui.QLabel("R:", parent=self))
    hbox.addWidget(rspin)
    hbox.addWidget(QtGui.QLabel("G:", parent=self))
    hbox.addWidget(gspin)
    hbox.addWidget(QtGui.QLabel("B:", parent=self))
    hbox.addWidget(bspin)

    row_widget.setLayout(hbox)

    vbox.addWidget(row_widget)

推荐答案

尝试从父小部件中移除,而不是从布局中移除.

Try removing from parent widget, not the layout.

QLayout 不是父级,正在布局的小部件的父级实际上是布局的父级.有关更多信息和更清晰的解释,请参阅 有关 Qt 布局的文档.

QLayout is not a parent, the parent for widgets being laid out is actually layout's parent. For more info and a clearer explanation see documentation on Qt layouts.

要删除小部件,请将其父级设置为无,如下所示:

To remove widget, set its parent to None like this:

widget = QWidget()    
layout = QVBoxLayout()

btn = QPushButton("To be removed")
layout.addWidget(btn)
widget.setLayout(layout)

# later
btn.setParent(None)

这篇关于pyqt:如何从 QVBoxLayout 中删除元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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