2

enter image description here

When this QTableView is created I want the sorting "arrow" indicator to be shown on the column at the middle. The arrow needs to be pointing down. How to achieve this?

from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])

class Model(QtCore.QAbstractTableModel):
    def __init__(self):
        QtCore.QAbstractTableModel.__init__(self)
        self.items = [[1, 'one', 'ONE'], [2, 'two', 'TWO'], [3, 'three', 'THREE']]

    def rowCount(self, parent=QtCore.QModelIndex()):
        return 3 
    def columnCount(self, parent=QtCore.QModelIndex()):
        return 3

    def data(self, index, role):
        if not index.isValid(): return 

        if role == QtCore.Qt.DisplayRole:
            return self.items[index.row()][index.column()]

tableModel=Model()
tableView=QtGui.QTableView() 
tableView.setModel(tableModel)
tableView.setSortingEnabled(True)

tableView.show()
app.exec_()
4

1 回答 1

1

您必须使用sortByColumn函数,使用Descending Order使箭头“指向下方”:

tableView.sortByColumn(1, QtCore.Qt.DescendingOrder)
于 2016-05-20T09:03:12.080 回答