pyqt:如何从 QVBoxLayout 中删除元素?
我想要一个多颜色选择小部件。我这样做的方式是有一个“+”按钮和一个最初为空的 vbox。当按下 + 时,它会向包含“-”按钮和 3 个旋转框的 vbox 添加一个 QHBoxLayout。当按下“-”按钮时,我希望该行消失,并且所有内容都恢复到添加该行之前的样子。我当前拥有的代码是:
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)
添加小部件效果很好。然而,删除它们会导致看起来非常混乱的事情,其中行实际上并未被删除,但间距却全部混乱了。
我做错了什么?
编辑:文档说,对于 removeWidget
:
在此调用之后,调用者有责任为小部件提供合理的几何形状或将小部件放回到布局中。
我该怎么做? (我来自 GTK 背景...)
编辑 2:我什至跟踪行并调用 takeAt
函数将其删除,但它仍然变得混乱。什么给?看起来布局已被删除,但没有任何小部件...
编辑3:这也不起作用,只是以类似的方式搞乱了事情:
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)
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.
What am I doing wrong?
EDIT: The docs say, for 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.
How do I do that? (I come from a GTK background...)
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...
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试从父窗口小部件中删除,而不是从布局中删除。
QLayout 不是父级,正在布局的小部件的父级实际上是布局的父级。有关更多信息和更清晰的说明,请参阅有关 Qt 布局的文档< /a>.
要删除小部件,请将其父项设置为 None,如下所示:
Try removing from parent widget, not the layout.
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: