Results 1 to 3 of 3

Thread: Issue with insertRow

  1. #1
    Join Date
    Oct 2014
    Posts
    22
    Qt products
    Qt3 Qt4 Qt5 PyQt3 PyQt4

    Default Issue with insertRow

    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. blankrow.jpg

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Issue with insertRow

    Can you post the code you are executing when inserting a row?

    Cheers,
    _

  3. #3
    Join Date
    Oct 2014
    Posts
    22
    Qt products
    Qt3 Qt4 Qt5 PyQt3 PyQt4

    Default Re: Issue with insertRow

    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 

Similar Threads

  1. tableview with proxy not refreshing on insertRow
    By BreakBad in forum Qt Programming
    Replies: 2
    Last Post: 6th March 2013, 17:42
  2. QTableView insertRow 's dynamically
    By migel in forum Newbie
    Replies: 1
    Last Post: 30th June 2011, 13:08
  3. How to InsertRow Throught QSqlQueryModel ?
    By innobleday in forum Qt Programming
    Replies: 0
    Last Post: 24th March 2010, 04:44
  4. QStandardItemModel insertRow crashing
    By munna in forum Qt Programming
    Replies: 1
    Last Post: 27th June 2008, 11:55
  5. QTableView QTablemodel & insertRow
    By pfusterschmied in forum Qt Programming
    Replies: 2
    Last Post: 5th June 2007, 12:43

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.