当前位置:   article > 正文

实战PyQt5: 098-鼠标事件

实战PyQt5: 098-鼠标事件

鼠标GUI程序中另外一种重要的交互方式,Qt中的鼠标事件包括鼠标点击、松开鼠标按键,移动鼠标到指定区域或者离开特定区域,更改鼠标指针的形状等等。

鼠标点击释放

对鼠标最常见的操作就是点击和释放,进行点击释放操作,将调用以下方法:

  • mousePressEvent (self, event) : 鼠标键按下时调用;
  • mouseReleaseEvent (self, event) : 鼠标键松开时调用;
  • mouseDoubieCiickEvent (self, event) - 双击鼠标时调用。

必须注意在双击之前的其他事件。双击时的事件顺序为:mouseButtonPress --> mouseButtonRelease --> mouseButtonDblClick --> mouseButtonPress --> mouseButtonRelease。

QApplicaption类的setDoubleClickInterval()方法可设置双击的时间间隔;doubleClickInterval( )方法返回双击的时间间隔。

event参数是QMouseEvent对象,存储事件的其他信息。常用方法有:

  • x() 和 y():返回相对于控件空间的鼠标坐标值;
  • pos(): 返回相对于控件空间的QPoint对象;
  • localPos():返回相对于控件空间的QPointF对象;
  • globalX() 和 globalY(): 返回相对于屏幕的x,y 坐标值;
  • globalPos(): 返回相对于屏幕的QPoint对象;
  • windowPos(): 返回相对于窗口的QPointF对象;
  • screenPos() : 返回相对于屏幕的QPointF对象;
  • button(): 返回以下枚举值(只列举了部分),用以判断是哪个鼠标键触发了事件;
    • Qt.NoButton (0): 没有按下鼠标键。例如移动鼠标时的button()返回值;
    • Qt.LeftButton (1): 按下鼠标左键;
    • Qt.RightButton (2): 按下鼠标右键;
    • t.Mion 或 Qt.MiddleButton (4): 按下鼠标中键。
  • buttons(): 返回前面所列枚举值的组合,用于判断同时按下了哪些键;
  • modifiers(): 判断按下了哪些修饰键(Shift,Ctrl , Alt,等等);
  • timestamp() : 返回事件发生的时间。

如果要让父控件继续收到鼠标事件,要调用事件的ignore()方法;否则,调用accept()。如果一个控件的Qt.WA_NoMousePropagation的属性设为True,则不会将事件传递给父控件。调用setAttribute( )方法可修改此参数:

button.setAttribute (Qt.WA_NoMousePropagation, True)

缺省情况下,鼠标事件只拦截控件区域中的鼠标操作。如果可拦截控件区域以外的鼠标事件,必须调用grabMouse()方法;释放时,调用releaseMouse()。

鼠标移动

重载mouseMoveEvent(self, event), 可以处理鼠标指针的移动。在缺省情况下,只有按下鼠标键并移动时,才会调用mouseMoveEvent()。 如果要处理一般情况下的鼠标移动(比如未按下鼠标键), 需要调用setMouseTracking(True)。如果要处理窗口中鼠标移动的事件,需要调用grabMouse()。

event对象中的pos()函数返回值为相对于控件的坐标,如果要转换成相对于父控件或者屏幕坐标,需要调用QWidget类的以下方法:

  • mapToGlobal (point): 将窗口坐标转换成屏幕坐标;
  • mapFromGlobal(point): 将屏幕坐标转换成窗口坐标;
  • mapToParent(point): 将窗口坐标转换成父窗口坐标。如果没有父窗口,则相当于mapToGlobal(point);
  • mapFromParent(point) - 将父窗口坐标转换成窗口坐标。如果没有父窗口,则相当于mapFromGlobal(point);
  • mapTo (widget, point) - 将窗口坐标转换成 widget父窗口坐标;
  • mapFrom (widget, point) - 将 widget父窗口坐标转换成窗口坐标。

鼠标移入和移出控件

鼠标指针移进和移出控件时,下列方法将被调用:

  • enterEvent (self, event):鼠标进入控件;
  • leaveEvent (self, event) :鼠标离开控件。

参数event为QEvent对象。

鼠标滚轮操作

