删除 on_change 函数中的父布局(SIGSEGV 错误)

Deleting a parent layout in on_change function (SIGSEGV Error)

我有一个 QComboBox,cbo_box,嵌套在一个框架中,self.ui.frameFilterControls,其中:cbo_box.currentTextChanged.connect(self.choice_changed)。在 self.choice_changed() 中,我调用了一个函数来清除框架布局,包括 cbo_box,然后使用适当的控件重新生成布局(包括新的 cbo_box):

layout = self.ui.frameFilterControls.layout()
if layout is not None:
    #clear layout
    for i in reversed(range(layout.count())):
        layout.itemAt(i).widget().setParent(None)

我遇到的问题是当 self.choice_changed() returns 时,它抛出一个 SIGSEGV 错误:

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

我猜是因为它无处 return,但我不确定。我想知道是否有可能不调用删除和重新创建 cbo_boxon_change 中的控件的函数,如果我可以安排它在下一次表单更新时进行评估,或者类似的东西行数?


编辑:

我制作了一个重现该行为的最小示例。

from PySide6 import QtCore, QtWidgets
from PySide6.QtWidgets import (QApplication, QListWidgetItem, QGridLayout, QVBoxLayout, QLineEdit, QComboBox)
import sys

#from output of designer -> simple main form with a frame for a starting point
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(381, 357)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName("verticalLayout")
        self.frame = QtWidgets.QFrame(self.centralwidget)
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.frame)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.pushButton = QtWidgets.QPushButton(self.frame)
        self.pushButton.setObjectName("pushButton")
        self.verticalLayout_2.addWidget(self.pushButton)
        self.verticalLayout.addWidget(self.frame)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 381, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))


class MainGUI(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainGUI, self).__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.set_frame_controls()

    def set_frame_controls(self):
        layout = self.ui.frame.layout()
        if layout is not None:
            #clear layout
            for i in reversed(range(layout.count())):
                layout.itemAt(i).widget().setParent(None)

        cbo_box = QComboBox()
        cbo_box.addItem("test 1")
        cbo_box.addItem("test 2")
        cbo_box.currentTextChanged.connect(self.choice_changed)
        layout.addWidget(cbo_box)

    def choice_changed(self):
        print("choice_changed")
        cbo_box = self.sender()
        self.set_frame_controls()
        print("end choice changed")

def main():
    app = QApplication(sys.argv)
    form = MainGUI()
    form.show()
    app.exit(app.exec_())

if __name__ == '__main__':
    main()

当我 运行 并将组合框从“测试 1”切换到“测试 2”时,上述程序的控制台输出如下:

choice_changed
end choice changed

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

我想要的行为是完全重新生成框架,没有错误。谢谢!

如果您想从布局中删除小部件,仅设置一个空父级是不够的,因为这不会将其从布局具有的内部列表中删除,而只会删除 对象,从而导致SIGSEGV。相反,您必须使用 deleteLater(),如果它确保正确消除元素(例如布局的内部列表)。

def set_frame_controls(self):
    layout = self.ui.frame.layout()
    if layout is not None:
        for i in reversed(range(layout.count())):
            widget = layout.itemAt(i).widget()
            if widget is not None:
                widget.deleteLater()

    cbo_box = QComboBox()
    cbo_box.addItem("test 1")
    cbo_box.addItem("test 2")
    cbo_box.currentTextChanged.connect(self.choice_changed)
    layout.addWidget(cbo_box)