PDA

View Full Version : Issue with insertRow



lukeQt
1st April 2015, 22:50
Hi Everyone,

I have a QtSql.QSqlRelationalTableModel and a tableView. I can insertRows just fine. If the index is not valid though a blank row is added to the view, but not the model. How do you prevent adding a row to the model if no row is added to the database? This happens when the user does not specify a value. How can you validate that the user has specified a value before adding another row. I was looking at dataChanged, but was not sure how to use this. 11052

anda_skoa
2nd April 2015, 08:03
Can you post the code you are executing when inserting a row?

Cheers,
_

lukeQt
2nd April 2015, 16:17
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.





def AddJoinRow(self):
"""this will allow one to add a row into the bottom model.
This tool is a many to many relationship between recipes and ingrendents.
Ingredients our popluated by the program. This is a many to many relationship.
This two tool has to models and two views the top model handles the recipes and the bottom model handles the ingredients.
"""
## recipe row count
recipe_count = self.obj_model.rowCount()
## this is to test if there is at least one planning ID.
if recipe_count == 0:
msg = "you must add a recipe before you can add a row to the join table."
QtGui.QMessageBox.warning(self, "warning", msg, QtGui.QMessageBox.Ok)
return
## else there is at least one row in the recipe model.
else:
## get the selected row in the recipe table.
selected_index = self.obj_tableView.currentIndex()
## this tests if there is not a selected index in the recipe model.
if not selected_index.isValid():
msg = "you must select a recipe row before adding a row in the "
QtGui.QMessageBox.warning(self, "warning", msg, QtGui.QMessageBox.Ok)
return
## else there is a selected index in the planning objective model.
else:
## get the row index of the selected recipe
row_index = selected_index.row()
max_id = 1
query = QtSql.QSqlQuery()
##Query the join database table to get the max id
query.exec_("SELECT MAX(id) FROM join_recipe_ingredents")
if query.next():
max_id = query.value(0).toInt()[0]

## query the recipe model to get the recipe id from the top model.
## cannot use rowcount because a user can remove a row.
recipe_id = self.obj_model.data(self.obj_model.index(row_index , ID)).toInt()[0]

## get the row count of the join table.
join_row = self.join_model.rowCount()
## insert a row into the model.
self.join_model.insertRow(join_row)
## set column 1 value to be the max id + 1
self.join_model.setData(self.join_model.index(join _row, JOIN_ID), max_id + 1)
## set column 2 value to be the recipe_id
self.join_model.setData(self.join_model.index(join _row, PLANNING_ID), recipe_id)
## column 3 is a combo box of all the ingrendents. I am using setRelation to populate this combobox.

## get the index of the newly added row.
index = self.join_model.index(join_row, CFR_ID)
if index.isValid():
## set it to editing.
self.join_tableView.edit(index)
else:
msg = "row was not inserted because you need to change row to a unique vaule."
QtGui.QMessageBox.warning(self, "warning", msg, QtGui.QMessageBox.Ok)
self.resizeRow()