QgraphicsPixMapiTem缩放入口/输出
我的应用程序是用于绘制形状(多边形,点..Etc),我使用了qgrapichview和Qgrapichscene,我添加了两个不同的pixmap项目(一个用于绘图,另一个是'gragend',它将向用户显示用户的距离是多少), 到目前为止,一切都很好。
我实现了缩放功能,并且很有趣。尽管如此,我的问题是:,当我放大/缩小整个场景时(canvas qgraphicspixmapitem和Legend Qgraphicspixmapitem)会受到影响,这是可以预期的,因为我正在重新缩放QgrapichView(使用Qgrapichview(使用)(鼠标车轮)。
我想要的是:我只需要缩放/输出的画布项目而不是整个场景,以便用户总是可以看到传奇。
这是在Zooming 从这个答案:
class PixmapScene(QGraphicsScene):
pass
class Canvas(QGraphicsView):
def __init__(self, scene):
super().__init__(scene)
self.scene = scene
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
background_color = QColor("#443b36")
self.pixmap_item: QGraphicsItem = self.scene.addPixmap(QPixmap(780, 580))
self.pixmap_item.setTransformationMode(Qt.FastTransformation)
self.pixmap_item.pixmap().fill(background_color)
self.legend = QPixmap(780, 580)
self.legend.fill(QColor("#00ffffff"))
p = QtGui.QPainter(self.legend)
p.setPen(QPen(QColor("#0000FF"), 4))
p.drawLine(35 * 5.1, self.legend.height() - 60, 35 * 8.9, self.legend.height() - 60)
p.setPen(QColor("#c9c9c9"))
p.setFont(QFont('Century Gothic', 14))
p.drawText(35 * 5.5, self.legend.height() - 35, f'this text is from the other pixmap (legend)')
p.end()
self.scene.addPixmap(self.legend)
self.zoom_times = 0
def wheelEvent(self, event):
zoom_in_factor = 1.25
zoom_out_factor = 1 / zoom_in_factor
# Save the scene pos
old_pos = self.mapToScene(event.pos())
# Zoom
if event.angleDelta().y() > 0:
if self.zoom_times == 6:
return
zoom_factor = zoom_in_factor
self.zoom_times += 1
else:
if self.zoom_times == 0:
return
zoom_factor = zoom_out_factor
self.zoom_times -= 1
# here we are scaling the whole scene, what I want is zooming with keeping legend as it is
self.scale(zoom_factor, zoom_factor)
# Get the new position
new_pos = self.mapToScene(event.pos())
# Move scene to old position
delta = new_pos - old_pos
self.translate(delta.x(), delta.y())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论