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.
FIRST, PREV, NEXT, LAST = range(4)
def __init__(self, client_id=None, clientModel=None, parent=None):
super(ClientEditDlg,self).__init__(parent)
if clientModel == None:
self.model.setTable("clients")
self.
model.
setRelation(db.
CURRENCY_ID,
QSqlRelation("currency",\
"currency_id", "currency_description"))
self.model.setRelation(db.STATUS_ID, \
"account_status_description"))
self.model.setSort(db.ACCOUNT, Qt.AscendingOrder)
self.model.select()
else:
self.model = clientModel
def lookupClient(self):
cp = ClientPopupDlg(self)
if cp.exec_():
client_id = cp.selectedID()
self.model.setFilter("client_id = %i" % client_id)
self.mapper.setCurrentIndex(0)
class ClientEditDlg(QDialog):
FIRST, PREV, NEXT, LAST = range(4)
def __init__(self, client_id=None, clientModel=None, parent=None):
super(ClientEditDlg,self).__init__(parent)
if clientModel == None:
self.model = QSqlRelationalTableModel(self)
self.model.setTable("clients")
self.model.setRelation(db.CURRENCY_ID, QSqlRelation("currency",\
"currency_id", "currency_description"))
self.model.setRelation(db.STATUS_ID, \
QSqlRelation("account_status", "account_status_id", \
"account_status_description"))
self.model.setSort(db.ACCOUNT, Qt.AscendingOrder)
self.model.select()
else:
self.model = clientModel
def lookupClient(self):
cp = ClientPopupDlg(self)
if cp.exec_():
client_id = cp.selectedID()
self.model.setFilter("client_id = %i" % client_id)
self.mapper.setCurrentIndex(0)
To copy to clipboard, switch view to plain text mode
QUERY_BASE = """
SELECT client_id, client_account, client_name
FROM clients"""
QUERY_ORDER = """ ORDER BY client_account"""
def __init__(self, parent=None):
super(ClientPopupDlg, self).__init__(parent)
query
= QSqlQuery(ClientPopupDlg.
QUERY_BASE+ClientPopupDlg.
QUERY_ORDER)
self.model.setQuery(query)
self.
model.
setHeaderData(1, Qt.
Horizontal,
QVariant("Acc")) self.
model.
setHeaderData(2, Qt.
Horizontal,
QVariant("Name")) self.model.select()
def selectedID(self):
s = self.clientView.selectedIndexes()
idModel = s[1].sibling(s[1].row(), 0)
client_id = idModel.data().toInt()[0]
return client_id
class ClientPopupDlg(QDialog):
QUERY_BASE = """
SELECT client_id, client_account, client_name
FROM clients"""
QUERY_ORDER = """ ORDER BY client_account"""
def __init__(self, parent=None):
super(ClientPopupDlg, self).__init__(parent)
query = QSqlQuery(ClientPopupDlg.QUERY_BASE+ClientPopupDlg.QUERY_ORDER)
self.model = QSqlTableModel(self)
self.model.setQuery(query)
self.model.setHeaderData(1, Qt.Horizontal, QVariant("Acc"))
self.model.setHeaderData(2, Qt.Horizontal, QVariant("Name"))
self.model.select()
def selectedID(self):
s = self.clientView.selectedIndexes()
idModel = s[1].sibling(s[1].row(), 0)
client_id = idModel.data().toInt()[0]
return client_id
To copy to clipboard, switch view to plain text mode
Bookmarks