在QTableWidget中对十六进制数字进行排序的最佳方法

时间:2019-07-09 06:47:25

标签: python python-3.x pyside2

我想知道QTableWidget内部对十六进制数字进行排序的最佳方法。

当前,我要做的是转换所有十六进制值,然后对其进行排序(升/降)。但是我的问题是因为我确实将其转换为十进制。表中显示的值以十进制表示。我想对十六进制进行排序而不更改表中显示的值

这是我当前的实现方式:

from PySide2.QtCore import Qt
from PySide2.QtWidgets import QTableWidget

class NumericData(QTableWidgetItem):
    def __lt__(self, other):
        return (self.data(Qt.UserRole) < other.data(Qt.UserRole))

class Window(QTableWidget):
    def __init__(self):
        super(Window, self).__init__(4, 2)
        for column, values in enumerate((
            ('ABCD', '1DCA', 'BD23', 'FFFFFFFF'),
            (1,2,3,4)
            )):
            for row, value in enumerate(values):
                if column == 0:
                    value = str(int(value, 16))
                else:
                    value = str(value)
                item = NumericData(value)
                item.setData(Qt.UserRole, value)
                self.setItem(row, column, item)
        self.setSortingEnabled(True)
        self.sortItems(0, Qt.AscendingOrder)

这是样本表

|--Values--|
|ABCD      |
|1BCD      |
|DEC1      |
|123       |
|2105      |
|AAAAAAAA  |

1 个答案:

答案 0 :(得分:1)

不必使用新角色来保存转换,因为如果用户更改了该值,则必须重新计算该值,则只能传递角色Qt :: DisplayRole。

考虑到这一点,我实现了以下示例:

from PySide2 import QtCore, QtGui, QtWidgets


class HexDelegate(QtWidgets.QStyledItemDelegate):
    def createEditor(self, parent, option, index):
        editor = QtWidgets.QSpinBox(parent)
        editor.setMaximum(2147483647)
        editor.setMinimum(-2147483647 - 1)
        editor.setDisplayIntegerBase(16)
        fnt = editor.font()
        fnt.setCapitalization(QtGui.QFont.AllUppercase)
        editor.setFont(fnt)
        return editor

    def setEditorData(self, editor, index):
        editor.setValue(int(index.data(QtCore.Qt.DisplayRole), 16))

    def setModelData(self, editor, model, index):
        value = ('{:x}'.format(editor.value())).upper()
        model.setData(index, value, QtCore.Qt.DisplayRole)


class NumericData(QtWidgets.QTableWidgetItem):
    def __lt__(self, other):
        return int(self.data(QtCore.Qt.DisplayRole), 16) < int(
            other.data(QtCore.Qt.DisplayRole), 16
        )


class Window(QtWidgets.QTableWidget):
    def __init__(self):
        super(Window, self).__init__(4, 2)
        delegate = HexDelegate(self)
        self.setItemDelegateForColumn(0, delegate)
        data = ("ABCD", "1DCA", "BD23", "FFFF"), (1, 2, 3, 4)
        for row, values in enumerate(zip(*data)):
            for col, value in enumerate(values):
                it = QtWidgets.QTableWidgetItem()
                it.setData(QtCore.Qt.DisplayRole, value)
                self.setItem(row, col, it)
        self.setSortingEnabled(True)
        self.sortItems(0, QtCore.Qt.AscendingOrder)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Window()
    w.show()
    sys.exit(app.exec_())