【问题标题】:QGraphicsItem rendering on Apple Retina DisplayQGraphicsItem 在 Apple Retina 显示屏上的渲染
【发布时间】:2016-10-24 00:55:31
【问题描述】:

我正在向我的 PyQt5 应用程序添加对 Apple Retina Display 的支持。虽然我成功地渲染了高分辨率图标(通过将 @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 pyqt retina-display qgraphicsview qgraphicsitem


    【解决方案1】:

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

    【讨论】:

    • 我想我错过了这篇博文,尽管我一直在寻找有关 Qt 5.5/5.6 中高 DPI 支持的新闻。我想我只需要等待 PyQt 5.6 中的修复。
    • 即使您的回答没有解决我的问题(因为如果不升级到最新版本的 Qt 显然无法解决),我也会为您的努力提供奖励。
    【解决方案2】:

    我也在 PyQt 5.7 中遇到类似的问题。

    如果您正在编写 QGraphicsItem 的子类,请尝试在 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)
    

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

    【讨论】:

      猜你喜欢
      • 2013-03-14
      • 1970-01-01
      • 1970-01-01
      • 2016-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多