简体   繁体   中英

Recursion error with if statement when changing items in PYQT5 table python

I have a table in pyqt5 and when I modify a cell I call a method(changeIcon), inside this method I use an if statement to check which column has been changed and based on the column I change some items, everytime I modify the column 3 and 4 it returns me this error:

Traceback (most recent call last):
File "c:\FOLDER\App.py", line 291, in Change_method
RecursionError

This is the code :

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

I find out why it returned that error , everytime I change the item of the column 3 or 4 it gives a signal and calls this method, so when I try to change the column 4 automatically the column 3 changes based on column 4, but when the columns 3 changes the column 4 changes as well, this for infinte times. The solution was to block signals before setting the item and to enable them after I set the item :

self.tabella_registrazioni.blockSignals(True)
self.tabella_registrazioni.setItem(row, 3, QtWidgets.QTableWidgetItem(str(ccode)))
self.tabella_registrazioni.blockSignals(False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM