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. [SOLVED] QSortFilterProxyModel::mapFromSource crashes application
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QSortFilterProxyModel::mapFromSource crashes application

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 4.0k 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.
  • A Offline
    A Offline
    A1exander_Z
    wrote on last edited by
    #1

    I have a view (QTableView), proxy model used for sorting (QSortFilterProxyModel), and the model proper (QSqlTableModel). I am trying to save row selection (selection of individual cells is disabled) in the view and restore it, if possible, after a database update (when model->select() is called). The selection is stored as a list of model row numbers (integers) in the following way:

    @selection.clear();
    foreach (QModelIndex idx, view->selectionModel()->selectedRows())
    selection << proxy->mapToSource(idx).row();@

    The resulting list is correct, it is filled with the original model row numbers independent of the current sorting by QSortFilterProxyModel.

    However, when I try to restore the selection in the following way:

    @if (!selection.isEmpty())
    {
    view->setSelectionMode(QAbstractItemView::MultiSelection);
    foreach (int i, selection)
    {
    if (i >= 0 && i < view->model()->rowCount())
    {
    QModelIndex idx = proxy->index(i, 0);
    int row = proxy->mapFromSource(idx).row();
    view->selectRow(row);
    }
    }
    }@

    The program crashes when calling proxy->mapFromSource(idx).row(). What am I doing wrong? The following code works:

    @if (!selection.isEmpty())
    {
    view->setSelectionMode(QAbstractItemView::MultiSelection);
    foreach (int i, selection)
    {
    if (i >= 0 && i < view->model()->rowCount())
    view->selectRow(i);
    }
    }@

    Of course, in this case the restored selection corresponds to the original model row numbers, not to row numbers after sorting.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Might be a silly question, but is the proxy correctly instantiated before calling mapFromSource ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        A1exander_Z
        wrote on last edited by
        #3

        It is. The view, the proxy, and the model are all instantiated only once, in the constructor of the widget containing the view. There are no issues with presenting and sorting the table model, so the proxy works properly. Moreover, the first piece of code, where proxy->mapToSource is called, is always executed before the second piece (with mapFromSource call). And

        @QModelIndex idx = proxy->index(i, 0);@

        line, just before mapFromSource call, produces a QModelIndex object which looks correct (in the debugger).

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Ok, I see. I think the problem is that you are trying to use mapFromSource with an index coming from the proxy model.

          mapFromSource should be called with an index from the source model.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • A Offline
            A Offline
            A1exander_Z
            wrote on last edited by
            #5

            Thank you, you are absolutely right! Changing the line

            @QModelIndex idx = proxy->index(i, 0);@

            to

            @QModelIndex idx = model->index(i, 0);@

            solved the problem.

            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