pyqt5   鼠标操作

 

  

#资料  http://blog.sina.com.cn/s/blog_6483fa330102xo6w.html


import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtGui import QCursor
from PyQt5.QtCore import Qt


class Demo(QWidget):

def __init__(self):
super(Demo, self).__init__()
self.label = QLabel('Hello World', self)
self.label1 = QLabel('喂 世界', self)
self.label1.move(0, 30)
self.label2 = QLabel('鼠标位置', self)
self.resize(500, 300)
self.label.resize(200, 20)
self.label1.resize(200, 20)
self.label2.resize(400, 20)
self.label2.move(0, 60)
self.label3 = QLabel('鼠标位置', self)
self.label3.resize(400, 20)
self.label3.move(0, 90)

self.setMouseTracking(True) # 设置鼠标移动跟踪是否有效
'''
设置为True时,只要鼠标在窗口内移动时mouseMoveEvent事件就能捕获
设置为False时(默认),只有鼠标键按下并在窗口内移动时mouseMoveEvent事件才能捕获
注意只能是QWidget,如果是QMainwindow,则无效
self.hasMouseTracking()返回设置的状态
'''

def mousePressEvent(self, event): # 鼠标键按下时调用(任意一个键,按一下调一次),这些方法是许多控件自带的,这里来自于QWidget。
self.label.setText('鼠标键按下了')
n = event.button() # 用来判断是哪个鼠标健触发了事件【返回值:0 1 2 4】
'''
QtCore.Qt.NoButton - 0 - 没有按下鼠标键
QtCore.Qt.LeftButton -1 -按下鼠标左键
QtCore.Qt.RightButton -2 -按下鼠标右键
QtCore.Qt.Mion 或 QtCore.Qt.MiddleButton -4 -按下鼠标中键
'''
nn = event.buttons() # 返回前面所列枚举值的组合,用于判断同时按下了哪些键【不知怎么判断】 <PyQt5.QtCore.Qt.MouseButtons object at 0x0000003326982F98>

def mouseReleaseEvent(self, event): #鼠标键释放时调用
      #参数1:鼠标的作用对象;参数2:鼠标事件对象,用来保存鼠标数据
self.label.setText('鼠标键放开了')

def mouseMoveEvent(self, event): # 鼠标移动事件
ret = self.hasMouseTracking() #返回鼠标MouseTracking的状态
     self.label1.setText('鼠标移动了:%s' % ret)
x = event.x() # 返回鼠标相对于窗口的x轴坐标
y = event.y() # 返回鼠标相对于窗口的y轴坐标
     self.label2.setText('鼠标x坐标:%s ,鼠标y坐标:%s' % (x, y))
        xy = event.pos()  #返回鼠标坐标 ,QPoint(463, 0) 相对于控件  【用xy.x()  xy.y()提取值】
s=event.localPos()
#返回鼠标坐标 相对于控件 QPointF(2.0, 2.0)
def wheelEvent(self, event):  # 滚轮滚动时调用。event是一个QWheelEvent对象
angle = event.angleDelta() # 返回滚轮转过的数值,单位为1/8度.PyQt5.QtCore.QPoint(0, 120)
angle = angle / 8 # 除以8之后单位为度。PyQt5.QtCore.QPoint(0, 15) 【向前滚是正数,向后滚是负数 用angle.y()取值】
ang = event.pixelDelta() # 返回滚轮转过的像素值 【不知为何 没有值】
# print(event.x(),event.y()) #返回鼠标相对于窗口的坐标
w = event.pos() # 返回相对于控件的当前鼠标位置.PyQt5.QtCore.QPoint(260, 173)
w1 = event.posF() # 返回相对于控件的当前鼠标位置.PyQt5.QtCore.QPointF(302.0, 108.0)
# 坐标函数与上面相同


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

 






鼠标形状对应图

pyqt5   鼠标操作

 

 QCursor     自定义鼠标:

 

from PyQt5.QtWidgets import QApplication, QWidget,QLabel,QPushButton
import sys
from PyQt5.QtGui import QCursor
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter, QPixmap

class win(QWidget): #创建一个类,为了集成控件
    def __init__(self):
        super(win, self).__init__()
        self.setWindowTitle('窗口标题')
        self.resize(400,300)
        self.setup_ui()#控件布局函数

    def setup_ui(self):  #控件布局
        label = QLabel('标签控件', self)
        button = QPushButton('按钮', self)
        label.move(10, 10)
        label.setStyleSheet('background-color: green')
        button.move(300, 10)

        pm=QPixmap('subiao.png')   #创建一张像素图纸
        pm1=pm.scaled(20,20)   #缩放pm图纸,赋值给图纸pm1
        #参数 图纸的宽高
        cursor=QCursor(pm1,0,0)    #创建鼠标对象
        #参数1 图纸
        #参数2 参数3 鼠标热点坐标;默认-1,图纸中心点;0,0图纸左上角
        label.setCursor(cursor)  # 设置鼠标形状

if __name__=='__main__':
    app=QApplication(sys.argv)  #创建应用
    window=win()
    window.show()
    sys.exit(app.exec_())

 

 unsetCursor()    重置形状-鼠标恢复默认

 

获取鼠标对象:

cursor() -> QCursor        获取鼠标对象

pm2=self.cursor()   #获取鼠标对象

s=pm2.pos()    #提取鼠标位置,相对于屏幕    QPoint(1184, 62)

pm2.setPos(0,0)  #设置鼠标位置

 

 

 

pyqt5   鼠标操作

 

相关文章:

  • 2022-01-05
  • 2022-12-23
  • 2021-05-10
  • 2022-01-31
  • 2021-06-27
  • 2022-01-20
  • 2021-12-30
  • 2022-12-23
猜你喜欢
  • 2022-01-04
  • 2021-11-11
  • 2021-10-09
  • 2021-05-25
  • 2021-09-10
  • 2022-01-15
相关资源
相似解决方案