如何在Qt4及更高版本中对QTableView进行排序

时间:2016-05-20 04:30:03

标签: python qt model pyqt qabstracttablemodel

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 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_() 。列排序已启用。 当我单击列名称时(1,2或3没有任何反应)。 如何在不使用代理模型的情况下进行此排序?

public function add(){

    // Ambil data perusahaan dari tabel perusahaan
        //$this->Super_Model->get('t_mynetpoin');
        $data['nim'] =  $this->Super_Model->query('select nim from t_mynetpoin');
        // End of Ambil Data perusahaan
        $data['view']= 'v_super_admin/super_admin_template/v_sidebar';
        $data['view']= 'v_super_admin/Coin/v_form';
        $this->load->view('index',$data);

}

3 个答案:

答案 0 :(得分:2)

我不是python开发人员,但似乎模型中没有实现sort()方法。

在C ++中,当您从QAbstractItemModel(或QAbstractTableModel)派生自定义模型时,必须实现QAbstractItemModel::sort()方法才能启用排序。

答案 1 :(得分:1)

虽然@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_()

答案 2 :(得分:-3)

感谢Tomas的指导!

以下是PyQt中的工作解决方案:

enter image description here

;WITH cte AS(
                SELECT *
                FROM   [User]
                       UNPIVOT([Value] FOR Cols IN ([Name], [Gender])) Unp
                       PIVOT(MAX([Value]) FOR Id IN ([1], [2], [3])) Piv
            )
SELECT Cols  AS Id,
       [1],
       [2],
       [3]
FROM   cte
ORDER BY
       Id       DESC