QTableWidget images not drawing properly in MacOS
-
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:
However on MacOS, I get this:
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:
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_())
-
Hi,
What version of macOS are you running ?
-
Apologies for the delay. It’s Mac OS Mojave 10.14.6.
-
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.
-
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 ?
-
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 ?
@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