Hi Everyone,

I am using QSqlRelationalTableModel. I have subclass the model. I am using editStrategy onManualSubmit.
This is the code I have so far. What is not working is the default values. I am using primeInsert so that dataChanged is not emitted each time insertRows is called. Why is setValue not working? Is this almost right?
Qt Code:
  1. class AppField(QtGui.QDialog, ui_app_field.Ui_AppFieldDialog):
  2. def __init__(self, database = None, table = None, parent = None):
  3. super(AppField, self).__init__(parent)
  4. self._table = table
  5. self._dbfname = database
  6. self.setupUi(self)
  7. # subclassed sqlrelationaltablemodel.
  8. self.model = AppModel(table = self._table, parent = self)
  9. self.app_field_tableView.setModel(self.model)
  10.  
  11. self.add_row_pushButton.clicked.connect(self.add_row)
  12. self.model.primeInsert.connect(self.default_val)
  13. self.model.dataChanged.connect(self.submit)
  14.  
  15. def submit(self, index):
  16. print "help"
  17. id = self.model.data(self.model.index(index.row(), ID))
  18. app_alias = self.model.data(self.model.index(index.row(), ALIAS))
  19. app_field = self.model.data(self.model.index(index.row(), APP_FIELD))
  20. app_def = self.model.data(self.model.index(index.row(), APP_DEF))
  21. if self.model.isDirty(self.model.index(index.row(), ALIAS)) or app_alias != "Event Alias Name":
  22. if self.model.isDirty(self.model.index(index.row(), APP_FIELD)) or app_field != "Event name":
  23. if self.model.isDirty(self.model.index(index.row(), APP_DEF)) or app_def != "Event Definition":
  24. self.model.submitAll()
  25. if self.model.lastError().isValid():
  26. if self.model.lastError().text() == "column app_field_alias is not unique Unable to fetch row":
  27. self.model.revertAll()
  28. ms = self.model.lastError().text()
  29.  
  30.  
  31. def default_val(self, rec):
  32. """
  33. add default values to the table. I can not use setData because that
  34. will emit dataChanged.
  35. This is not working.
  36. """
  37. query = QtSql.QSqlQuery()
  38. query.exec_("SELECT id from in_gtlf")
  39. if query.next():
  40. gtlf_id = query.value(0).toInt()[0]
  41. record = self.model.record(rec)
  42. record.setValue(1, "Event Alias Name")
  43. record.setValue(2, "Event name")
  44. record.setValue(3, "Event Definition")
  45. record.setValue(4, gtlf_id)
  46.  
  47.  
  48.  
  49. def add_row(self):
  50. """
  51. This method adds rows to the model.
  52. """
  53. row = self.model.rowCount()
  54. if self.model.lastError().isValid():
  55. ms = self.model.lastError().text()
  56. QtGui.QMessageBox.warning(self,"warning", ms, QtGui.QMessageBox.Ok)
  57. else:
  58. self.model.insertRows(row, 1)
  59. self.model.submitAll()
  60. index = self.model.index(row, 1)
  61. if index.isValid():
  62. self.app_field_tableView.edit(index)
  63. self.resizeRow()
To copy to clipboard, switch view to plain text mode