[pyqt5]关于在pyqt5界面上鼠标位置问题

先上代码,主要看鼠标移动事件里面代码

import sys
from PyQt5 import QtCore
from PyQt5.QtCore import Qt, QPoint
from PyQt5.QtGui import QPainter, QPen, QPixmap, QPainterPath, QFont, QColor, QBrush
from PyQt5.QtWidgets import QApplication, QWidget
import numpy as np


class DemoMouseEvent(QWidget):
    def __init__(self, parent=None):
        super(DemoMouseEvent, self).__init__(parent)
        # 设置窗口标题
        self.setWindowTitle('鼠标事件演示')
        # 设置窗口大小
        self.setFixedSize(480, 320)

        self.beginPoint = QPoint()  # 起始点
        self.endPoint = QPoint()  # 结束点

        self.pixmap = QPixmap('d:\\67.jpg')
        #self.pixmap.fill(Qt.lightGray)
        self.copt_pixmap =self.pixmap.copy()
        self.setMouseTracking(True)
        self.cur_x = 0
        self.cur_y = 0


    def draw(self, painter):
        path = QPainterPath()
        f = QFont('黑体', 10)
        point = QPoint(20, 20)
        path.addText(point, f, "世界您好")
        point1 = QPoint(250, 250)
        point2 = QPoint(self.cur_x, self.cur_y)
        path.moveTo(point)
        path.lineTo(point1)
        path.lineTo(point2)
        path.lineTo(point)
        #path.addEllipse(point, 5, 5)
        painter.fillPath(path, QColor(0, 255, 0, 128))
        painter.drawPath(path)

    # 重绘窗口事件
    def paintEvent(self, event):
        self.pixmap=self.copt_pixmap.copy()
        pp = QPainter(self.pixmap)
        pp.setPen(QPen(Qt.blue, 2))  # 设置画笔
        self.draw(pp)
        # 绘制直线
        pp.drawLine(self.beginPoint, self.endPoint)
        # 上一直线的终点就是下一直线的起点
        self.beginPoint = self.endPoint

        # 在画布上画出
        painter = QPainter(self)
        painter.drawPixmap(0, 0, self.pixmap)


    def wheelEvent(self, ev):
        mods = ev.modifiers()
        # print('mods=', mods)
        delta = ev.angleDelta()
        # print('delta=', delta)
        if QtCore.Qt.ControlModifier == int(mods):
            if int(delta.y()) > 0:
                print("ctrl 向上滚轮")
            else:
                print("ctrl 向下滚轮")

    def mousePressEvent(self, event):
        # 鼠标左键按下
        if event.button() == Qt.LeftButton:
            self.startPoint = event.pos()

    def mouseReleaseEvent(self, event):
        # 鼠标左键释放
        if event.button() == Qt.LeftButton:
            self.endPoint = event.pos()
            # 重新绘制
            self.update()

    def mouseMoveEvent(self, event):
        tmp1 = event.y()
        tmp2 = event.localPos().y()
        tmp3 = event.pos().y()
        tmp4 = event.windowPos().y()

        tmp5 = event.y() + self.y()
        
        tmp6 = event.screenPos().y()
        tmp7 = event.globalPos().y()
        print("所有点的位置:")
        print(tmp1)
        print(tmp2)
        print(tmp3)
        print(tmp4)
        print(tmp5)
        print(tmp6)
        print(tmp7)
        # 鼠标左键按下的同时移动鼠标
        if event.buttons() and Qt.LeftButton:
            self.endPoint = event.pos()
            # 重新绘制
            self.update()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = DemoMouseEvent()
    window.show()
    sys.exit(app.exec())

通过运行代码可以发现:

tmp1 = event.y()
tmp2 = event.localPos().y()
tmp3 = event.pos().y()
tmp4 = event.windowPos().y()
上面四个是相对于父控件的坐标,搞编程都知道图像坐标是x轴是图像上面,y轴是图像左侧


tmp5 = event.y() + self.y()
上面这个是相对于窗口坐标
tmp6 = event.screenPos().y()
tmp7 = event.globalPos().y() 

上面这2行就是相当于屏幕左上角坐标了

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

FL1623863129

你的打赏是我写文章最大的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值