PyQt5基础学习-界面随着鼠标移动 1.QMouseEvent.globalPos(获得鼠标的位置) 2.QWidget.pos(当前界面的位置) 3.self.setCursor(QCursor(Qt.OpenHandCursor))(设置点击后鼠标的样子)

通过计算鼠标的位置和初始化位置来对选框进行移动操作

AbnormityWindowMove.py 

"""
实现不规则窗口的移动(异形窗口)

通过mask实现异形窗口

需要一张透明的png图, 透明部分被扣出, 形成一个非矩形区域
"""

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class AbnormityWindowMove(QWidget):
    def __init__(self):
        super(AbnormityWindowMove, self).__init__()
        self.setWindowTitle("异形窗口")
        self.pix = QBitmap("../picture/mask.png")
        self.resize(self.pix.size())
        self.setMask(self.pix)

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.m_drag = True

            self.m_DragPosition = event.globalPos() - self.pos() #self.pos()表示当前窗口的位置
            #设置点击以后设置小手指
            self.setCursor(QCursor(Qt.OpenHandCursor))
            # print(event.globalPos())
            # print(event.pos())
            # print(self.pos())
        if event.button() == Qt.RightButton:
            self.close()

    def mouseMoveEvent(self, QMouseEvent):
        if Qt.LeftButton and self.m_drag:
            # 当左键移动窗体修改偏移值
            # QPoint
            # 实时计算窗口左上角坐标
            print(self.m_DragPosition)
            #当前的鼠标位置-初始化的位置
            self.move(QMouseEvent.globalPos() - self.m_DragPosition)

    def mouseReleaseEvent(self, QMouseEvent):
        self.m_drag = False
        self.setCursor(QCursor(Qt.ArrowCursor))
    
    def paintEvent(self, event):
        painter = QPainter(self)
        painter.drawPixmap(0, 0, self.pix.width(), self.pix.height(),
                           QPixmap("../picture/screen1.jpg"))




if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = AbnormityWindowMove()
    form.show()
    sys.exit(app.exec_())

 

posted @ 2022-02-13 12:19  c语言我的最爱  阅读(1087)  评论(0编辑  收藏  举报