我在pyqt5中有一个表,当我修改一个单元格时,我调用一个方法(changeIcon),在这个方法中,我使用if语句来检查哪一列已被更改,并根据列我更改了一些项目,每次我修改列3和4它返回我这个错误:
Traceback (most recent call last):
File "c:FOLDERApp.py", line 291, in Change_method
RecursionError
这是代码:
def tabledata(self)
.....
self.tabella_registrazioni.itemChanged.connect(self.changeIcon)
.....
def changeIcon(self, item):
row = item.row()
col = item.column()
custcode = self.tabella_registrazioni.item(row, 3).text()
custname = self.tabella_registrazioni.item(row, 4).text()
if col == 3 :
if not custcode.isspace() and custcode != " " and custcode != "":
cname = d.execute("SELECT 1 FROM CODES WHERE ANCODICE = ?", (custcode)).fetchone()[0]
if cname != None:
self.tabella_registrazioni.setItem(row, 4, QtWidgets.QTableWidgetItem(str(cname)))
if col == 4 :
if not custname.isspace() and custname != " " and custname != "":
ccode = d.execute("SELECT 2 FROM CODES WHERE ANDESCRI = ?", (custname)).fetchone()[0]
if ccode != None:
self.tabella_registrazioni.setItem(row, 3, QtWidgets.QTableWidgetItem(str(ccode)))
我发现为什么它返回这个错误,每次我改变列3或4的项目它给出一个信号并调用这个方法,所以当我试图自动改变列4的列3基于列4的变化,但当列3改变列4的变化,这是无限次。解决方案是在设置项之前阻止信号,并在设置项之后启用它们:
self.tabella_registrazioni.blockSignals(True)
self.tabella_registrazioni.setItem(row, 3, QtWidgets.QTableWidgetItem(str(ccode)))
self.tabella_registrazioni.blockSignals(False)