Question

I have the following code:

#####   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)

Then i have the following SLOT in another file:

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] 

My question is , When i click on checkboxes, i fill a dict with a text : self.filterNameDict , But when i click for more of once, it can't handle the given dict.How i create a dynamically dict for handle every checkboxes? It means i need to when i click to a checkbox add name of fieald and lineEdit text() to dict and When i remove it , automatically remove from dict?

Was it helpful?

Solution

You need to use self.filterNameDict.pop(field, None) instead of del self.filterNameDict[field].

Help:

>>> 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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top