0

I need automatically trigger refresh QTableView with setModel method in parent window after close child window. Child window insert data into sqlite database. Fragment of code from child window:

class addClientWindow(QtWidgets.QWidget):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)
        self.initUI()
    def initUI(self):

    def addButtonHandle():
        query = QSqlQuery()
        query.exec("insert into clients values(NULL,'"+name.text()+"','"+surname.text()+"')")
        self.close()

Fragment of code parent window:

class ClientsWindow(QtWidgets.QWidget):
    def __init__(self):
        QtWidgets.QWidget.__init__(self, parent=None)
        self.initUI()

    def addClientHandle(self):
        self.window = addClientWindow()     
        self.window.show()

    def triggerTableUpdate(self):
        self.mainTable.setModel(self.setClientModel())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • child has `self.parent` to access parent window - so you can use it direectly before `close()` to update parent window - `self.parent.triggerTableUpdate()`. After `close()` you may not have access to `self` and `self.parent`. – furas Jul 29 '21 at 14:48

1 Answers1

2

If you want a window that performs some action and after closing then perform another action then you must use QDialog. On the other hand, do not concatenate a SQL query since your code will be susceptible to SQL Injection attacks.

class addClientWindow(QtWidgets.QDialog):
    def __init__(self, parent=None):
        QDialog.QWidget.__init__(self, parent)
        self.initUI()

    def addButtonHandle(self):
        query = QSqlQuery()
        query.prepare("INSERT INTO clients VALUES(?, ?)")
        query.addBindValue(name.text())
        query.addBindValue(surname.text())
        self.close()
def addClientHandle(self):
    self.window = addClientWindow()     
    self.window.exec_()
    print("update table here")
eyllanesc
  • 235,170
  • 19
  • 170
  • 241