QTableView按UserRole排序列数据

时间:2018-11-27 01:58:46

标签: python pyqt pyqt5 qtableview

我有一个QStandardItemModel通过管道传递到QTableView中。我模型中的列之一包含日期,这些日期具有用户友好的displayData和计算机友好的userData。因此,例如,一个QStandardItem可能显示一个类似于22 Nov 2018的字符串,但是userdata看起来类似于324586(自纪元以来的秒数)。但是,当我对列进行排序时,它当然会根据displayData进行排序。如何强制该表改为按userData排序?

1 个答案:

答案 0 :(得分:1)

您必须使用setSortRole()

from PyQt5 import QtCore, QtGui, QtWidgets
import random

DATECOLUMN = 1

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self._tableView = QtWidgets.QTableView()
        self.setCentralWidget(self._tableView)

        self._model = QtGui.QStandardItemModel(10, 4) 
        self._tableView.setModel(self._model)

        now_second = QtCore.QDateTime.currentDateTime().toSecsSinceEpoch()
        for i in range(self._model.rowCount()):
            for j in range(self._model.columnCount()):
                if j == DATECOLUMN:
                    t = QtCore.QDateTime.fromSecsSinceEpoch(random.randint(0, now_second))
                    text = t.toString("dd MMM yyyy")
                    it = QtGui.QStandardItem(text)
                    it.setData(t.toSecsSinceEpoch(), QtCore.Qt.UserRole)
                else:
                    it = QtGui.QStandardItem("{}-{}".format(i, j))
                self._model.setItem(i, j, it)

        self._model.setSortRole(QtCore.Qt.UserRole)
        self._model.sort(DATECOLUMN, QtCore.Qt.AscendingOrder)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

enter image description here

尽管我更喜欢直接保存QDateTime并使用委托以所需的格式显示数据。

from PyQt5 import QtCore, QtGui, QtWidgets
import random

DATECOLUMN = 1

class DateDelegate(QtWidgets.QStyledItemDelegate):
    def displayText(self, value, locale):
        return locale.toString(value, "dd MMM yyyy")

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self._tableView = QtWidgets.QTableView()
        self.setCentralWidget(self._tableView)

        self._model = QtGui.QStandardItemModel(10, 4) 
        self._tableView.setModel(self._model)
        delegate = DateDelegate(self._tableView)
        self._tableView.setItemDelegateForColumn(DATECOLUMN, delegate)

        now_second = QtCore.QDateTime.currentDateTime().toSecsSinceEpoch()
        for i in range(self._model.rowCount()):
            for j in range(self._model.columnCount()):
                if j == DATECOLUMN:
                    t = QtCore.QDateTime.fromSecsSinceEpoch(random.randint(0, now_second))
                    it = QtGui.QStandardItem()
                    it.setData(t, QtCore.Qt.DisplayRole)
                else:
                    it = QtGui.QStandardItem("{}-{}".format(i, j))
                self._model.setItem(i, j, it)

        self._model.sort(DATECOLUMN, QtCore.Qt.AscendingOrder)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

enter image description here