我如何填充和删除小部件的动态字典



我有以下代码:

#####   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时,我需要将fieald和lineEdit text()的名称添加到字典中,当我删除它时,会自动从字典中删除?

您需要

使用 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

相关内容

  • 没有找到相关文章

最新更新