Apple Retina 显示屏上的 QGraphicsItem 渲染

QGraphicsItem rendering on Apple Retina Display

我正在为我的 PyQt5 应用程序添加对 Apple Retina 显示屏的支持。虽然我成功地渲染了高分辨率图标(通过将 @2x 后缀添加到我所有的 .png 文件并在我的 QApplication 中设置 Qt.AA_UseHighDpiPixmaps ), 我在 QGraphicsScene + QGraphicsView.

中渲染高分辨率 QGraphicsItem 时遇到了一些麻烦

在我的应用程序中,除了加载 .png 文件外,我还自己生成了几个 QPixmap(将它们嵌入到 Icon 中),以构建用户的符号调色板可用于向 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

在我的调色板中生成一个符号(钻石那个)。

然而,当我向我的 QGraphicsScene 添加元素时,它们以 QGraphicsView 显示,它们以低分辨率呈现:

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)

形状内的文本呈现正确,我没有自己绘制它,因为它是一个 QGraphicsTextItem,我的 QGraphicsItem 作为父对象。

问题是,对于 QPixmap 我可以设置设备像素比,对于 QGraphicsItem 我不能。我错过了什么吗?

我正在使用针对 Qt 5.5.1 和 SIP 4.18 构建的 PyQt 5.5.1(不使用 5.6,因为我在应用程序启动时遇到了几次崩溃,我已经向 PyQt 开发人员报告过)。

可能不是您想听到的,但是 Qt added retina support in 5.6

我也在努力解决 PyQt 5.7 中的类似问题。

如果您正在编写 QGraphicsItem 的子 class,请尝试在 paint() 方法中将渲染提示设置为抗锯齿:

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)

请注意,这可能不是最佳或正确答案。