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

  • 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 :)

  • 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 }.

Log in to reply