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. QSqlTableModel/View, re-select same registers when index changed due to re-ordering

QSqlTableModel/View, re-select same registers when index changed due to re-ordering

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 759 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.
  • I Offline
    I Offline
    i92guboj
    wrote on last edited by
    #1

    Hello.

    Imagine a table like this:

    @1 one first
    2 two second
    3 three third@

    Easy enough to understand ;)

    I also have a QDataWidgetMapper instance with a bunch of QLineEdits where all this info is displayed conveniently. They are also used as an editor, but that's irrelevant here.

    If I select the third register/row, I see its fields displayed in the QDataWidgetMapper fields, and the row is highlighted in the QTableView. If I changed some field, it's correctly updated, as it's due.

    The problem comes when I do this: first, I set the view to be ordered by the second column, then we have this:

    @1 one first
    3 three third
    2 two second@

    Then I select the third line, which now is "2 two second", and edit it using the QLineEdits, and set the second field to "beta", so the line now is "2 beta second".

    As the vew is set up to re-order by the second column, the list is rearranged this way, automatically:

    @2 beta second
    1 one first
    3 three third@

    But, the selected row continues to be the third, which, in my case, is not desirable.

    By adding some qDebugs here and there, I found that it isn't only the view which is rearranged. The model is also reordered, so that the indexes in the view and the model always match (which is ok, otherwise I would be greatly screwed at this stage :lol: ).

    So, the question is: is there a way to track the new index so that the selection stays at the register I am editing, semantically speaking? I would like to continue editing "2 beta second" after the table has been reordered...

    Thanks for any idea :)

    1 Reply Last reply
    0
    • I Offline
      I Offline
      i92guboj
      wrote on last edited by
      #2

      For now I am wrapping the save operations into a method that gets the current ID (autonumeric), and after submitting to the db I walk through the model until I find a match. Then if(new_row != old_row) { I_do_what_needs_to_be_done }.

      1 Reply Last reply
      0

      • Login

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