pyqt5 实现图像显示、缩放,平移

该博客介绍了一种使用PyQt5库中的QGraphicsView和QGraphicsPixmapItem实现图像显示、缩放和平移的方法。通过创建自定义的ImageWidget类,实现了图像在窗口中的适应性缩放、鼠标事件处理(包括点击、滚动和移动)以及双击重置图像大小的功能。此外,还提供了一个ImageLabel类用于封装图像显示空间,支持接收图像数据并显示。

在这里只贴出图像的相关代码,基于QGraphicsPixmapItem实现:

# -*- coding: utf-8 -*-
from PyQt5.QtCore import Qt, QPoint, QRectF, pyqtSignal, QObject
from PyQt5.QtGui import QPainter, QPixmap
from PyQt5.QtWidgets import QGraphicsPixmapItem, QStyleOptionGraphicsItem, QWidget, QGraphicsItem

from util.ImageUtil import MyImage


class ImageWidget(QGraphicsPixmapItem):
    # 影像在横轴偏移量
    w_offset = 0
    # 影像在纵轴的偏移量
    h_offset = 0
    # 鼠标当前所在的像素位置
    w_cur_pos = 0
    h_cur_pos = 0

    def __init__(self, pixmap: QPixmap, signal_pixel_selected):
        super(ImageWidget, self).__init__(pixmap)
        self.pixmap = pixmap
        self.setAcceptDrops(True)
        self.m_scaleValue = 1
        self.m_scaleDafault = 1
        self.m_isMove = False
        self.m_startPos = None
        self.setAcceptHoverEvents(True)
        self.signal_pixel_selected = signal_pixel_selected

    def boundingRect(self):
        self.w_offset = int(self.pixmap.width() / 2)
        self.h_offset = int(self.pixmap.height() / 2)
        return QRectF(-self.w_offset, -self.h_offset,
                      self.pixmap.width(), self.pixmap.height())

    def paint(self, painter: QPainter, const: QStyleOptionGraphicsItem, widget: QWidget):
        self.setOffset(-self.w_offset, -self.h_offset)
        painte
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZH_CS

随缘吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值