QTableView:如何按行索引(header索引-1)对TableView中的输入数据进行排序?

QTableView: How to sort input data in TableView by row index (header index -1)?

我正在与 QTableView 合作。我能够根据单个列 header 以升序和降序对数据进行排序。一旦我按列 header 对数据进行排序,我想再次根据原始索引对其进行排序。我尝试关注 但无法修复它。

代码:

import numpy as np
from qtpy import QtCore, QtGui, QtWidgets
from qtpy.QtCore import Qt, QAbstractTableModel
from qtpy.QtWidgets import QApplication, QTableView

data = np.random.random((10, 3))

app = QApplication([''])


class SimpleModel(QAbstractTableModel):

    headers = 'Col 1', 'Col 2', 'Col 3'

    def __init__(self, *args, **kwargs):
        super(SimpleModel, self).__init__(*args, **kwargs)
        table.setSortingEnabled(True)
        self.order = np.arange(data.shape[0])
        self.layoutChanged.emit()

    def rowCount(self, index=None):
        return 10

    def columnCount(self, index=None):
        return 3

    def data(self, index, role):
        if role == Qt.DisplayRole:
            col = index.column()
            row = index.row()
            return str(data[self.order[row], col])

    def sort(self, column, ascending):
        self.order = np.argsort(data[:, column])
        if ascending == Qt.DescendingOrder:
            self.order = self.order[::-1]
        self.layoutChanged.emit()

    def headerData(self, section, orientation, role):
        if role == QtCore.Qt.DisplayRole :
            if orientation == QtCore.Qt.Horizontal :
                return self.headers[section]
            elif orientation == QtCore.Qt.Vertical :
                return section
        return None


table = QTableView()
table.setModel(SimpleModel())
table.show()

app.exec_()

截图:

谢谢。

问题是sort方法对数据进行排序,而且转换是不可逆的,之前的解决方案使用了一个代理,这个排序的是视觉部分而不是数据,所以最后在另一个解决方案的情况下它足以避免重新排序并显示原始数据。

import numpy as np
from qtpy import QtCore, QtGui, QtWidgets

data = np.random.random((10, 3))*10 -9

app = QtWidgets.QApplication([''])


class SimpleModel(QtCore.QAbstractTableModel):

    headers = 'Col 1', 'Col 2', 'Col 3'

    def __init__(self, *args, **kwargs):
        super(SimpleModel, self).__init__(*args, **kwargs)
        table.setSortingEnabled(True)
        self.order = np.arange(data.shape[0])
        self.layoutChanged.emit()

    def rowCount(self, index=None):
        return 10

    def columnCount(self, index=None):
        return 3

    def data(self, index, role=QtCore.Qt.DisplayRole):
        if role == QtCore.Qt.DisplayRole:
            col = index.column()
            row = index.row()
            return str(data[self.order[row], col])

    def headerData(self, section, orientation, role):
        if role == QtCore.Qt.DisplayRole :
            if orientation == QtCore.Qt.Horizontal :
                return self.headers[section]
            elif orientation == QtCore.Qt.Vertical :
                return section
        return None

class SortProxyModel(QtCore.QSortFilterProxyModel):
    def lessThan(self, left_index, right_index):
        left_var = self.sourceModel().data(left_index)
        right_var = self.sourceModel().data(right_index)
        return float(left_var) < float(right_var)


table = QtWidgets.QTableView()
model = SimpleModel()
proxy = SortProxyModel()
proxy.setSourceModel(model)
table.setModel(proxy)
table.show()
cornerButton = table.findChild(QtWidgets.QAbstractButton)
cornerButton.clicked.connect(lambda: proxy.sort(-1))
app.exec_()