<p>从<a href="http://doc-snapshot.qt-project.org/qdoc/qsortfilterproxymodel.html#sorting" rel="nofollow">latest official documentation</a>:</p>
<blockquote>
<p>Behind the scene, the view calls the sort() virtual function on the model to reorder the data in the model. To make your data sortable, you can either implement sort() in your model, or use a QSortFilterProxyModel to wrap your model QSortFilterProxyModel provides a generic sort() reimplementation that operates on the sortRole() (Qt::DisplayRole by default) of the items and that understands several data types, including int, QString, and QDateTime. For hierarchical models, sorting is applied recursively to all child items. String comparisons are case sensitive by default; this can be changed by setting the sortCaseSensitivity property.</p>
</blockquote>
<p>实现您自己的sort()也可以是一个选项,但是:</p>
<blockquote>
<p>An alternative approach to sorting is to disable sorting on the view and to impose a certain order to the user. This is done by explicitly calling sort() with the desired column and order as arguments on the QSortFilterProxyModel (or on the original model if it implements sort()). For example:</p>
</blockquote>
<pre><code> proxyModel->sort(2, Qt::AscendingOrder);
</code></pre>
<blockquote>
<p>QSortFilterProxyModel can be sorted by column -1, in which case it returns to the sort order of the underlying source model.</p>
</blockquote>
<p>可以按以下方式将-1传递给该方法:</p>
^{pr2}$
<p>请注意,您在那里粘贴的两行没有分号。由于语法错误,代码甚至不能像那样编译。最好粘贴正确的代码。在</p>
<p>如果它不起作用,就实现你的排序。在</p>