【pyqt5】鼠标控制 窗口图片的缩放和移动

本篇博客实现的功能:

一、在main Windows的控件中显示 离线地图

二、可以通过鼠标对该图进行缩放和拖动

(ps:需要显示的图片是离线的,不需要调用cv的库)

代码简单,主要记录思路,下面是具体实现步骤

①设置控件间的父子层继承关系

(这很重要,层设置的不对,图片将无法显示)

本次设置的关系是:main Windows -< QFrame.Box-<label_map-<photo

②实现图片的显示

使用QPixmap

    def map_show(self):  # 显示
        self.img = QPixmap('map.jpg')
        self.label_map.setPixmap(self.img)
        self.label_map.setFrameShape(QFrame.Box)
        self.label_map.setLineWidth(1)  # 设置外线宽度
        self.label_w = self.label_map.width()
        self.label_x = self.label_map.x()
        self.label_y = self.label_map.y()
        self.label_h = self.label_map.height()
        # print(self.label_x,self.label_h)
        self.label_map.setScaledContents(True)  # 设置label内部图片自适应填充

③设置鼠标按下和释放事件

注意要设置标志位

左键点击移动,右键点击还原图片

    def mousePressEvent(self, e):  # 鼠标按下事件
        if e.buttons() == Qt.LeftButton:
            self.flag = True
        if e.buttons() == Qt.RightButton:
            print('545')
            self.label_x = 0
            self.label_y = 0
            self.label_w = 861
            self.label_h = 610
            self.label_map.setGeometry(QRect(self.label_x, self.label_y, self.label_w, self.label_h))

    def mouseReleaseEvent(self, e):  # 鼠标释放事件重写
        self.flag = False
        self.flag_2 = False
        self.movex = ""
        self.movey = ""

④ 图片移动

注意要调用全局变量的坐标值,不然会影响缩放后再进行移动的效果

    def mouseMoveEvent(self, e):
        if self.flag:
            self.x1 = e.x()
            self.y1 = e.y()
        if self.movex != "" and self.movey != "":
            self.label_x = self.label_x + (self.x1 - self.movex)
            self.label_y = self.label_y + (self.y1 - self.movey)
            print(self.label_x)
        self.movex = self.x1
        self.movey = self.y1
        # self.label_map.setGeometry(QtCore.QRect(0, 0, 861, 601))

        self.label_map.setGeometry(QtCore.QRect(self.label_x, self.label_y, self.label_map.width(), self.label_map.height()))  #使得图片能够随着缩放而不影响大小,同时QtCore.QRect确定图形为矩形

⑤ 图片缩放

放大倍数为15

    def wheelEvent(self,e):  # 缩放
        self.angle = e.angleDelta()/8
        self.angleY = self.angle.y()
        if self.angleY > 0:
            x_1 = e.x()
            y_1 = e.y()
            new_w = self.label_map.width() + 15
            new_h = self.label_map.height() + 15
            new_x = x_1 - (self.label_map.width() * (x_1 - self.label_map.x())) / (self.label_map.width())
            new_y = y_1 - (self.label_map.height()  * (y_1 - self.label_map.y())) / (self.label_map.height())
            self.label_map.setGeometry(QRect(new_x, new_y, new_w, new_h ))
            print(new_x,new_y,new_w,new_h)

        elif self.angleY < 0:
            print('222')
            x_1 = e.x()
            y_1 = e.y()
            self.new_w = self.label_map.width() - 15
            self.new_h = self.label_map.height() - 15
            self.new_x = x_1 - (self.label_map.width() * (x_1 - self.label_map.x())) / (self.label_map.width())
            self.new_y = y_1 - (self.label_map.height()  * (y_1 - self.label_map.y())) / (self.label_map.height() )
            self.label_map.setGeometry(QRect(self.new_x, self.new_y, self.new_w, self.new_h ))
            print(self.new_x,self.new_y,self.new_w,self.new_h)
  • 0
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值