5

enter image description here

This code creates a single QTableView. Clicking the column displays the arrow that indicates the direction of column sorting. Clicking the tableView's item itself prints out the clicked index. When the tableView item is clicked I want to know what of three columns (headers) is current (the column with the arrow displayed) and what direction the sorting arrow is pointing: up or 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 in [QtCore.Qt.DisplayRole, QtCore.Qt.EditRole]:
            return self.items[index.row()][index.column()]

def onClick(index):
    print 'clicked index:  %s'%index

def sortIndicatorChanged(column=None, sortOrder=None):
    print 'sortIndicatorChanged: column: %s, sortOrder: %s'%(column, sortOrder)

tableModel=Model()
tableView=QtGui.QTableView() 
tableView.setModel(tableModel)
tableView.setSortingEnabled(True)
tableView.clicked.connect(onClick)
tableView.horizontalHeader().sortIndicatorChanged.connect(sortIndicatorChanged)
tableView.show()

app.exec_()
alphanumeric
  • 17,967
  • 64
  • 244
  • 392

0 Answers0