在对表进行排序时,如何保留项目的选择?在下面的示例中,选择始终固定到行索引,即如果我选择第一行,则排序后始终选择第一行,而不是我选择的实际行
import sys
from PySide2 import QtWidgets, QtGui, QtCore
from PySide2.QtCore import Qt
class TableModel(QtCore.QAbstractTableModel):
def __init__(self, data):
super().__init__()
self._data = data
def data(self, index, role):
if role == Qt.DisplayRole:
return self._data[index.row()][index.column()]
def sort(self, column, order):
if order == Qt.DescendingOrder:
rev = True
else:
rev = False
self.layoutAboutToBeChanged.emit()
self._data.sort(key=lambda x: x[column], reverse=rev)
self.layoutChanged.emit()
def rowCount(self, parent=None):
return len(self._data)
def columnCount(self, parent=None):
return len(self._data[0])
class Main(QtWidgets.QDialog):
def __init__(self, data):
super().__init__()
self.layout = QtWidgets.QVBoxLayout()
self.table = QtWidgets.QTableView()
self.table.setSortingEnabled(True)
self.table.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectionBehavior.SelectRows)
self.model = TableModel(data)
self.table.setModel(self.model)
self.layout.addWidget(self.table)
self.setLayout(self.layout)
def main():
app = QtWidgets.QApplication(sys.argv)
data = [
[1,2,3,4],
[5,6,7,8],
[6,5,4,3],
[2,1,0,9]
]
m = Main(data)
m.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
实现排序方法时,您正在修改数据,而视图无法知道项目的新位置,您必须使用QSortFilterProxyModel修改索引,而不是修改数据:
相关问题 更多 >
编程相关推荐