PDA

View Full Version : QSqlTableModel Setting Current Row?



fifth
15th May 2008, 19:53
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.



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)


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

wysota
18th May 2008, 09:05
But what exactly is the problem? The model index identifies each record just fine. If you have a relational model, you can query its relation model if you need it (although I don't know if you do).

fifth
19th May 2008, 19:23
But what exactly is the problem? The model index identifies each record just fine. If you have a relational model, you can query its relation model if you need it (although I don't know if you do).

I need to 'goto' a record within the model which is identified with a field value. The field is an auto-incremented index. I can do this by filtering, but that means navigating by next,prev,first,last does not work as I am now working with a result of 1.

I need to maintain access to all records in the table but be able to set the cursor position within the table model according the field value.

The auto incremented field will not necessarily correspond to the model row number as the model may be re-ordered or filtered by the user.

Is it possible to? :

Filter the model data
Create a variable pointer to the single filtered record
Reset the filter
Set the model index to the previously filtered record


Not really sure if I'm explaining any of this properly!

I'm not even sure if this is possible using the Qt model/view properly. I might try re-implementing the edit form with sql and populating my widgets from the result set.

wysota
20th May 2008, 09:35
Won't QAbstractItemModel::match() allow you to find the proper index? Anyway, wouldn't it be better if the lookup dialog returned a model index instead of the field value? If you operate on the same set of tables, it would be wise to stick with the model interface everywhere possible.

fifth
15th June 2008, 13:44
Won't QAbstractItemModel::match() allow you to find the proper index?

Thanks, I have things working at the moment using functions to find next, prev etc with sql. But I'll have a look into the match method, seems much simpler and exactly what I was looking for.


Anyway, wouldn't it be better if the lookup dialog returned a model index instead of the field value? If you operate on the same set of tables, it would be wise to stick with the model interface everywhere possible.

I did consider using the same model across all windows/dialogs. However, I decided against that given an example situation such as;

1. Main Client List window - user filters the records
2. Client Detail/Edit window - user creates a new record

Now, if the new record doesn't fit the filter, then from the users perspective their newly created record 'vanishes'.