0

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    
Inthu
  • 1,009
  • 1
  • 8
  • 16
  • I never used a `QSqlRelationalTableModel` but maybe you should emit the `dataChanged` signal (http://qt-project.org/doc/qt-4.8/qabstractitemmodel.html#dataChanged) when data is changed in your model. This is how it works for table models for example. – Plouff Jan 08 '15 at 17:34
  • I'm kind of confused, do you know how to write that in python? or could you give me an example? – Inthu Jan 08 '15 at 20:00
  • I deleted my answer below since it is not working. I am pretty sure, this has to do with the `dataChanged` signal. Try to find something regarding this and your `QSqlRelationalTableModel`. Good luck! – Plouff Jan 09 '15 at 13:08
  • I have found out why :) thanks – Inthu Jan 10 '15 at 18:42
  • Glad to read that you found an answer :)! – Plouff Jan 10 '15 at 21:03

1 Answers1

3

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

Inthu
  • 1,009
  • 1
  • 8
  • 16