QgraphicsPixMapiTem缩放入口/输出

发布于 2025-01-29 12:26:22 字数 2657 浏览 0 评论 0 原文

我的应用程序是用于绘制形状(多边形,点..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())

My application is for drawing shapes (polygons, points ..etc), I used QGrapichView and QGrapichScene and I added two different pixmap items (one for drawing, and the other one is 'legend' that will show the user how much the distance is), So far so good.

I implemented the zoom functionality and it is working fun. Still, my problem is: that when I zoom in/out the whole scene (the canvas QGraphicsPixmapItem and the legend QGraphicsPixmapItem) is affected and that is expected because I am re-scaling the QGrapichView while zooming (using the mouse wheel).

What I want is: I need only the canvas item to be zoomed in/out not the whole scene so that the legend will always be visible to the user.

before zooming in, you can see the legend in the blue text

after zooming in, now you can't see the legend.

Here is a snippet of the code that am using in zooming from this answer:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文