Hi Anda_skoa,

Thank you for your response. Below is the code. I think the problem is with self.join_model.insertRow(join_row). I only want to insert a row if the previous row was successfully added to the model. If the user keeps hitting the addrow button, then a row is added to the view, but not the model. This is because I am using a QSqlRelationalTableModel and ingredient cannot be null, therefore, if the user keeps hitting addrow then a row is added to the view, but not the model. How do you prevent a row from being added to the view if it is not added to the model first. This does not break anything it just looks bad. One option that I have thought of is reinstantiate the model class every time a row is added. While this will fix the problem this does not seem like a good approach.



Qt Code:
  1. def AddJoinRow(self):
  2. """this will allow one to add a row into the bottom model.
  3. This tool is a many to many relationship between recipes and ingrendents.
  4. Ingredients our popluated by the program. This is a many to many relationship.
  5. This two tool has to models and two views the top model handles the recipes and the bottom model handles the ingredients.
  6. """
  7. ## recipe row count
  8. recipe_count = self.obj_model.rowCount()
  9. ## this is to test if there is at least one planning ID.
  10. if recipe_count == 0:
  11. msg = "you must add a recipe before you can add a row to the join table."
  12. QtGui.QMessageBox.warning(self, "warning", msg, QtGui.QMessageBox.Ok)
  13. return
  14. ## else there is at least one row in the recipe model.
  15. else:
  16. ## get the selected row in the recipe table.
  17. selected_index = self.obj_tableView.currentIndex()
  18. ## this tests if there is not a selected index in the recipe model.
  19. if not selected_index.isValid():
  20. msg = "you must select a recipe row before adding a row in the "
  21. QtGui.QMessageBox.warning(self, "warning", msg, QtGui.QMessageBox.Ok)
  22. return
  23. ## else there is a selected index in the planning objective model.
  24. else:
  25. ## get the row index of the selected recipe
  26. row_index = selected_index.row()
  27. max_id = 1
  28. query = QtSql.QSqlQuery()
  29. ##Query the join database table to get the max id
  30. query.exec_("SELECT MAX(id) FROM join_recipe_ingredents")
  31. if query.next():
  32. max_id = query.value(0).toInt()[0]
  33.  
  34. ## query the recipe model to get the recipe id from the top model.
  35. ## cannot use rowcount because a user can remove a row.
  36. recipe_id = self.obj_model.data(self.obj_model.index(row_index, ID)).toInt()[0]
  37.  
  38. ## get the row count of the join table.
  39. join_row = self.join_model.rowCount()
  40. ## insert a row into the model.
  41. self.join_model.insertRow(join_row)
  42. ## set column 1 value to be the max id + 1
  43. self.join_model.setData(self.join_model.index(join_row, JOIN_ID), max_id + 1)
  44. ## set column 2 value to be the recipe_id
  45. self.join_model.setData(self.join_model.index(join_row, PLANNING_ID), recipe_id)
  46. ## column 3 is a combo box of all the ingrendents. I am using setRelation to populate this combobox.
  47.  
  48. ## get the index of the newly added row.
  49. index = self.join_model.index(join_row, CFR_ID)
  50. if index.isValid():
  51. ## set it to editing.
  52. self.join_tableView.edit(index)
  53. else:
  54. msg = "row was not inserted because you need to change row to a unique vaule."
  55. QtGui.QMessageBox.warning(self, "warning", msg, QtGui.QMessageBox.Ok)
  56. self.resizeRow()
To copy to clipboard, switch view to plain text mode