Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableWidget images not drawing properly in MacOS

QTableWidget images not drawing properly in MacOS

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 435 Views
  • 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.
  • D Offline
    D Offline
    donquibeats
    wrote on last edited by donquibeats
    #1

    In my PyQt5 app (using Qt 5.13.1), there are several places where I have QTableWidgets where images (PixImage) will be added programatically after the table has already been shown to the user.

    This is working absolutely fine on Windows, but it's very broken on MacOS. I don't know if I'm doing something wrong, or if it's a bug.

    A self-contained (but slightly messy) code sample to reproduce the issue is at the bottom of this post. It requires a file "files/testcard_f.jpg" to be present, but any image file should do.

    On Windows, I get this, as expected:
    Windows working example

    However on MacOS, I get this:
    MacOS example, not working

    When the table is first drawn, no images are shown- but when I click on a cell, the image for that cell suddenly appears.

    And when I try to scroll the window on MacOS, things get even worse:
    Second MacOS example, not working

    The closest equivalent to this problem I found was this: https://bugreports.qt.io/browse/QTBUG-70599 but it claims to have been fixed a year ago in 5.12.0, but I'm having this problem on 5.13.1. It also states that calling repaint() on the viewport() of the QTableWidget fixes the problem- and for me, that didn't help at all, it made no difference.

    Please can somebody tell me if they can reproduce this issue on MacOS, and whether I can do anything about it or whether it's a bug?

    import sys, os
    from PyQt5.QtWidgets import QApplication, QGridLayout, QComboBox, QFrame, QDialog, QVBoxLayout, QTableWidget, \
        QTableWidgetItem
    from PyQt5.QtCore import Qt
    from PyQt5.QtGui import QPixmap
    from PIL.ImageQt import ImageQt
    from PIL import Image
    from pathlib import Path
    import qdarkstyle
    
    
    def get_piximage_from_image_filepath(thumbnailPath, thumbnailWidth, thumbnailHeight):
        try:
            thumbnailAsImage = Image.open(thumbnailPath)
        except Exception as e:
            print("ERROR: PixImage-LoadFail.", "e:", e)
            return False
        if thumbnailAsImage is None:
            return False
        thumbnailAsImage_rgb = thumbnailAsImage.convert("RGBA")
        if thumbnailAsImage_rgb is None:
            return False
        thumbnailAsData = thumbnailAsImage_rgb.tobytes("raw", "RGBA")
        if thumbnailAsData is None:
            return False
        qpixImage = ImageQt(thumbnailAsImage)
        if qpixImage is None:
            return False
        pixImage = QPixmap.fromImage(qpixImage).scaled(thumbnailWidth, thumbnailHeight)
        return pixImage
    
    
    class App_MacImageTableTest(QDialog):
        def __init__(self):
            super().__init__()
            self.title = 'PyQt5 table test'
            self.layout = QGridLayout(self)
    
            self.tableWidget = QTableWidget()
            self.tableWidget.setColumnCount(10)
            self.cellArray = {}
    
            rowCount = 50
            columnCount = 10
            imageWidth = 120
            imageHeight = 90
    
            pixImage = get_piximage_from_image_filepath(
                os.path.join(Path(__file__).parent.parent, "files", "testcard_f.jpg"), imageWidth, imageHeight)
    
            for row in range(0, rowCount + 1):
                numRows = self.tableWidget.rowCount()
                self.tableWidget.insertRow(numRows)
    
                self.cellArray[numRows] = {}
                for column in range(0, columnCount + 1):
                    self.cellArray[numRows][column] = QTableWidgetItem()
                    self.cellArray[numRows][column].setText(str(row) + "-" + str(column))
                    self.tableWidget.setItem(numRows, column, self.cellArray[numRows][column])
                    self.tableWidget.setColumnWidth(column, imageWidth)
                self.tableWidget.setRowHeight(numRows, imageHeight)
    
            self.layout.addWidget(self.tableWidget, 0, 0)
            self.show()
    
            for row in range(0, rowCount + 1):
                for column in range(0, columnCount + 1):
                    self.cellArray[row][column].setData(Qt.DecorationRole, pixImage)
                    print("row:", row, "column:", column, "pixImage:", pixImage)
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        # app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
        ex = App_MacImageTableTest()
        sys.exit(app.exec_())
    
    1 Reply Last reply
    1
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What version of macOS are you running ?

      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
      • D Offline
        D Offline
        donquibeats
        wrote on last edited by
        #3

        Apologies for the delay. It’s Mac OS Mojave 10.14.6.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          donquibeats
          wrote on last edited by
          #4

          Apologies for bumping up this unanswered thread, but it's possible it got buried in the pre-Christmas excitement. I don't suppose anybody has been able to replicate this problem, have they? It's still a major stumbling block for the MacOS side of my development.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Sorry, I lost track. Are you using a special image type since you are using pillow to open them ? By the way, why not use QPixmap directly to open the image files ?

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

            D 1 Reply Last reply
            1
            • SGaistS SGaist

              Sorry, I lost track. Are you using a special image type since you are using pillow to open them ? By the way, why not use QPixmap directly to open the image files ?

              D Offline
              D Offline
              donquibeats
              wrote on last edited by
              #6

              @SGaist Thanks for the response.

              The example I gave was a reduction of what's happening in my larger application, where use of Pillow is for other reasons as well, not applicable to this example.

              As far as I'm aware the images are not of a special type. The testcard image, which I downloaded just for the sake of this test, was (if I remember correctly) from here: https://upload.wikimedia.org/wikipedia/en/5/52/Testcard_F.jpg

              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