Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Impossible to display an Icon in a QtTableView

Impossible to display an Icon in a QtTableView

Scheduled Pinned Locked Moved Unsolved Qt for Python
8 Posts 4 Posters 1.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    faldo1
    wrote on last edited by
    #1

    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)
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      F 1 Reply Last reply
      1
      • SGaistS SGaist

        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.

        F Offline
        F Offline
        faldo1
        wrote on last edited by
        #3

        @SGaist Hello,

        Thanks, I will try

        is it possible to have multiple icons in a column, depending on multiple states of each row ?

        For example

        • last payment too late (yes/no)
        • important customer (yes / no)

        Aso,

        Thanks

        JonBJ SGaistS 2 Replies Last reply
        0
        • F faldo1

          @SGaist Hello,

          Thanks, I will try

          is it possible to have multiple icons in a column, depending on multiple states of each row ?

          For example

          • last payment too late (yes/no)
          • important customer (yes / no)

          Aso,

          Thanks

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @faldo1 You would need your own delegate for that. Whether multiple icons for multiple columns maps nicely to display in a single column is a different matter.

          1 Reply Last reply
          0
          • F faldo1

            @SGaist Hello,

            Thanks, I will try

            is it possible to have multiple icons in a column, depending on multiple states of each row ?

            For example

            • last payment too late (yes/no)
            • important customer (yes / no)

            Aso,

            Thanks

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @faldo1 how many icon combinations would you have ?

            From what you described, as @JonB wrote, a custom item delegate will likely be the solution. But a simple one, just the paint event that retrieves the data and then paint the various icons.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            F 1 Reply Last reply
            0
            • SGaistS SGaist

              @faldo1 how many icon combinations would you have ?

              From what you described, as @JonB wrote, a custom item delegate will likely be the solution. But a simple one, just the paint event that retrieves the data and then paint the various icons.

              F Offline
              F Offline
              faldo1
              wrote on last edited by faldo1
              #6

              @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

              M SGaistS 2 Replies Last reply
              0
              • F faldo1

                @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

                M Offline
                M Offline
                mpergand
                wrote on last edited by
                #7

                @faldo1
                Have a look HERE

                1 Reply Last reply
                0
                • F faldo1

                  @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

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @faldo1 Are you sure the file was loaded successfully ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved