【Python PyQt5 中combobox用clear()崩溃的问题及解决】

Python PyQt5 中combobox用clear()崩溃的问题及解决

今天使用QCombobox控件时,使用clear()清空下拉列表时,程序崩了,在本站搜索了好久,都没有很好解决,只是看到要用disconnect方法解除函数的绑定,但没有实际解决我的问题。经过思考和一步一步调试,解决了问题,直接上程序
修改前

    def __init__(self):
        super(***********, self).__init__()
        super().__init__()
        self.setupUi(self)
		self.plan_num.currentTextChanged.connect(lambda:self.show_deploy(self.plan_num.currentText()))
		
    def plan_change(self, text):
       self.plan_num.clear()
       self.plan_num.addItem("请选择")
       result = service.query('select plan_num from plan_sheet where plan_model=%s', text)
       for i in result:
           self.plan_num.addItem(i[0])

解决办法
修改 plan_change 函数
如下

    def plan_change(self, text):
        self.plan_num.currentTextChanged.disconnect()
        self.plan_num.clear()
        self.plan_num.addItem("请选择")
        result = service.query('select plan_num from plan_sheet where plan_model=%s', text)
        for i in result:
            self.plan_num.addItem(i[0])
        self.plan_num.currentTextChanged.connect(lambda: self.show_deploy(self.plan_num.currentText()))

对比一下该函数修改前和修改后
在函数开始先解除槽函数的绑定

	self.plan_num.currentTextChanged.disconnect()

函数末尾再将槽函数重新连接即可

	self.plan_num.currentTextChanged.connect(lambda: self.show_deploy(self.plan_num.currentText()))