From: Drew Fisher Date: Mon, 13 Jul 2009 01:01:32 +0000 (-0700) Subject: Make opening a new database work correctly in EditPerson. X-Git-Url: https://git.zarvox.org/shortlog/month?a=commitdiff_plain;h=31bfa115addd7c3d0fdb8fe4dde68347b6aa6117;p=wp3.git Make opening a new database work correctly in EditPerson. This patch fixes the bug where you'd open a new database, edit some data in it, hit save, and it would update that row in the originally-opened database. Also thrown in is some refactoring work in EditPerson to maximize code reuse. --- diff --git a/editperson.py b/editperson.py index 9422a85..959bce9 100644 --- a/editperson.py +++ b/editperson.py @@ -7,12 +7,7 @@ class EditPerson(QWidget): QWidget.__init__(self,parent) self.db = db - self.model = QSqlTableModel(self, self.db) - self.model.setEditStrategy(QSqlTableModel.OnManualSubmit) - self.model.setTable("people") - self.model.select() - self.model.setHeaderData(self.model.fieldIndex("forename"), Qt.Horizontal, QVariant("First Name")) - self.model.setHeaderData(self.model.fieldIndex("surname"), Qt.Horizontal, QVariant("Last Name")) + self.setupModel() self.tableview = QTableView(self) self.tableview.setSelectionBehavior(QAbstractItemView.SelectRows) @@ -56,6 +51,13 @@ class EditPerson(QWidget): self.show() + def setupModel(self): + self.model = QSqlTableModel(self, self.db) + self.model.setEditStrategy(QSqlTableModel.OnManualSubmit) + self.model.setTable("people") + self.model.select() + self.model.setHeaderData(self.model.fieldIndex("forename"), Qt.Horizontal, QVariant("First Name")) + self.model.setHeaderData(self.model.fieldIndex("surname"), Qt.Horizontal, QVariant("Last Name")) def setupForm(self): self.current_record = QSqlRecord() self.current_row = -1 @@ -239,21 +241,7 @@ class EditPerson(QWidget): print "Closing" self.model.revertAll() # Wipe contents of all lineedits, destroy current_record and current_index - self.search_bar.clear() - self.current_record = QSqlRecord() - self.current_index = -1 - self.surname.clear() - self.firstname.clear() - self.phone.clear() - self.email.clear() - self.major.clear() - self.netid.clear() - self.year.setCurrentIndex(0) - self.month.setCurrentIndex(0) - self.day.setCurrentIndex(0) - self.dorm.setCurrentIndex(0) - self.room.clear() - self.photo.setText("No record selected") + self.wipeFields() self.model.select() self.emit(SIGNAL("done()")) def savePressed(self): @@ -300,6 +288,34 @@ class EditPerson(QWidget): self.pb_reset.setEnabled(True) self.pb_save.setEnabled(True) self.pb_saveclose.setEnabled(True) + def wipeFields(self): + self.search_bar.clear() + self.current_record = QSqlRecord() + self.current_index = -1 + self.surname.clear() + self.firstname.clear() + self.phone.clear() + self.email.clear() + self.major.clear() + self.netid.clear() + self.year.setCurrentIndex(0) + self.month.setCurrentIndex(0) + self.day.setCurrentIndex(0) + self.dorm.setCurrentIndex(0) + self.room.clear() + self.photo.setText("No record selected") + + def updateDB(self, newdbname): + print "updateDB called",newdbname + self.db.close() + self.db.setDatabaseName(newdbname) + self.db.open() + self.setupModel() + self.tableview.setModel(self.model) + QObject.connect(self.tableview.selectionModel(), SIGNAL("selectionChanged(QItemSelection, QItemSelection)"), self.selectionChanged ) + self.wipeFields() + self.updateTable(QString()) + if __name__ == "__main__": diff --git a/mainapp.py b/mainapp.py index 599cd3d..14ee865 100644 --- a/mainapp.py +++ b/mainapp.py @@ -43,6 +43,7 @@ class MainApp (QMainWindow): QtCore.QObject.connect( self.fileQuitAction, QtCore.SIGNAL("triggered()"), self.quit) QtCore.QObject.connect( self.chooseaction, QtCore.SIGNAL("editPerson()"), self.editPersonSlot) QtCore.QObject.connect( self.editperson, QtCore.SIGNAL("done()"), self.returnToMainMenu) + QtCore.QObject.connect( self, QtCore.SIGNAL("databaseChanged(QString)"), self.editperson.updateDB) def createMenus(self): @@ -65,6 +66,7 @@ class MainApp (QMainWindow): q = QSqlQuery(self.db) q.exec_(MainApp.schema) self.statusBar().showMessage("Closed the existing database, created " + fileName) + self.emit(QtCore.SIGNAL("databaseChanged(QString)"),self.db.databaseName()) else: self.statusBar().showMessage("Cancelled creating new database") def openFile(self): @@ -74,6 +76,7 @@ class MainApp (QMainWindow): self.db.setDatabaseName(fileName) self.db.open() self.statusBar().showMessage("Successfully loaded database " + fileName) + self.emit(QtCore.SIGNAL("databaseChanged(QString)"),self.db.databaseName()) else: self.statusBar().showMessage("Canceled loading database") def mergeWizard(self):