Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Sorting with MVC and database. Row number confusion - conceptual question
Forum Updated to NodeBB v4.3 + New Features

Sorting with MVC and database. Row number confusion - conceptual question

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 188 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ocgltdO Offline
    ocgltdO Offline
    ocgltd
    wrote on last edited by ocgltd
    #1

    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?

    JonBJ 1 Reply Last reply
    0
    • ocgltdO ocgltd

      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?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @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 of map...() methods for converting QModelIndexes 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.

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved