jkariukidev
jkariukidev

Reputation: 23

PyQt TableView align Icons to Center

I have a QTableView with icons and these icons by default display aligned to the left while I want to align them to the center. From the Qt Documentation the alignment only occurs for the TextAlignment under Qt.role only which has the role of Qt.DisplayRole().

How can the alingment be set to center for icons which have Qt.DecorationRole().

enter image description here

Upvotes: 1

Views: 1158

Answers (1)

eyllanesc
eyllanesc

Reputation: 243975

One possible solution is to use a delegate:

class IconDelegate(QtWidgets.QStyledItemDelegate):
    def initStyleOption(self, option, index):
        super().initStyleOption(option, index)
        option.decorationSize = option.rect.size()
delegate = IconDelegate(tableview)
tableview.setItemDelegate(delegate)

Upvotes: 1

Related Questions