[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行就是相当于屏幕左上角坐标了

猜你喜欢

转载自blog.csdn.net/FL1623863129/article/details/131575893