修复 QTreeWidget 上的选定项目荧光笔

Fix selected item highlighter on QTreeWidget

我有一个 QTreeWidget,里面有值,我只想显示一定数量的小数,但为了计算目的保持精度。我让它正常工作,但所选项目的突出显示混乱,并在已绘制的单元格周围显示白色。

如何修复突出显示,使整行都显示为纯蓝色?

class InitialDelegate(QtWidgets.QItemDelegate):
    'Changes number of decimal places in gas analysis self.chosen table'
    def __init__(self, decimals, parent=None):
        super().__init__(parent)
        self.nDecimals = decimals
    def paint(self, painter, option, index):
        if index.column() == 1:
            value = index.model().data(index, QtCore.Qt.DisplayRole)
            try:
                number = float(value)
                painter.drawText(option.rect, QtCore.Qt.AlignCenter , "{:.{}f}".format(number, self.nDecimals))
            except:
                QtWidgets.QItemDelegate.paint(self, painter, option, index)
        else:
            QtWidgets.QItemDelegate.paint(self, painter, option, index)

这是它产生的结果:

我在实施过程中有 2 个观察结果:

  • 如果您只想更改文本显示的格式,那么您不应该覆盖 paint() method since it not only paints the text but also the background, icons, etc. The drawDisplay() 方法必须被覆盖。

  • 如果您只想将更改应用于列,最好使用 setItemDelegateForColumn() 方法设置委托

综合以上,则解法为:

from PyQt5 import QtCore, QtGui, QtWidgets


class InitialDelegate(QtWidgets.QItemDelegate):
    "Changes number of decimal places in gas analysis self.chosen table"

    def __init__(self, decimals, parent=None):
        super().__init__(parent)
        self.nDecimals = decimals

    def drawDisplay(self, painter, option, rect, text):
        option.displayAlignment = QtCore.Qt.AlignCenter
        try:
            number = float(text)
            text = "{:.{}f}".format(number, self.nDecimals)
        except:
            pass
        super().drawDisplay(painter, option, rect, text)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = QtWidgets.QTreeWidget(columnCount=3)
    delegate = InitialDelegate(2, w)
    w.setItemDelegateForColumn(1, delegate) # <---
    w.setHeaderLabels(["Gas Component", "Molecular Weight", "Mol%"])
    it = QtWidgets.QTreeWidgetItem(["Hexane", "86.1777", ""])
    w.addTopLevelItem(it)
    w.show()
    sys.exit(app.exec_())

加上:

如果你想做同样的事情但使用 QStyledItemDelegate then the solution is override initStyleOption():

class InitialDelegate(QtWidgets.QStyledItemDelegate):
    "Changes number of decimal places in gas analysis self.chosen table"

    def __init__(self, decimals, parent=None):
        super().__init__(parent)
        self.nDecimals = decimals

    def initStyleOption(self, option, index):
        super().initStyleOption(option, index)
        option.displayAlignment = QtCore.Qt.AlignCenter
        try:
            text = index.model().data(index, QtCore.Qt.DisplayRole)
            number = float(text)
            option.text = "{:.{}f}".format(number, self.nDecimals)
        except:
            pass