Pyside按字母顺序排序QListView忽略区分大小写

时间:2016-08-26 17:35:40

标签: python pyside

有人可以告诉我如何对项目列表进行排序而忽略区分大小写吗?我在很多论坛上都在网上搜索过,其中大多数都是错误的语法,过于复杂或只是指向某些文档。我现在还找到了一个有效的解决方案,所以任何帮助都会很棒。

import sys
from PySide import QtGui, QtCore

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

        self.initUI()

    def initUI(self):
        self.uiItems = QtGui.QListView()
        self.uiItems.setAlternatingRowColors(True)
        self.uiItems.setModel(self.createModel(self))
        self.uiItems.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)

        grid = QtGui.QGridLayout()
        grid.setContentsMargins(0, 0, 0, 0)
        grid.addWidget(self.uiItems, 0, 0)
        self.setLayout(grid)

        self.setLayout(grid)

        # main layout
        main_widget = QtGui.QWidget()
        main_widget.setLayout(grid)
        self.setCentralWidget(main_widget)

        self.uiItems.doubleClicked.connect(self.doubleClickedItem)

        self.show()

    def doubleClickedItem(self, item):
        name = item.data(role=QtCore.Qt.DisplayRole)
        self.results = name
        self.accept()

    def createModel(self, parent):

        items = [
            'Cookie dough',
            'Hummus',
            'Spaghetti',
            'Dal makhani',
            'Chocolate whipped cream',
            'candles',
            'Apples',
            'zebra',
            'Oranges'
        ]

        model = QtGui.QStandardItemModel()

        for item in items:
            model.appendRow(QtGui.QStandardItem(item))
        return model


def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

0 个答案:

没有答案