Byte Insight
Byte Insight

Reputation: 1154

Pyside6 QAbstractTableModel - Replace Boolean value with centre aligned icon

I have a class GenericTableModel(QAbstractTableModel): in which I am trying to replace Boolean <class 'numpy.bool_'> with icons. I have had it working using the following:

if role == Qt.DecorationRole:
    value = self.dataframe.iloc[index.row(), index.column()]
    if hasattr(value, 'dtype') and value.dtype == bool:
        if value:
            return QIcon('static/icons/check-circle-fill.png')
        return QIcon("static/icons/icon_x.png")

But they were left aligned and I'd like them centred.

If I change the role to QDisplayRole then no icons are displayed because the value type at this time is always str. I guess this is because I am misunderstanding the role.

I have added the following from enter link description here and been able to set the icon size but not the alignment as it is not clearly specified in the answer given.

class IconDelegate(QStyledItemDelegate):
def initStyleOption(self, option, index):
    super().initStyleOption(option, index)
    option.decorationSize = QSize(16, 16) # option.rect.size()

Looking at the docs I'm expecting maybe a displayAlignment or textAlignment but they don't appear to be valid.

I suspect its based on a two prong misundertanding. I think DecorationRole is always intended to decorate the left side of a cell and if I can get the right role it might work?

Anyway, if someone could offer me some assistance I'd be grateful. Thanks

Upvotes: 0

Views: 33

Answers (0)

Related Questions