QGraphicsItem rendering on an Apple Retina display

I am adding Apple Retina Display support to the PyQt5 application. Although I successfully deleted high-resolution images (adding the @ 2x suffix to all my .png files and installing Qt.AA_UseHighDpiPixmaps in my QApplication ), I am having some problems rendering high-resolution QGraphicsItem in QGraphicsScene + QGraphicsView .

In my application, in addition to loading .png files, I also create several QPixmap my self (embedding them in Icon ) to create a character palette that the user can use to add new shapes to the diagram displayed in QGraphicsView , QGraphicsView .:

 def icon(cls, width, height, **kwargs): """ Returns an icon of this item suitable for the palette. :type width: int :type height: int :rtype: QIcon """ icon = QIcon() for i in (1.0, 2.0): # CREATE THE PIXMAP pixmap = QPixmap(width * i, height * i) pixmap.setDevicePixelRatio(i) pixmap.fill(Qt.transparent) # PAINT THE SHAPE polygon = cls.createPolygon(46, 34) painter = QPainter(pixmap) painter.setRenderHint(QPainter.Antialiasing) painter.setPen(QPen(QColor(0, 0, 0), 1.1, Qt.SolidLine)) painter.setBrush(QColor(252, 252, 252)) painter.translate(width / 2, height / 2) painter.drawPolygon(polygon) # PAINT THE TEXT INSIDE THE SHAPE painter.setFont(Font('Arial', 11, Font.Light)) painter.drawText(polygon.boundingRect(), Qt.AlignCenter, 'role') painter.end() # ADD THE PIXMAP TO THE ICON icon.addPixmap(pixmap) return icon 

Create one of the symbols of my palette (diamond).

Eddy palette

However, when I add items to my QGraphicsScene displayed in the QGraphicsView , they are displayed in low resolution:

 def paint(self, painter, option, widget=None): """ Paint the node in the diagram. :type painter: QPainter :type option: QStyleOptionGraphicsItem :type widget: QWidget """ painter.setPen(self.pen) painter.setBrush(self.brush) painter.drawPolygon(self.polygon) 

Eddy diagram

The text inside the form is displayed correctly, and I do not draw it myself, since it QGraphicsTextItem has my QGraphicsItem as the parent.

The problem is that while for QPixmap I can set the pixel ratio of the device, for QGraphicsItem I can not. Did I miss something?

I use PyQt 5.5.1, built against Qt 5.5.1 and SIP 4.18 (I do not use 5.6, since I experience several crashes when starting the application, which I already reported to PyQt developers).

+5
source share
2 answers

Probably not what you wanted to hear, but Qt added retina support in 5.6 .

0
source

I am also struggling with a similar problem in PyQt 5.7.

If you are writing your subclass QGraphicsItem, try setting the rendering hint to antialiases in the paint () method:

 def paint(self, painter, option, widget=None): """ Paint the node in the diagram. :type painter: QPainter :type option: QStyleOptionGraphicsItem :type widget: QWidget """ painter.setPen(self.pen) painter.setBrush(self.brush) painter.setRenderHint(QPainter.Antialiasing) # <-- add this line painter.drawPolygon(self.polygon) 

Please note: this may not be the best or correct answer.

-1
source

All Articles