Inthu
Inthu

Reputation: 1029

pyqt relational table not updating when foreign key is updated

I have a window with a splitter, inside the two frames there are two different tables, and the product type has a relation with product and the GUI runs and works fine but when I change a name on product type it doesn't update on the product table, why is that? but once i restart the changes are made? is there a way to force pyqt model to check the database for changes?

def initUI(self):

        self.connectDb()

        mainlayout = QtGui.QVBoxLayout(self)
        self.mode = QtGui.QAbstractItemView.SingleSelection

        self.topleft = QtGui.QFrame(self)
        self.topleft.setFrameShape(QtGui.QFrame.StyledPanel)
        self.topleftfill()
        self.topright = QtGui.QFrame(self)
        self.topright.setFrameShape(QtGui.QFrame.StyledPanel)
        self.toprightfill()

        splittertop = QtGui.QSplitter(QtCore.Qt.Horizontal)
        splittertop.addWidget(self.topleft)
        splittertop.addWidget(self.topright)
        mainlayout.addWidget(splittertop)
        self.setLayout(mainlayout)

def topleftfill(self):
    layout = QtGui.QVBoxLayout(self)
    model = QtSql.QSqlRelationalTableModel()
    model.setTable("Product")
    model.setRelation(6, QtSql.QSqlRelation("ProductType","ProductTypeID","Type"))
    model.select()
    model.setHeaderData(0, QtCore.Qt.Horizontal, "Product ID")
    model.setHeaderData(1, QtCore.Qt.Horizontal, "Product Name")
    model.setHeaderData(2, QtCore.Qt.Horizontal, "In Stock ")
    model.setHeaderData(3, QtCore.Qt.Horizontal, "Expiry Date")
    model.setHeaderData(4, QtCore.Qt.Horizontal, "Stock Alert")
    model.setHeaderData(5, QtCore.Qt.Horizontal, "Price")
    model.setHeaderData(6, QtCore.Qt.Horizontal, "Product Type")
    edit = QtSql.QSqlTableModel.OnFieldChange
    model.setEditStrategy(edit)
    view = QtGui.QTableView()
    view.setModel(model)
    view.setSelectionMode(self.mode)
    view.setItemDelegate(QtSql.QSqlRelationalDelegate(view))
    layout.addWidget(view)
    self.topleft.setLayout(layout)

def toprightfill(self):
    layout = QtGui.QVBoxLayout(self)
    model = QtSql.QSqlRelationalTableModel()
    model.setTable("ProductType")
    model.select()
    model.setHeaderData(0, QtCore.Qt.Horizontal, "Product Type ID")
    model.setHeaderData(1, QtCore.Qt.Horizontal, "Product Type")
    edit = QtSql.QSqlTableModel.OnFieldChange
    model.setEditStrategy(edit)
    view = QtGui.QTableView()
    view.setModel(model)
    view.setSelectionMode(self.mode)
    layout.addWidget(view)
    self.topright.setLayout(layout)


def connectDb(self):
    name = 'PostOffice.db'
    initDb.setup_database(name)
    db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
    db.setDatabaseName(name)
    if not db.open():
        print("Cannot Connect")
        return False    

Upvotes: 0

Views: 1026

Answers (1)

Inthu
Inthu

Reputation: 1029

The problem is once your button has been pressed to edit changes if your using the OnManualSubmit edit strategy, you need make sure you repopulate your table by calling the model.select() function otherwise the program will use your old model until you restart the program

Upvotes: 3

Related Questions