Hi,
I have a QTableWidget, in which I create dynamically some QComboBox. I simplified the code so things might be missing but the important part is in there, it looks like this :
def testPrint(self, index):
print index
def itemComboBox(self, array, curIndex):
comboBox.addItems(array)
comboBox.setCurrentIndex(curIndex)
return comboBox
def createTable(self):
tw = self.ui.tableWidget
version = [ 'v01', 'v02', 'v03' ]
cb = []
for i range(0,len(version)):
cb.append(self.itemComboBox(version,'v01'))
tw.setCellWidget(i, 0, cb[i])
def testPrint(self, index):
print index
def itemComboBox(self, array, curIndex):
comboBox = QtGui.QComboBox(self)
comboBox.addItems(array)
comboBox.setCurrentIndex(curIndex)
return comboBox
def createTable(self):
tw = self.ui.tableWidget
version = [ 'v01', 'v02', 'v03' ]
cb = []
for i range(0,len(version)):
cb.append(self.itemComboBox(version,'v01'))
tw.setCellWidget(i, 0, cb[i])
To copy to clipboard, switch view to plain text mode
So no problem with that, it creates what I need to see and when I go through each rows I get the values I want. However, I'm trying to run a function when the comboBox value get changed. I first tried that :
def createTable(self):
tw = self.ui.tableWidget
version = [ 'v01', 'v02', 'v03' ]
cb = []
for i range(0,len(version)):
cb.append(self.itemComboBox(version,'v01'))
cb[i].currentIndexChanged.connect( lambda: self.testPrint(i) ) ###LINE###
tw.setCellWidget(i, 0, cb[i])
def createTable(self):
tw = self.ui.tableWidget
version = [ 'v01', 'v02', 'v03' ]
cb = []
for i range(0,len(version)):
cb.append(self.itemComboBox(version,'v01'))
cb[i].currentIndexChanged.connect( lambda: self.testPrint(i) ) ###LINE###
tw.setCellWidget(i, 0, cb[i])
To copy to clipboard, switch view to plain text mode
But with this, for some reason, each combobox returns the value from the last in the list, seems like I keep overriding the event in the loop but I don't see how since I use a list with index. So as a test I quickly tried something simple outside of the loop after getting rid of the line ###LINE###
for k in range(0,len(version)) :
cb[k].currentIndexChanged.connect( lambda: self.testPrint(k) )
for k in range(0,len(version)) :
cb[k].currentIndexChanged.connect( lambda: self.testPrint(k) )
To copy to clipboard, switch view to plain text mode
Which still doesn't work, then because I was really suspicious I tried that instead:
cb[0].currentIndexChanged.connect( lambda: self.testPrint(0) )
cb[1].currentIndexChanged.connect( lambda: self.testPrint(1) )
cb[2].currentIndexChanged.connect( lambda: self.testPrint(2) )
cb[0].currentIndexChanged.connect( lambda: self.testPrint(0) )
cb[1].currentIndexChanged.connect( lambda: self.testPrint(1) )
cb[2].currentIndexChanged.connect( lambda: self.testPrint(2) )
To copy to clipboard, switch view to plain text mode
And that works ! I really don't get why going through a loop would not work the same way.
Anyone can save me on this one ?
Cheers
Chris
Bookmarks