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)
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
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):
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__":
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):
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):
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):