鼠标滚轮操作事件由wheelEvent(self, event)方法来处理。参数event是一个QWheelEvent对象,它包含滚轮操作的相关信息。有以下常用方法可使用:

  • angleDelta( ): 返回QPoint对象,为滚轮转过的数值,单位为1/8度。例如:
  1.          angle=event.angleDelta( ) /8
  2.          angleX=angle.x()
  3.          angleY=angle.y()
  • pixelDeita (): 返回QPoint对象,为滚轮转过的像素值;
  • x() 和 y(): 返回相对于控件的当前鼠标的x,y位置;
  • pos() :返回相对于控件的当前鼠标位置的QPoint对象;
  • posF() :返回相对于控件的当前鼠标位置的QPoinFt对象;
  • globalX() 和globalY(): 返回相对于屏幕的当前鼠标的x,y位置;
  • globalPos(): 返回相对于屏幕的当前鼠标QPoint位置;
  • globalPosF(): 返回相对于屏幕的当前鼠标QPointF位置;
  • buttons():返回按下了哪些鼠标键,同‘鼠标点击释放’中buttons()。
  • modifiers():判断按下了哪些修饰键(Shift,Ctrl , Alt,等等);
  • timestamp():返回事件发生的时间。

如果要让父控件继续收到滚轮事件,要调用事件的ignore()方法;否则,调用accept()。

鼠标指针形状

QWidget类中以下方法设置和管理鼠标指针:

  • setCursor(qcr): 参数qcr为QCursor对象,在Qt.CursorShape中内建一些鼠标指针形状,如:
    • ArrowCursor: 标准箭头;
    • upArrowCursor: 向上箭头;
    • CrossCursor: 十字光标;
    • Waitcursor: 沙漏;

等常用鼠标形状。

  • unsetCursor() :取消设置的鼠标形状;
  • cursor(): 返回当前鼠标形状的QCursor对象;

使用QApplication类中的以下静态方法来控制整个应用程序的鼠标形状:

  • setOverrideCursor(qcr) - 参数qcr为QCursor对象或 Qt.CursorShape的枚举值;
  • restoreOverrideCursor() - 取消全局鼠标形状设置;
  • hangeOverrideCursor(qcr) - 将鼠标形状设置为qcr。只有先调用setOverrideCursor()了,该函数才起作用;
  • overrideCursor( ): 返回当前鼠标形状的QCursor 对象。

setOverrideCursor()和restoreOverrideCursor( )通常配合使用。

鼠标事件测试

测试程序中,使用鼠标演示了在窗口中绘制连续线,完整代码如下:

  1. import sys
  2. from PyQt5 import QtCore, QtGui, QtWidgets
  3. from PyQt5.QtCore import Qt, QEvent, QPoint
  4. from PyQt5.QtGui import QPainter, QPen, QPixmap
  5. from PyQt5.QtWidgets import QApplication, QWidget
  6.  
  7. class DemoMouseEvent(QWidget):
  8.     def __init__(self, parent=None):
  9.         super(DemoMouseEvent, self).__init__(parent)
  10.  
  11.         # 设置窗口标题
  12.         self.setWindowTitle('实战PyQt5: 鼠标事件演示')
  13.         # 设置窗口大小
  14.         self.setFixedSize(480320)
  15.         
  16.         self.beginPoint = QPoint() #起始点
  17.         self.endPoint = QPoint()   #结束点
  18.         
  19.         self.pixmap = QPixmap(self.rect().size())
  20.         self.pixmap.fill(Qt.lightGray)
  21.         
  22.     #重绘窗口事件
  23.     def paintEvent(self, event):           
  24.         pp = QPainter(self.pixmap)
  25.         pp.setPen(QPen(Qt.blue, 2))    #设置画笔
  26.         
  27.         #绘制直线
  28.         pp.drawLine(self.beginPoint, self.endPoint)
  29.         #上一直线的终点就是下一直线的起点
  30.         self.beginPoint = self.endPoint
  31.         
  32.         #在画布上画出
  33.         painter = QPainter(self)
  34.         painter.drawPixmap(00, self.pixmap)
  35.         
  36.     def mousePressEvent(self, event):
  37.         #鼠标左键按下
  38.         if event.button() == Qt.LeftButton:
  39.             self.startPoint = event.pos()
  40.       
  41.     def mouseReleaseEvent(self, event):
  42.         #鼠标左键释放
  43.         if event.button() == Qt.LeftButton:
  44.             self.endPoint = event.pos()
  45.             #重新绘制
  46.             self.update()
  47.             
  48.     def mouseMoveEvent(self, event):
  49.         #鼠标左键按下的同时移动鼠标
  50.         if event.buttons() and Qt.LeftButton:
  51.            self.endPoint = event.pos()
  52.            #重新绘制
  53.            self.update() 
  54.          
  55. if __name__ == '__main__':
  56.     app = QApplication(sys.argv)
  57.     window = DemoMouseEvent()
  58.     window.show()
  59.     sys.exit(app.exec())

运行结果如下图:

实战PyQt5: 098-鼠标事件

测试鼠标事件

本文知识点

  • 了解各种鼠标事件;
  • 鼠标指针形状控制;
  • 使用QPainter,QPixmap在窗口中绘制连续折线。

前一篇: 实战PyQt5: 097-键盘事件

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/887827
推荐阅读
相关标签
  

闽ICP备14008679号