ggI have a dialog which displays a database form for editing (LineEdits and ComboBoxes etc). I have a seperate popup dialog which the user can use to lookup records. The lookup dialog returns a row_id to the editing form which it loads from its own model into a mapper.

At the moment I'm using a 'quick fix' by filtering the model to the returned id, but this approach breaks the edit dialogs navigation buttons.

Is there any way to identify the row in the model? I have been considering looping through the records to find the right row, but there potentially could be a *lot* of rows in the table.

btw Both dialogs are querying the same table, but with different rows in there result set and one will be filtered and the other not - so obtaining the row number from the model or using the same model for both will not work.

Qt Code:
  1. class ClientEditDlg(QDialog):
  2.  
  3. FIRST, PREV, NEXT, LAST = range(4)
  4.  
  5. def __init__(self, client_id=None, clientModel=None, parent=None):
  6. super(ClientEditDlg,self).__init__(parent)
  7.  
  8. if clientModel == None:
  9. self.model = QSqlRelationalTableModel(self)
  10. self.model.setTable("clients")
  11. self.model.setRelation(db.CURRENCY_ID, QSqlRelation("currency",\
  12. "currency_id", "currency_description"))
  13. self.model.setRelation(db.STATUS_ID, \
  14. QSqlRelation("account_status", "account_status_id", \
  15. "account_status_description"))
  16. self.model.setSort(db.ACCOUNT, Qt.AscendingOrder)
  17. self.model.select()
  18. else:
  19. self.model = clientModel
  20.  
  21. def lookupClient(self):
  22. cp = ClientPopupDlg(self)
  23. if cp.exec_():
  24. client_id = cp.selectedID()
  25. self.model.setFilter("client_id = %i" % client_id)
  26. self.mapper.setCurrentIndex(0)
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. class ClientPopupDlg(QDialog):
  2.  
  3. QUERY_BASE = """
  4. SELECT client_id, client_account, client_name
  5. FROM clients"""
  6. QUERY_ORDER = """ ORDER BY client_account"""
  7. def __init__(self, parent=None):
  8. super(ClientPopupDlg, self).__init__(parent)
  9.  
  10. query = QSqlQuery(ClientPopupDlg.QUERY_BASE+ClientPopupDlg.QUERY_ORDER)
  11.  
  12. self.model = QSqlTableModel(self)
  13. self.model.setQuery(query)
  14. self.model.setHeaderData(1, Qt.Horizontal, QVariant("Acc"))
  15. self.model.setHeaderData(2, Qt.Horizontal, QVariant("Name"))
  16. self.model.select()
  17.  
  18. def selectedID(self):
  19. s = self.clientView.selectedIndexes()
  20. idModel = s[1].sibling(s[1].row(), 0)
  21. client_id = idModel.data().toInt()[0]
  22. return client_id
To copy to clipboard, switch view to plain text mode