Sorting with MVC and database. Row number confusion - conceptual question
-
I'm building my first MVC app and have a conceptual question. Assuming I have:
View -> QSortFilterProxyModel -> QAbstractItemModel -> DB
Let's say my view has three columns: FIRSTNAME, LASTNAME, AGE. I can sort by any of these by clicking a column header in the View, and my QSortFilterProxyModel level will reorder the contents. So far so good.
My confusion is this: The 'data' function in the QAbstractItemModel is has to return a database row based on the ROW number it receives. But what is the row number? If I sort my view by LASTNAME, and want to get the data for row 0, the request get sent to the QSFPM, which sends it to the QAIM, which retrieves row 0 from the DB. But that assumes the DB is sorted by LASTNAME as well, but it is sorted by age.
How/where do I translate row 0 of the view (sorted by LASTNAME), to row X of the DB (sorted by AGE). Does the QSFPM translate view row numbers to QAIM row numbers as read from the DB? Or I have to have the QAIM perform SQL Selects using a different 'sort by' order based on the View's sort order?
-
I'm building my first MVC app and have a conceptual question. Assuming I have:
View -> QSortFilterProxyModel -> QAbstractItemModel -> DB
Let's say my view has three columns: FIRSTNAME, LASTNAME, AGE. I can sort by any of these by clicking a column header in the View, and my QSortFilterProxyModel level will reorder the contents. So far so good.
My confusion is this: The 'data' function in the QAbstractItemModel is has to return a database row based on the ROW number it receives. But what is the row number? If I sort my view by LASTNAME, and want to get the data for row 0, the request get sent to the QSFPM, which sends it to the QAIM, which retrieves row 0 from the DB. But that assumes the DB is sorted by LASTNAME as well, but it is sorted by age.
How/where do I translate row 0 of the view (sorted by LASTNAME), to row X of the DB (sorted by AGE). Does the QSFPM translate view row numbers to QAIM row numbers as read from the DB? Or I have to have the QAIM perform SQL Selects using a different 'sort by' order based on the View's sort order?
@ocgltd said in Sorting with MVC and database. Row number confusion - conceptual question:
Does the QSFPM translate view row numbers to QAIM row numbers as read from the DB?
Yes. And note that
QSortFilterProxyModel
has a bunch ofmap...()
methods for convertingQModelIndex
es between it and the source model when these are needed.Or I have to have the QAIM perform SQL Selects using a different 'sort by' order based on the View's sort order?
Noooo :) And note that any model could be used by multiple views, each with different sorting/filtering, so the model never needs to do anything about, or know anything about, what views might do with its data.