I have a PYQT form that I load from a sqlite table. The QLineEdits load fine and the navigation buttons let me move through the table but I am unable to update. The code that loads the form is:
class MyForm(QtGui.QWidget):
recno=0
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.model=QtSql.QSqlTableModel(self)
self.model.setTable("Person")
self.model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
self.model.select()
while self.model.canFetchMore():
self.model.fetchMore() self.record=self.model.record(0)
self.ui.txtState.setText(self.record.value("State"))
self.ui.txtLastName.setText(self.record.value("LastName"))
self.ui.txtFirstName.setText(self.record.value("FirstName"))
self.ui.txtZip.setText(self.record.value("ZipCode"))
self.ui.txtNotes.setText(self.record.value("Notes"))
self.ui.txtAddress.setText(self.record.value("Address"))
self.ui.txtCity.setText(self.record.value("City"))
self.ui.txtEmail.setText(self.record.value("email"))
self.ui.txtEmail2.setText(self.record.value("email2"))
self.ui.txtCompany.setText(self.record.value("Company"))
self.ui.txtPhone.setText(self.record.value("Phone"))
self.ui.txtCell.setText(self.record.value("Phone2"))
self.ui.txtFax.setText(self.record.value("Fax"))
QtCore.QObject.connect(self.ui.cmdFirst, QtCore.SIGNAL('clicked()'), self.dispFirst)
QtCore.QObject.connect(self.ui.cmdPrev, QtCore.SIGNAL('clicked()'), self.dispPrevious)
QtCore.QObject.connect(self.ui.cmdLast, QtCore.SIGNAL('clicked()'), self.dispLast)
QtCore.QObject.connect(self.ui.cmdNext, QtCore.SIGNAL('clicked()'), self.dispNext)
QtCore.QObject.connect(self.ui.cmdExit, QtCore.SIGNAL('clicked()'), self.done)
QtCore.QObject.connect(self.ui.cmdSave, QtCore.SIGNAL('clicked()'), self.UpdateRecords)
The code to move next and update is:
def dispNext(self):
MyForm.recno+=1
if MyForm.recno >self.model.rowCount()-1:
MyForm.recno=0
self.record=self.model.record(MyForm.recno)
self.ui.txtState.setText(self.record.value("State"))
self.ui.txtLastName.setText(self.record.value("LastName"))
self.ui.txtFirstName.setText(self.record.value("FirstName"))
self.ui.txtZip.setText(self.record.value("ZipCode"))
self.ui.txtNotes.setText(self.record.value("Notes"))
self.ui.txtAddress.setText(self.record.value("Address"))
self.ui.txtCity.setText(self.record.value("City"))
self.ui.txtEmail.setText(self.record.value("email"))
self.ui.txtEmail2.setText(self.record.value("email2"))
self.ui.txtCompany.setText(self.record.value("Company"))
self.ui.txtPhone.setText(self.record.value("Phone"))
self.ui.txtCell.setText(self.record.value("Phone2"))
self.ui.txtFax.setText(self.record.value("Fax"))
def UpdateRecords(self):
self.record=self.model.record(MyForm.recno)
self.field = self.record.field("email2", 0)
self.field.setValue=(self.ui.txtEmail2.text)
self.model.submitAll()
I don't get any error message but the table doesn't update. I have used a print statement with debug and the self.ui.txtEmail2.text is holding the changed text value. I can update the table using Python and an Update query and I will go that way if I have to, but I would really like to use QtSql if I can just get it to work. Any help will be greatly appreciated. Thanks
Aucun commentaire:
Enregistrer un commentaire