このコードは単一のQTableView
. 列をクリックすると、列のソート方向を示す矢印が表示されます。tableView の項目自体をクリックすると、クリックされたインデックスが出力されます。tableView 項目をクリックすると、3 つの列 (ヘッダー) のうち現在のもの (矢印が表示されている列) と、並べ替えの矢印が指している方向 (上または下) を知りたいです。これを達成する方法は?
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_()