[PyQt] QSqlTableModel/QTableView not updating fields

hkx gitawa at gmail.com
Wed Sep 9 10:02:04 BST 2009


The following code allows me to click on each field and change it, 
however when clicking on another row,  a) the changes are not submitted 
as expected, the screen just refreshes and the fields are cleared b) 
need several clicks on another row before changes are 'submitted' and 
screen refreshed even with setEditStrategy set to QSqlTableModel.OnRowChange

Why aren't the cells being written to the database and why are multiple 
clicks on cells in another row required before changes are submitted.

I've also noticed this behaviour in sample code from 'Rapid GUI 
Programming with Python and Qt'. This is running on Ubuntu jaunty 64bit.

Thanks

class MainForm(QDialog):

    def __init__(self):
        super(MainForm, self).__init__()

        self.assetModel = QSqlTableModel(self)
        self.assetModel.setEditStrategy(QSqlTableModel.OnRowChange)      
        self.assetModel.setTable("testing")
               
        self.assetModel.select()

        self.assetView = QTableView()
        self.assetView.setModel(self.assetModel)
        self.assetView.setSelectionMode(QTableView.SingleSelection)
        self.assetView.setSelectionBehavior(QTableView.SelectRows)
       
        dataLayout = QVBoxLayout()    
        dataLayout.addWidget(self.assetView, 1)
        layout = QHBoxLayout()
        layout.addLayout(dataLayout, 1)
        self.setLayout(layout)

        self.setWindowTitle("Asset Manager")



if __name__ == '__main__':
 
    app = QApplication(sys.argv)
  
    filename = os.path.join(os.path.dirname(__file__), "test.db")
    create = not QFile.exists(filename)
    db = QSqlDatabase.addDatabase("QSQLITE")
    db.setDatabaseName(filename)
    if not db.open():
        QMessageBox.warning(None, "Asset Manager",
            QString("Database Error: %1").arg(db.lastError().text()))
        sys.exit(1)

   
    print "Creating table..."
    query = QSqlQuery()
    r = query.exec_('drop table testing')
    r = query.exec_("""CREATE TABLE testing (
                id INTEGER,
                type VARCHAR(20),
                rights VARCHAR(20),
                expirydate VARCHAR(20),
                insid INTEGER)""" )
               
    query.exec_("INSERT INTO testing (id, insid) "
                "VALUES (1, 2)")
    query.exec_("INSERT INTO testing (id, insid) "
                "VALUES (3, 4)")

    m = MainForm()
    m.show()
   
    app.exec_()



More information about the PyQt mailing list