如何在QStandardItemModel中居中图标?

时间:2015-03-03 13:23:48

标签: qt pyqt4 pyqt5

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)

图标显示良好但不居中。

1 个答案:

答案 0 :(得分:0)

我找到了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)