【发布时间】: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