QTableView:按标题索引-1排序 [英] QTableView: sort by header index -1

查看:292
本文介绍了QTableView:按标题索引-1排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PyQt4,并且有一个带有2列数据的QTableView. 索引还有一个额外的列(它来自源模型的headerData函数).为了在单击标题按钮时进行排序,我用代理类包装了模型.

I am using PyQt4 and have a QTableView with a 2 columns data. There is an extra column for an index (it comes from the headerData function of the source model). For sorting when clicking on the header buttons I wrap my model with a proxy class.

这很好,但是我也想通过单击左上角的按钮(column number: "-1" I would say)按第一列进行排序:

That's working fine, but I also want to sort by the first column by clicking on the left-top corner button (column number: "-1" I would say):

根据要求,这是一个最小的示例:

As requested, here is a minimal example:

from PyQt4 import QtCore, QtGui
import random, sys

class MyModel(QtCore.QAbstractTableModel):

  headers = 'Name', 'Value'

  def __init__(self, parent=None):
    super(MyModel, self).__init__(parent)

    # Dummy data
    rows = []
    columns = []    
    for i in range(10):
      numbr = random.randint(1,20)
      rows.append('Name #%i' % numbr)
      columns.append('Value #%i' % numbr)
    self.data = zip(rows, columns)

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

  def rowCount(self, parent=QtCore.QModelIndex()): 
    return len(self.data)

  def data(self, index, role=QtCore.Qt.DisplayRole):
    if not index.isValid():
      return None

    if (index.row() >= len(self.data)) or (index.row() < 0):
      return None

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

    return None

  def headerData(self, section, orientation, role):
    if role == QtCore.Qt.DisplayRole :
        if orientation == QtCore.Qt.Horizontal :
            return self.headers[section]
        elif orientation == QtCore.Qt.Vertical :
            return section
    return None

class MyProxyModel(QtGui.QSortFilterProxyModel):
    def __init__(self):
      super(MyProxyModel, self).__init__()

    def lessThan(self, left_index, right_index):
      left_var = left_index.data(QtCore.Qt.EditRole)
      right_var = right_index.data(QtCore.Qt.EditRole)

      # Column #1 (values) should be not sortable
      if left_index.column() == 1: 
        return False

      return left_var < right_var

class MainWindow(QtGui.QMainWindow):
  def __init__(self):
    super(MainWindow, self).__init__()     

    self.model = MyModel()

    self.proxy = MyProxyModel()
    self.proxy.setSourceModel(self.model)
    self.proxy.setFilterKeyColumn(0)

    self.selectionModel = QtGui.QItemSelectionModel(self.proxy)

    self.tableView = QtGui.QTableView()
    self.tableView.setModel(self.proxy)    
    self.tableView.setSelectionModel(self.selectionModel)

    self.tableView.setSortingEnabled(True)
    self.tableView.sortByColumn(0, QtCore.Qt.AscendingOrder)    
    self.setCentralWidget(self.tableView) 

if __name__ == '__main__':
  app = QtGui.QApplication(sys.argv)
  window = MainWindow()
  window.show()
  sys.exit(app.exec_())

有人可以帮我吗?

推荐答案

首先,您必须能够获得cornerWidget,它是QAbstractButton,因为我们使用的是findChild.然后,我们使用sort(-1)使顺序无效并将其置于初始状态.

First you must be able to get the cornerWidget which is a QAbstractButton, for that we use findChild. Then we use sort(-1) that invalidates the ordering and places it in the initial state.

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        ...    
        self.setCentralWidget(self.tableView) 
        cornerButton = self.tableView.findChild(QtGui.QAbstractButton)
        cornerButton.clicked.connect(lambda: self.proxy.sort(-1))

这篇关于QTableView:按标题索引-1排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