2016-05-20 4 views
2

enter image description hereQt4をでQTableViewをソートし

コードまで単一QTableViewを作成する方法。列のソートが有効になりました。 列名をクリックすると(何も起こらない1,2,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 flags(self, index): 
     return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsEditable 

    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): 
    tableView.openPersistentEditor(tableView.model().index(index.row(), index.column())) 
    print 'clicked index: %s'%index 

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

tableView.show() 
app.exec_() 
+0

なぜプロキシモデルを使用しませんか? QSortFilterProxyModelは方法です。 –

+0

'QSortFilterProxyModel'は、sourceModelのインデックスを再マッピングするので、かなり複雑になります。特にデリゲートが使用されている場合は、データを追跡することが困難になります。 – alphanumeric

答えて

2

私は、Pythonの開発者ではないけど、あなたのモデルに実装何sort()方法がないと思われます。

C++では、カスタムモデルをQAbstractItemModel(またはQAbstractTableModel)から派生させるときは、並べ替えを有効にするためにQAbstractItemModel::sort()メソッドを実装する必要があります。

-3

おかげさまで、以下は

のPyQtで実用的なソリューションです:

enter image description here

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 flags(self, index): 
     return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsEditable 

    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 sort(self, column=None, direction=None): 
     print 'Model.sort: column: %s, direction: %s'%(column, direction) 


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

tableView.show() 
app.exec_() 
+0

これはまったく並べ替えを行いません – johnson

0

@alphanumericがProxyModelを使用する必要はありませんが、それは、コードの唯一の2つの追加の行を必要とするので、多分他の誰かが、ありません:

from PySide 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 flags(self, index): 
     return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsEditable 

    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()] 


tableModel=Model() 
tableView=QtGui.QTableView() 

proxyModel = QtGui.QSortFilterProxyModel() 
proxyModel.setSourceModel(tableModel) 

tableView.setModel(proxyModel) 
tableView.setSortingEnabled(True) 

tableView.show() 
app.exec_() 
関連する問題