Impossible to display an Icon in a QtTableView
-
Hello all
My tableView is created at application level
I create a manager for that tableView
The column with index 1 should display an Icon depending on the state
But impossible, the column remains blank
Why ?
I tried png, jpg, 64px height, 24px height
Here is my code
Thanks
import sys from PyQt6 import QtCore, QtWidgets from PyQt6.QtCore import Qt from PyQt6.QtGui import QIcon class IconColumnDelegate(QtWidgets.QStyledItemDelegate): def __init__(self, parent=None): self.ioIconWarning = QIcon("../gfx/icons/warning24.jpg") QtWidgets.QStyledItemDelegate.__init__(self, parent) def createEditor(self, parent, option, index): """ Important, otherwise an editor is created if the user clicks in this cell. """ return None def paint(self, painter, option, index): if index.column() == 1: rect = option.rect self.ioIconWarning.paint(painter, rect, Qt.AlignmentFlag.AlignHCenter) return super().paint(painter, option, index) class DirectoryScanFilesTableViewModel(QtCore.QAbstractTableModel): aoColumnTitles = ["Name", "Tags", "Size", "Path"] def __init__(self, data): super(DirectoryScanFilesTableViewModel, self).__init__() self._data = data def headerData(self, anSection, aoOrientation, aoRole): if aoOrientation == Qt.Orientation.Horizontal and aoRole == Qt.ItemDataRole.DisplayRole: return self.aoColumnTitles[anSection] def data(self, index, role): if role == Qt.ItemDataRole.DisplayRole: # See below for the nested-list data structure. # .row() indexes into the outer list, # .column() indexes into the sub-list return self._data[index.row()][index.column()] def rowCount(self, index): # The length of the outer list. return len(self._data) def columnCount(self, index): # The following takes the first sub-list, and returns # the length (only works if all rows are an equal length) return len(self._data[0]) class DirectoryScanFilesTableViewManager: ioModel = None ioDirectoryScanTableView = None ioIconColumnDelegate = None def __init__(self, aoDirectoryScanTableView): self.ioDirectoryScanTableView = aoDirectoryScanTableView self.ioIconColumnDelegate = IconColumnDelegate() self.ioDirectoryScanTableView.setItemDelegateForColumn(1, self.ioIconColumnDelegate) def SetData(self, aoData): self.ioModel = DirectoryScanFilesTableViewModel(aoData) self.ioDirectoryScanTableView.setModel(self.ioModel) self.ioDirectoryScanTableView.setColumnWidth(0, 200) self.ioDirectoryScanTableView.setColumnWidth(1, 200) self.ioDirectoryScanTableView.setColumnWidth(2, 200) self.ioDirectoryScanTableView.horizontalHeader().setStretchLastSection(True)
-
Hi,
I don't have a computer at hand to test but you might be complicating your life a bit.
You can use the DecorationRole and return the icon you want there, no need for a delegate.
Same goes for keeping the column read only, you can do it in the model's flags method.
-
-
@SGaist Hello, Sgaist,
I have 4 distinct values I would like to combine in 4 on/off icons in a kind of "Status" column
I tried a simple delegate but it didn't work, the column is empty.
That's why I created this post.
I don't know if it can help you, but I remember
-
the breakpoint in the delegate paint() function was reached, the size of rect was the size of the target cell in the TableView
-
but the self.ioIconWarning.availableSizes() returned no value.
Best regards,
Dany
-