QT:如何在布局中动态添加和删除小部件?

问题描述 投票:0回答:1
def add_ranges(self):
    self.ranges_layout = QtGui.QHBoxLayout()                            # setup the ranges_layout, it will be a child of all_ranges_layout
    self.min_label = QtGui.QLabel(self.normalization)                   # setup the min label
    self.max_label = QtGui.QLabel(self.normalization)                   # setup the max label
    self.min_lineEdit = QtGui.QLineEdit(self.normalization)             # setup the min lineEdit
    self.max_lineEdit = QtGui.QLineEdit(self.normalization)             # setup the max lineEdit
    self.all_ranges_layout.addLayout(self.ranges_layout)                # make ranges_layout a child of all_ranges_layout
    self.ranges_layout.addWidget(self.min_label)                        # apply the min label to the widget
    self.ranges_layout.addWidget(self.min_lineEdit)                     # apply the min lineEdit to the widget
    self.ranges_layout.addWidget(self.max_label)                        # apply the max label
    self.ranges_layout.addWidget(self.max_lineEdit)                     # apply the max lineEdit
    self.min_label.setText(_translate("MainWindow", "Min", None))       # set the text of the min label
    self.max_label.setText(_translate("MainWindow", "Max", None))       # set the text of the max label

    self.min_lineEdits.append(self.min_lineEdit)                        # set up an array of lineEdits
    self.max_lineEdits.append(self.max_lineEdit)                        #

def del_ranges(self):
    self.ranges_layout.removeWidget(self.min_label)                     # remove the min label to the widget
    self.ranges_layout.removeWidget(self.min_lineEdit)                  # remove the min lineEdit to the widget
    self.ranges_layout.removeWidget(self.max_label)                     # remove the max label
    self.ranges_layout.removeWidget(self.max_lineEdit)                  # remove the max lineEdit
    del self.min_lineEdits[-1]                                          # delete last element in list
    del self.max_lineEdits[-1]                                          # delete last element in list

单击“添加范围”,将出现两个新框供用户添加数据。

单击“删除范围”,除了按钮在视觉上向上移动外,没有任何反应。这告诉我该功能正在工作(有点)并且正在删除布局,但不是 lineEdits 和标签。

这是我正在设计的程序中的两个功能。我想通过按一下按钮来添加和删除小部件。我尝试使用 deleteLater() 但是,这个函数也陷入了同一个坑;实际上并没有删除 lineEdits 和标签。是删除前没有断开连接的问题吗?

qt user-interface pyqt qwidget
1个回答
2
投票

这是一种方法,可用于迭代遍历布局和

deleteLater()
特定布局中的每个小部件。在这种情况下
self.all_ranges_layout

代码

def del_ranges(self):
    to_delete = self.all_ranges_layout.takeAt(self.all_ranges_layout.count() - 1)
    if to_delete is not None:
        while to_delete.count():
            item = to_delete.takeAt(0)
            widget = item.widget()
            if widget is not None:
                widget.deleteLater()
            else:
                pass

简单的英语

get the layout item at n-1 index
if there are any objects to be deleted
    while the count of objects is not 0
        get the layout item at 0th index
        get the widget at this location
        if there is an object in this widget
            deleteLater this widget
© www.soinside.com 2019 - 2024. All rights reserved.