Issue
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())
Solution
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")
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.