1

我有以下代码:

#####   Slots for checkboxes (enable/disabling)
QtCore.QObject.connect(self.checkBox_3, QtCore.SIGNAL(_fromUtf8("stateChanged(int)")), self.checkBox_3.setOn)
QtCore.QObject.connect(self.checkBox_3, QtCore.SIGNAL(_fromUtf8("stateChanged(int)")), self.dateEdit.setEnabled)
QtCore.QObject.connect(self.checkBox_3, QtCore.SIGNAL(_fromUtf8("stateChanged(int)")), self.dateEdit_2.setEnabled)
QtCore.QObject.connect(self.checkBox, QtCore.SIGNAL(_fromUtf8("stateChanged(int)")), self.lineEdit.setEnabled)
QtCore.QObject.connect(self.checkBox_2, QtCore.SIGNAL(_fromUtf8("stateChanged(int)")), self.lineEdit_2.setEnabled)
QtCore.QObject.connect(self.checkBox_4, QtCore.SIGNAL(_fromUtf8("stateChanged(int)")), self.lineEdit_3.setEnabled)
#####

##### Slots for checkboxes/lineEdits (filling dict for database fields and values)
QtCore.QObject.connect(self.checkBox, QtCore.SIGNAL(_fromUtf8("stateChanged(int)")), lambda: self.interfaceCodesConstructor.setFilterDict(self,"name",self.lineEdit.text()))
QtCore.QObject.connect(self.checkBox_2, QtCore.SIGNAL(_fromUtf8("stateChanged(int)")), lambda: self.interfaceCodesConstructor.setFilterDict(self,"bought_price",persianToInteger(unicode(self.lineEdit_2.text()))))
QtCore.QObject.connect(self.checkBox_3, QtCore.SIGNAL(_fromUtf8("stateChanged(int)")), lambda: self.interfaceCodesConstructor.setFilterDict(self,"bought_date",self.dateEdit.text())) ##date        
QtCore.QObject.connect(self.checkBox_4, QtCore.SIGNAL(_fromUtf8("stateChanged(int)")), lambda: self.interfaceCodesConstructor.setFilterDict(self,"stock",persianToInteger(unicode(self.lineEdit_3.text()))))        


QtCore.QObject.connect(self.lineEdit, QtCore.SIGNAL(_fromUtf8("editingFinished()")), lambda: self.interfaceCodesConstructor.responseToRequestForData(self))
QtCore.QObject.connect(self.lineEdit_2, QtCore.SIGNAL(_fromUtf8("editingFinished()")), lambda: self.interfaceCodesConstructor.responseToRequestForData(self))
QtCore.QObject.connect(self.dateEdit, QtCore.SIGNAL(_fromUtf8("editingFinished()")), lambda: self.interfaceCodesConstructor.responseToRequestForData(self))         
QtCore.QObject.connect(self.lineEdit_3, QtCore.SIGNAL(_fromUtf8("editingFinished()")), lambda: self.interfaceCodesConstructor.responseToRequestForData(self))        



#### pressing seach key and start search action
QtCore.QObject.connect(self.lineEdit, QtCore.SIGNAL(_fromUtf8("returnPressed()")), lambda: self.interfaceCodesConstructor.responseToRequestForData(self))
QtCore.QObject.connect(self.pushButtonSearch, QtCore.SIGNAL(_fromUtf8("clicked()")), lambda: self.interfaceCodesConstructor.responseToRequestForData(self))
#######



##    enable/disabling "price date" checkbox
QtCore.QObject.connect(self.checkBox_3, QtCore.SIGNAL(_fromUtf8("toggled(bool)")), self.dateEdit.setEnabled)
QtCore.QObject.connect(self.checkBox_3, QtCore.SIGNAL(_fromUtf8("toggled(bool)")), self.dateEdit_2.setEnabled)

然后我SLOT在另一个文件中有以下内容:

def setFilterDict(self,obj,field,lineEditContent):
    self.objectSearchMaterials = obj

    if field not in self.filterNameDict.keys():# and self.filterNameDict.values() is not None:
        self.filterNameDict.update({field:lineEditContent})
    else:
        del self.filterNameDict[field] 

我的问题是,当我单击时checkboxes,我用文本填充字典:self.filterNameDict,但是当我单击多次时,它无法处理给定的字典。我如何创建一个动态字典来处理每个checkboxes?这意味着当我单击checkbox添加字段名称和lineEdit text()字典时我需要,当我删除它时,自动从字典中删除?

4

1 回答 1

1

您需要使用self.filterNameDict.pop(field, None)而不是del self.filterNameDict[field].

帮助:

>>> help(dict.pop)
Help on method_descriptor:

pop(...)
    D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
    If key is not found, d is returned if given, otherwise KeyError is raised
于 2013-09-14T05:52:09.993 回答