如何使 QStandardItemModel 中的图标居中?

How to center an icon in QStandardItemModel?

def data(self, index, role):
    if role == Qt.DecorationRole:
        if index.column() == 0:
            return QApplication.style().standardIcon(QStyle.SP_DialogYesButton if self.item(index.row()).valid else QStyle.SP_DialogNoButton)
    elif role == Qt.TextAlignmentRole:
        if index.column() == 0:
            return QVariant(Qt.AlignCenter | Qt.AlignVCenter)

图标显示良好,但未居中。

我找到了使用 QStyledItemDelegate 的解决方案。

    def paint(self, painter, option, index):
        QStyledItemDelegate.paint(self, painter, option, index)
        QApplication.style().standardIcon(QStyle.SP_DialogYesButton if index.data().valid else QStyle.SP_DialogNoButton).paint(painter, option.rect)