PyQt Combobox with QtGui.QDataWidgetMapper not saving key to database
-
I have a MySQL database with two 2 tables. The one table (movie) containing a foreign key to the other table (category).
I have setup the code and the record is inserted in the main table but with the foreign key field set to null. It does not insert the value from the combobox. A description is displayed from the category table and the category Id must be saved in the main table
Below is my code. The dialog is set up in Qt Desginer. Can somebody maybe spot the problem?
import sys
from moviewindow import *
from PyQt4 import QtSql, QtGuidef createConnection():
db = QtSql.QSqlDatabase.addDatabase('QMYSQL')
db.setHostName('localhost')
db.setDatabaseName('movies')
db.setUserName('root')
db.setPassword('mce')
db.open()
print (db.lastError().text())
return Trueclass MyForm(QtGui.QDialog):
def init(self, parent=None):
QtGui.QWidget.init(self, parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.model = QtSql.QSqlRelationalTableModel(self)
self.model.setTable('movie')
self.model.setRelation(self.model.fieldIndex("movie_category_id"),QtSql.QSqlRelation("movie_category","category_id","category_desc"))
self.model.select();
self.mapper = QtGui.QDataWidgetMapper(self)
self.mapper.setSubmitPolicy(QtGui.QDataWidgetMapper.ManualSubmit)
self.mapper.setModel(self.model)self.mapper.setItemDelegate(QtSql.QSqlRelationalDelegate(self)) self.mapper.addMapping(self.ui.lineEdit_Movietitle,self.model.fieldIndex("movie_title")) self.mapper.addMapping(self.ui.spinBox_Year,self.model.fieldIndex("movie_year")) self.mapper.addMapping(self.ui.lineEdit_Location,self.model.fieldIndex("movie_location")) relationModel = self.model.relationModel(4) self.ui.comboBox_Category.setModel(relationModel) self.ui.comboBox_Category.setModelColumn(relationModel.fieldIndex("category_desc")) self.mapper.addMapping(self.ui.comboBox_Category,self.model.fieldIndex("movie_category_id")) self.mapper.toFirst() QtCore.QObject.connect(self.ui.pushButton_First, QtCore.SIGNAL('clicked()'), self.goto_First) QtCore.QObject.connect(self.ui.pushButton_Last, QtCore.SIGNAL('clicked()'), self.goto_Last) QtCore.QObject.connect(self.ui.pushButton_Prior, QtCore.SIGNAL('clicked()'), self.goto_Prior) QtCore.QObject.connect(self.ui.pushButton_Next, QtCore.SIGNAL('clicked()'), self.goto_Next) QtCore.QObject.connect(self.ui.pushButton_Quit, QtCore.SIGNAL('clicked()'), self.accept) QtCore.QObject.connect(self.ui.pushButton_Add, QtCore.SIGNAL('clicked()'), self.add_Record) QtCore.QObject.connect(self.ui.pushButton_Delete, QtCore.SIGNAL('clicked()'), self.delete_Record) QtCore.QObject.connect(self.ui.pushButton_Save, QtCore.SIGNAL('clicked()'), self.save_Record) self.ui.comboBox_Category.currentIndexChanged.connect(lambda: self.mapper.itemDelegate().commitData.emit(self.ui.comboBox_Category)) def save_Record(self): self.mapper.submit() self.model.submitAll() def delete_Record(self): row = self.mapper.currentIndex() self.model.removeRow(row) self.model.submitAll() if row + 1 >= self.model.rowCount(): row = self.model.rowCount()-1 self.mapper.setCurrentIndex(row) def add_Record(self): row = self.model.rowCount() self.mapper.submit() self.model.submitAll() self.model.insertRow(row) self.mapper.setCurrentIndex(row) self.ui.comboBox_Category.setCurrentIndex(self.ui.comboBox_Category.findText("Unresolved")) self.ui.lineEdit_Movietitle.setFocus() def accept(self): self.mapper.submit() self.model.submitAll() QtGui.QDialog.accept(self) def goto_First(self): self.mapper.toFirst() def goto_Last(self): self.mapper.toLast() def goto_Prior(self): self.mapper.toPrevious() def goto_Next(self): self.mapper.toNext()
if name == "main":
app = QtGui.QApplication(sys.argv)
if not createConnection():
sys.exit(1)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())