python - 在 QAbstractItemModel 中不区分大小写排序

标签 python sorting pyqt4 qtableview qabstracttablemodel

我在尝试使用 QAbstractItemModel 创建自己的排序函数时遇到问题。它可以工作,但不区分大小写。我尝试过使用 QSortFilterProxyModel,但没有成功。我的排序功能:

def sort(self, col, order):
    self.emit(SIGNAL("layoutAboutToBeChanged()"))
    self.tableData = sorted(self.tableData, key=operator.itemgetter(col))       
    if order == Qt.AscendingOrder:
        self.tableData.reverse()
    self.emit(SIGNAL("layoutChanged()"))

我正在使用QTableView。我怎样才能使其不区分大小写?

完整示例:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import operator
import sys

class Window(QWidget):
    def __init__(self):
        super(Window, self).__init__()

        header = ["one", "two"]
        tableDict = [["abcdef", "tuvwx"], ["aBcde", "TUvWx"], ["acdef","tUvWX"], ["Acdef", "TUVwx"], ["ACdef", "TUVwx"]]
        self.myTable = newTableModel(header, tableDict)

        mainLayout = QHBoxLayout()
        mainLayout.addWidget(self.myTable.tableView)
        self.setLayout(mainLayout)
        self.setWindowTitle("Test table")

class newTableModel(QAbstractTableModel): 
    def __init__(self, header, data, parent=None, *args):
        super(newTableModel, self).__init__(parent)
        self.tableView = QTableView()
        self.tableData = data
        self.header = header

        self.tableView.setShowGrid(True)
        self.tableView.setFrameStyle( QFrame.NoFrame )
        self.tableView.setFocusPolicy( Qt.NoFocus )
        self.tableView.setSelectionMode( QAbstractItemView.NoSelection )

        vHeader = self.tableView.verticalHeader()
        vHeader.setVisible(False)
        vHeader.setStretchLastSection(False)
        hHeader = self.tableView.horizontalHeader()
        hHeader.setVisible(True)
        hHeader.setStretchLastSection(False)
        self.tableView.setSortingEnabled(True)

        self.tableView.setModel(self)
        self.tableView.resizeRowsToContents()
        self.tableView.resizeColumnsToContents()
        vHeader.setResizeMode(QHeaderView.ResizeToContents)

        self.tableView.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
        self.tableView.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

    def rowCount(self, parent): 
        return len(self.tableData) 

    def columnCount(self, parent): 
        return len(self.tableData[0]) 

    def data(self, index, role=Qt.DisplayRole):
        row = index.row()
        col = index.column()
        if role == Qt.DisplayRole:
            return "{0}".format(self.tableData[row][col])
        return None

    def setData(self, index, value, role):
        if index.isValid():
             return True
        return False

    def flags(self, index):
        fl = QAbstractTableModel.flags(self, index)
        if index.column() == 0:
            fl |= Qt.ItemIsUserCheckable
        return fl

    def headerData(self, col, orientation, role):
        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
            return self.header[col]

    def sort(self, col, order):
        self.emit(SIGNAL("layoutAboutToBeChanged()"))
        self.tableData = sorted(self.tableData, key=operator.itemgetter(col))      
        if order == Qt.AscendingOrder:
            self.tableData.reverse()
        self.emit(SIGNAL("layoutChanged()"))

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

示例中的此表数据显示排序 - 不区分大小写。

最佳答案

您只需将传递给 sorted 函数的排序键中的值转换为小写(或大写)即可。为了提高效率,您还可以使用reverse参数来避免在单独的步骤中执行此操作:

def sort(self, col, order):
    self.layoutAboutToBeChanged.emit()
    self.tableData = sorted(
        self.tableData,
        key=lambda row: row[col].lower(),
        reverse=(order != Qt.AscendingOrder),
        )
    self.layoutChanged.emit()

请注意,sorted 进行稳定排序,因此相等的值(在应用键之后)将保留其原始位置。因此,示例中的第二列在排序时不会显示任何更改,因为这些值都是“相同”(如果忽略大小写)。

更新:

这是一个适用于字符串和数字的解决方案。它假设列不是两种类型的混合:

def sort(self, col, order):
    if self.tableData and self.tableData[0]:
        self.layoutAboutToBeChanged.emit()
        if isinstance(self.tableData[0][col], str):
            sortkey = lambda row: row[col].lower()
        else:
            sortkey = operator.itemgetter(col)
        self.tableData = sorted(
            self.tableData, key=sortkey,
            reverse=(order != Qt.AscendingOrder))
        self.layoutChanged.emit()

关于python - 在 QAbstractItemModel 中不区分大小写排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50322830/

相关文章:

python多处理特有的内存管理

python - Ansible 和 jinja - 将双引号转义的 JSON 字符串发送到 AWS

javascript - aa数据表中的排序无法正常工作

java - java实现QuickSort的一些问题

python - 如何在 pyqt 中嵌入 matplotlib - For Dummies

python - PyQt4 @pyqtSlot : what is the result kwarg for?

python - 将元组转换为索引?

python - SQLAlchemy 混合表达式访问不同的行

php - mysql 中的穆迪评级排序

python - 如何在 pyqt 中同时运行 2 个线程?