<small id='pZgG9'></small><noframes id='pZgG9'>

  • <legend id='pZgG9'><style id='pZgG9'><dir id='pZgG9'><q id='pZgG9'></q></dir></style></legend>

      1. <i id='pZgG9'><tr id='pZgG9'><dt id='pZgG9'><q id='pZgG9'><span id='pZgG9'><b id='pZgG9'><form id='pZgG9'><ins id='pZgG9'></ins><ul id='pZgG9'></ul><sub id='pZgG9'></sub></form><legend id='pZgG9'></legend><bdo id='pZgG9'><pre id='pZgG9'><center id='pZgG9'></center></pre></bdo></b><th id='pZgG9'></th></span></q></dt></tr></i><div id='pZgG9'><tfoot id='pZgG9'></tfoot><dl id='pZgG9'><fieldset id='pZgG9'></fieldset></dl></div>

        <tfoot id='pZgG9'></tfoot>
          <bdo id='pZgG9'></bdo><ul id='pZgG9'></ul>

        QSortFilterProxyModel 对多列进行排序

        时间:2023-10-18

        <tfoot id='2uRIi'></tfoot>

          • <bdo id='2uRIi'></bdo><ul id='2uRIi'></ul>
            <legend id='2uRIi'><style id='2uRIi'><dir id='2uRIi'><q id='2uRIi'></q></dir></style></legend>
              <tbody id='2uRIi'></tbody>

            <small id='2uRIi'></small><noframes id='2uRIi'>

            • <i id='2uRIi'><tr id='2uRIi'><dt id='2uRIi'><q id='2uRIi'><span id='2uRIi'><b id='2uRIi'><form id='2uRIi'><ins id='2uRIi'></ins><ul id='2uRIi'></ul><sub id='2uRIi'></sub></form><legend id='2uRIi'></legend><bdo id='2uRIi'><pre id='2uRIi'><center id='2uRIi'></center></pre></bdo></b><th id='2uRIi'></th></span></q></dt></tr></i><div id='2uRIi'><tfoot id='2uRIi'></tfoot><dl id='2uRIi'><fieldset id='2uRIi'></fieldset></dl></div>
                  本文介绍了QSortFilterProxyModel 对多列进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试实现一个可在多个列上排序的表.Qt 的 QSortFilterProxyModel 仅支持对一列进行排序(至少在 Qt 4.6.2 中).

                  I am trying to implement a table that is sortable on more than one column. Qt's QSortFilterProxyModel only supports sorting on one column (at least in Qt 4.6.2).

                  我在 github 上找到了 dimkanovikov 的此解决方案,但它缺少对添加行的动态更新.我的意思是,模型发生了变化,并且 beginInsertRows()、beginRemoveRows()、它们对应的 end..-methods 和 dataChanged() 信号被发出.理想情况下,我只想更新这些行,但模型至少应该对此类更改做出反应.

                  I've found this solution by dimkanovikov on github, but it lacks dynamic updating on added rows. What I mean by this, is that the model is changed and the beginInsertRows(), beginRemoveRows(), their corresponding end..-methods and the dataChanged() signals are emitted. Ideally I would like to only these rows to be updated, but the model should at least react to such changes.

                  Qt 网站上还有另一个常见问题解答项目,可以对 QTableWidget 进行排序,但它也缺乏动态更新.

                  There's another FAQ item on Qt's site that sorts a QTableWidget, but it lacks dynamic updating, too.

                  我是 Qt 的新手,我想得到一些关于我应该如何处理的建议.

                  I am new to Qt and I'd like to get some pointers on how I should go about this.

                  推荐答案

                  有一个稍微不雅的解决方案,它总是用于对多列进行排序.

                  There's one slightly inelegant solution, that is always used to sort multiple columns.

                  您必须继承 QSortFilterProxyModel 并重新实现 bool lessThan(const QModelIndex &rLeft, const QModelIndex &rRight) const.检查所有列,而不是仅在两个给定索引之间进行比较:

                  You have to subclass QSortFilterProxyModel and reimplement bool lessThan(const QModelIndex &rLeft, const QModelIndex &rRight) const. Instead of only comparing between the two given indices, check all the columns:

                  int const left_row  = rLeft.row();
                  int const right_row = rRight.row();
                  
                  int const num_columns = sourceModel()->columnCount();
                  for(int compared_column = rLeft.column(); compared_column<num_columns; ++compared_column) {
                      QModelIndex const left_idx = sourceModel()->index(left_row, compared_column, QModelIndex());
                      QModelIndex const right_idx = sourceModel()->index(right_row, compared_column, QModelIndex());
                  
                      QString const leftData = sourceModel()->data(left_idx).toString();
                      QString const rightData = sourceModel()->data(right_idx).toString();
                  
                      int const compare = QString::localeAwareCompare(leftData, rightData);
                      if(compare!=0) {
                          return compare<0;
                      }
                  }
                  
                  return false;
                  

                  然后您可以在您的 QSortFilterProxyModel 子类上调用 sort(0) ,它将对所有列进行排序.如果您希望在模型数据更改时动态重新排序已排序的行,也不要忘记调用 setDynamicSortFilter(true).

                  Then you can call sort(0) on your QSortFilterProxyModel subclass and it will sort all the columns. Also don't forget to call setDynamicSortFilter(true) when you want the sorted rows to be dynamically resorted when the model data changes.

                  要支持按升序或降序对任意列进行排序,您必须将此信息保存在 QList 中,并在调用 lessThan 时进行相应比较.在列表中,您将按优先级顺序排列列,并以相同的顺序进行比较.您还应该以某种预定义的顺序对其他非活动"列进行排序,否则默认情况下它们不会被排序.

                  To support sorting on arbitrary columns in ascending or descending order, you would have to keep this info in a QList and compare accordingly when lessThan is called. In the list you would have the columns in order of their priority and do the comparisons in the same order. You should also sort the other "inactive" columns in some predefined order, otherwise they will not be sorted by default.

                  这篇关于QSortFilterProxyModel 对多列进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将 QAbstractTableModel 与 Qml TableView 一起使用仅显示第一列 下一篇:为树视图创建 Qt 模型

                  相关文章

                    <bdo id='Y0VNJ'></bdo><ul id='Y0VNJ'></ul>

                  1. <small id='Y0VNJ'></small><noframes id='Y0VNJ'>

                    <i id='Y0VNJ'><tr id='Y0VNJ'><dt id='Y0VNJ'><q id='Y0VNJ'><span id='Y0VNJ'><b id='Y0VNJ'><form id='Y0VNJ'><ins id='Y0VNJ'></ins><ul id='Y0VNJ'></ul><sub id='Y0VNJ'></sub></form><legend id='Y0VNJ'></legend><bdo id='Y0VNJ'><pre id='Y0VNJ'><center id='Y0VNJ'></center></pre></bdo></b><th id='Y0VNJ'></th></span></q></dt></tr></i><div id='Y0VNJ'><tfoot id='Y0VNJ'></tfoot><dl id='Y0VNJ'><fieldset id='Y0VNJ'></fieldset></dl></div>

                    <tfoot id='Y0VNJ'></tfoot>
                      <legend id='Y0VNJ'><style id='Y0VNJ'><dir id='Y0VNJ'><q id='Y0VNJ'></q></dir></style></legend>