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. QTableView, QAbstractTableModel, QSortFilterProxyModel, Sorting
Forum Updated to NodeBB v4.3 + New Features

QTableView, QAbstractTableModel, QSortFilterProxyModel, Sorting

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 3 Posters 5.5k 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.
  • VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by
    #11

    No, you are free to call ui->tableView->model()->sort(); wherever/whenever you want or even connect a signal to sort() of the model

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    1 Reply Last reply
    1
    • S Offline
      S Offline
      Slawomir_Piernikowski
      wrote on last edited by Slawomir_Piernikowski
      #12

      In Qt doc:
      void QTableView::setSortingEnabled(bool enable)
      If enable is true, enables sorting for the table and immediately trigger a call to sortByColumn() with the current sort section and order
      Note: Setter function for property sortingEnabled.
      See also isSortingEnabled().

      Now we look for: sortByColumn();

      void QTableView::sortByColumn(int column, Qt::SortOrder order)
      Sorts the model by the values in the given column in the given order.
      This function was introduced in Qt 4.2.

      So indeed it is a one way of sorting a model.

      Lets come back to:
      You wrote: "the default implementation of QSortFilterProxyModel however, takes care of sorting perfectly fine"

      But it looks like it does not. I have checked it:
      State:
      ui->tableView.setSortingEnabled(false);
      customized proxy model and customized model do not have overloaded sort functions.

      In qt doc is written that the sort function in QSortFilterProxyModel is derived from QAbstractItemModel class and the base class implementation does nothing.

      [virtual] void QAbstractItemModel::sort(int column, Qt::SortOrder order = Qt::AscendingOrder)
      Sorts the model by column in the given order.
      The base class implementation does nothing.
      So???

      VRoninV 1 Reply Last reply
      0
      • S Slawomir_Piernikowski

        In Qt doc:
        void QTableView::setSortingEnabled(bool enable)
        If enable is true, enables sorting for the table and immediately trigger a call to sortByColumn() with the current sort section and order
        Note: Setter function for property sortingEnabled.
        See also isSortingEnabled().

        Now we look for: sortByColumn();

        void QTableView::sortByColumn(int column, Qt::SortOrder order)
        Sorts the model by the values in the given column in the given order.
        This function was introduced in Qt 4.2.

        So indeed it is a one way of sorting a model.

        Lets come back to:
        You wrote: "the default implementation of QSortFilterProxyModel however, takes care of sorting perfectly fine"

        But it looks like it does not. I have checked it:
        State:
        ui->tableView.setSortingEnabled(false);
        customized proxy model and customized model do not have overloaded sort functions.

        In qt doc is written that the sort function in QSortFilterProxyModel is derived from QAbstractItemModel class and the base class implementation does nothing.

        [virtual] void QAbstractItemModel::sort(int column, Qt::SortOrder order = Qt::AscendingOrder)
        Sorts the model by column in the given order.
        The base class implementation does nothing.
        So???

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #13

        @Slawomir_Piernikowski said in QTableView, QAbstractTableModel, QSortFilterProxyModel, Sorting:

        In qt doc is written that the sort function in QSortFilterProxyModel is derived from QAbstractItemModel class and the base class implementation does nothing.

        But QSortFilterProxyModel reimplements it to do stuff.
        It's easy to check the difference in code between QAbstractItemModel::sort and QSortFilterProxyModel::sort

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        2
        • S Offline
          S Offline
          Slawomir_Piernikowski
          wrote on last edited by
          #14

          In Qt code:

          void QAbstractItemModel::sort(int column, Qt::SortOrder order)
          {
              Q_UNUSED(column);
              Q_UNUSED(order);
              // do nothing
          }
          
          void QSortFilterProxyModel::sort(int column, Qt::SortOrder order)
          {
              Q_D(QSortFilterProxyModel);
              if (d->dynamic_sortfilter && d->proxy_sort_column == column && d->sort_order == order)
                  return;
              d->sort_order = order;
              d->proxy_sort_column = column;
              d->update_source_sort_column();
              d->sort();
          }
          

          So as I see in the abowe code: if I have customized QSortFilterProxyModel the sort function is already overrided and do the sorting.... but
          do You agree that if ui->tableView.setSortingEnabled(false); the sorting is not posible???

          VRoninV 1 Reply Last reply
          0
          • S Slawomir_Piernikowski

            In Qt code:

            void QAbstractItemModel::sort(int column, Qt::SortOrder order)
            {
                Q_UNUSED(column);
                Q_UNUSED(order);
                // do nothing
            }
            
            void QSortFilterProxyModel::sort(int column, Qt::SortOrder order)
            {
                Q_D(QSortFilterProxyModel);
                if (d->dynamic_sortfilter && d->proxy_sort_column == column && d->sort_order == order)
                    return;
                d->sort_order = order;
                d->proxy_sort_column = column;
                d->update_source_sort_column();
                d->sort();
            }
            

            So as I see in the abowe code: if I have customized QSortFilterProxyModel the sort function is already overrided and do the sorting.... but
            do You agree that if ui->tableView.setSortingEnabled(false); the sorting is not posible???

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #15

            @Slawomir_Piernikowski said in QTableView, QAbstractTableModel, QSortFilterProxyModel, Sorting:

            You agree that if ui->tableView.setSortingEnabled(false); the sorting is not posible???

            No, I don't.

            @VRonin said in QTableView, QAbstractTableModel, QSortFilterProxyModel, Sorting:

            you are free to call ui->tableView->model()->sort(); wherever/whenever you want or even connect a signal to sort() of the model

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            S 1 Reply Last reply
            2
            • S Offline
              S Offline
              Slawomir_Piernikowski
              wrote on last edited by Slawomir_Piernikowski
              #16

              So I decided to not use ui->tableView.setSortingEnabled(false) or ui->tableView.setSortingEnabled(true). I have customized proxy model and customized model which do not have overloaded sort functions.
              I have tried to call setData function of the model(QAbstractTableModel) by clicking on a cell of the QTableView The data was changed but the model and view was not sorted ... so from the soft behaviour it looks like if you want to sort data from the level of the TableView it is obligatory to have set: ui->tableView.setSortingEnabled(true) or ui->tableView.setSortingEnabled(false) and use
              sortByColumn function.

              Am I right or I have missed something???
              Maybe this will be helpful:

              void QTableView::sortByColumn(int column, Qt::SortOrder order)
              {
                  Q_D(QTableView);
                  if (column < 0)
                      return;
                  // If sorting is enabled it will emit a signal connected to
                  // _q_sortIndicatorChanged, which then actually sorts
                  d->horizontalHeader->setSortIndicator(column, order);
                  // If sorting is not enabled, force to sort now
                  if (!d->sortingEnabled)
                      d->model->sort(column, order);
              }
              

              If sotring is not enabled, force to sort now???
              So even if ui->tableView.setSortingEnabled(false) and we call sortByColumn the model will be sorted.
              I asked:
              You agree that if ui->tableView.setSortingEnabled(false); the sorting is not posible???
              You answered:
              No, I don't.
              Yes You were right even if ui->tableView.setSortingEnabled(false) we can still sort a model using
              sortByColumn function.

              But if ui->tableView.setSortingEnabled(false) which is by default and we do not use sortByColumn function in my soft sorting not works when I set data of a cell of the tableView.

              VRoninV 1 Reply Last reply
              0
              • VRoninV VRonin

                @Slawomir_Piernikowski said in QTableView, QAbstractTableModel, QSortFilterProxyModel, Sorting:

                You agree that if ui->tableView.setSortingEnabled(false); the sorting is not posible???

                No, I don't.

                @VRonin said in QTableView, QAbstractTableModel, QSortFilterProxyModel, Sorting:

                you are free to call ui->tableView->model()->sort(); wherever/whenever you want or even connect a signal to sort() of the model

                S Offline
                S Offline
                Slawomir_Piernikowski
                wrote on last edited by Slawomir_Piernikowski
                #17

                @VRonin said in QTableView, QAbstractTableModel, QSortFilterProxyModel, Sorting:

                you are free to call ui->tableView->model()->sort();

                Yes of course it can be done.
                I just add in:

                bool MagazynModel::setData(const QModelIndex &index, const QVariant &value, int role)
                {
                      ....
                      this->sort(index.column());
                }
                

                and now when I set data in the tableView sorting is working.

                1 Reply Last reply
                0
                • S Slawomir_Piernikowski

                  So I decided to not use ui->tableView.setSortingEnabled(false) or ui->tableView.setSortingEnabled(true). I have customized proxy model and customized model which do not have overloaded sort functions.
                  I have tried to call setData function of the model(QAbstractTableModel) by clicking on a cell of the QTableView The data was changed but the model and view was not sorted ... so from the soft behaviour it looks like if you want to sort data from the level of the TableView it is obligatory to have set: ui->tableView.setSortingEnabled(true) or ui->tableView.setSortingEnabled(false) and use
                  sortByColumn function.

                  Am I right or I have missed something???
                  Maybe this will be helpful:

                  void QTableView::sortByColumn(int column, Qt::SortOrder order)
                  {
                      Q_D(QTableView);
                      if (column < 0)
                          return;
                      // If sorting is enabled it will emit a signal connected to
                      // _q_sortIndicatorChanged, which then actually sorts
                      d->horizontalHeader->setSortIndicator(column, order);
                      // If sorting is not enabled, force to sort now
                      if (!d->sortingEnabled)
                          d->model->sort(column, order);
                  }
                  

                  If sotring is not enabled, force to sort now???
                  So even if ui->tableView.setSortingEnabled(false) and we call sortByColumn the model will be sorted.
                  I asked:
                  You agree that if ui->tableView.setSortingEnabled(false); the sorting is not posible???
                  You answered:
                  No, I don't.
                  Yes You were right even if ui->tableView.setSortingEnabled(false) we can still sort a model using
                  sortByColumn function.

                  But if ui->tableView.setSortingEnabled(false) which is by default and we do not use sortByColumn function in my soft sorting not works when I set data of a cell of the tableView.

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by VRonin
                  #18

                  @Slawomir_Piernikowski said in QTableView, QAbstractTableModel, QSortFilterProxyModel, Sorting:

                  when I set data of a cell of the tableView.

                  This was they key piece of information you left out in your previous posts. You want automatic sorting when data changes. In that case setting ui->tableView->setSortingEnabled(true); is the most convenient way, alternatively you can QObject::connect(ui->tableView->model(),&QAbstractItemModel::dataChanged,ui->tableView->model(),&QAbstractItemModel::sort,Qt::QueuedConnection);

                  P.S.
                  sortByColumn has been deprecated, it should not be used in any new code

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  S JonBJ 2 Replies Last reply
                  2
                  • VRoninV VRonin

                    @Slawomir_Piernikowski said in QTableView, QAbstractTableModel, QSortFilterProxyModel, Sorting:

                    when I set data of a cell of the tableView.

                    This was they key piece of information you left out in your previous posts. You want automatic sorting when data changes. In that case setting ui->tableView->setSortingEnabled(true); is the most convenient way, alternatively you can QObject::connect(ui->tableView->model(),&QAbstractItemModel::dataChanged,ui->tableView->model(),&QAbstractItemModel::sort,Qt::QueuedConnection);

                    P.S.
                    sortByColumn has been deprecated, it should not be used in any new code

                    S Offline
                    S Offline
                    Slawomir_Piernikowski
                    wrote on last edited by Slawomir_Piernikowski
                    #19

                    @VRonin Yes I ment automatic sorting when setting data from the level of the QTableView

                    1 Reply Last reply
                    0
                    • VRoninV VRonin

                      @Slawomir_Piernikowski said in QTableView, QAbstractTableModel, QSortFilterProxyModel, Sorting:

                      when I set data of a cell of the tableView.

                      This was they key piece of information you left out in your previous posts. You want automatic sorting when data changes. In that case setting ui->tableView->setSortingEnabled(true); is the most convenient way, alternatively you can QObject::connect(ui->tableView->model(),&QAbstractItemModel::dataChanged,ui->tableView->model(),&QAbstractItemModel::sort,Qt::QueuedConnection);

                      P.S.
                      sortByColumn has been deprecated, it should not be used in any new code

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

                      @VRonin

                      QObject::connect(ui->tableView->model(),&QAbstractItemModel::dataChanged,ui->tableView->model(),&QAbstractItemModel::sort,Qt::QueuedConnection);
                      

                      This is not the place for detailed discussion, but in a word why do you specify Qt::QueuedConnection here, please?

                      1 Reply Last reply
                      0
                      • VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by
                        #21

                        If you set multiple roles for the same index the sorting should be done only once, I was too lazy to write down the check for the 3rd argument of dataChanged so I just cheated and let the event loop take care of it

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          Slawomir_Piernikowski
                          wrote on last edited by Slawomir_Piernikowski
                          #22

                          Code from Qt doc:

                          void QTableView::sortByColumn(int column, Qt::SortOrder order)
                          {
                              Q_D(QTableView);
                              if (column < 0)
                                  return;
                              // If sorting is enabled it will emit a signal connected to
                              // _q_sortIndicatorChanged, which then actually sorts
                              d->horizontalHeader->setSortIndicator(column, order);
                              // If sorting is not enabled, force to sort now
                              if (!d->sortingEnabled)
                                  d->model->sort(column, order); 
                          // this sort function is called from 
                          //QAbstractItemModel so do nothing. See hereunder function
                          
                          }
                          
                          
                          /*!
                              Sorts the model by \a column in the given \a order.
                              The base class implementation does nothing.
                          */
                          void QAbstractItemModel::sort(int column, Qt::SortOrder order)
                          {
                              Q_UNUSED(column);
                              Q_UNUSED(order);
                              // do nothing
                          }
                          

                          In Qt doc:

                          1. QTableView::setSortingEnabled(true) calls void QTableView::sortByColumn(int column, Qt::SortOrder order)

                          2. QTableView::sortByColumn fuction If sorting is enabled it will emit a signal connected to
                            _q_sortIndicatorChanged, which then actually sorts or If sorting is not enabled, force to sort now but if only the sort function in QAbstractItemModel::sort is overloaded and the body of the function has a code which sort, or you have customized QSortFilterProxyModel which has the sort function and the function is called when you click the header column arrow but not sorts when you setData from tableView level.

                          3. if QTableView::setSortingEnabled(false) the header column arrow for descending or ascending sorting is not seen so there is no posibility to sort from the level of TableView.

                          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