简体   繁体   English

在QAbstractItemModel中不区分大小写地排序

[英]Sorting case insensitively in QAbstractItemModel

I have trouble with trying create my own sorting function with QAbstractItemModel. 我在尝试使用QAbstractItemModel创建自己的排序功能时遇到了麻烦。 It works but not case insensitive. 它可以工作,但不区分大小写。 I have tried to use QSortFilterProxyModel, but any success. 我尝试使用QSortFilterProxyModel,但没有成功。 My sort function: 我的排序功能:

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

I am using QTableView. 我正在使用QTableView。 How I could make it case insensitive? 如何使它不区分大小写?

Full example: 完整示例:

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

This table data in example shows sorting - non case sensitive. 示例中的此表数据显示排序-不区分大小写。

You simply need to convert the values to lower (or upper) case in the sort-key that is passed to the sorted function. 您只需要在传递给已sorted函数的排序键中将值转换为小写(或大写)即可。 For improved efficiency, you can also use the reverse argument to avoid doing that in a separate step: 为了提高效率,您还可以使用反向参数来避免在单独的步骤中执行此操作:

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

Note that sorted does a stable sort , so equal values (after the key has been applied), will keep their original places. 请注意, sorted会进行稳定排序 ,因此相等的值(在应用键之后)将保留其原始位置。 Thus, the second column in your example won't show any changes when it's sorted, since the values are all the "same" (if you ignore case). 因此,示例中的第二列在排序时不会显示任何更改,因为值都是“相同”的(如果忽略大小写)。

UPDATE : 更新

Here's a solution that will work for strings and numbers. 这是一个适用于字符串和数字的解决方案。 It assumes the columns aren't a mixture of the two types: 假定列不是两种类型的混合:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM