网站首页 > 博客文章 正文
鼠标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度。例如:
angle=event.angleDelta( ) /8
angleX=angle.x()
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( )通常配合使用。
鼠标事件测试
测试程序中,使用鼠标演示了在窗口中绘制连续线,完整代码如下:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt, QEvent, QPoint
from PyQt5.QtGui import QPainter, QPen, QPixmap
from PyQt5.QtWidgets import QApplication, QWidget
class DemoMouseEvent(QWidget):
def __init__(self, parent=None):
super(DemoMouseEvent, self).__init__(parent)
# 设置窗口标题
self.setWindowTitle('实战PyQt5: 鼠标事件演示')
# 设置窗口大小
self.setFixedSize(480, 320)
self.beginPoint = QPoint() #起始点
self.endPoint = QPoint() #结束点
self.pixmap = QPixmap(self.rect().size())
self.pixmap.fill(Qt.lightGray)
#重绘窗口事件
def paintEvent(self, event):
pp = QPainter(self.pixmap)
pp.setPen(QPen(Qt.blue, 2)) #设置画笔
#绘制直线
pp.drawLine(self.beginPoint, self.endPoint)
#上一直线的终点就是下一直线的起点
self.beginPoint = self.endPoint
#在画布上画出
painter = QPainter(self)
painter.drawPixmap(0, 0, self.pixmap)
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):
#鼠标左键按下的同时移动鼠标
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())
运行结果如下图:
本文知识点
- 了解各种鼠标事件;
- 鼠标指针形状控制;
- 使用QPainter,QPixmap在窗口中绘制连续折线。
喜欢本文内容就关注, 收藏,点赞,评论和转发。
猜你喜欢
- 2024-09-29 Qt的6个简单小案例(qt官方例子)
- 2024-09-29 Qt入门阶段之事件(qt入门阶段之事件是什么)
- 2024-09-29 Qt编写安防视频监控系统9-自动隐藏光标
- 2024-09-29 Qt为什么站稳c++GUI的top1(qt quuid)
- 2024-09-29 Qt5 事件(event)机制详解(qt事件是通过什么实现的)
- 2024-09-29 Qt 保持GUI响应的几种方法(qt保存快捷键)
- 2024-09-29 Qt如何模拟鼠标点击?(qt 模拟鼠标点击)
- 2024-09-29 Qt事件的接受与忽略(qt菜单栏按钮点击事件)
- 2024-09-29 「VTK」「DICOM」修改后的VTK sample code
- 2024-09-29 Qt事件(qt事件与信号槽)
你 发表评论:
欢迎- 最近发表
-
- 给3D Slicer添加Python第三方插件库
- Python自动化——pytest常用插件详解
- Pycharm下安装MicroPython Tools插件(ESP32开发板)
- IntelliJ IDEA 2025.1.3 发布(idea 2020)
- IDEA+Continue插件+DeepSeek:开发者效率飙升的「三体组合」!
- Cursor:提升Python开发效率的必备IDE及插件安装指南
- 日本旅行时想借厕所、买香烟怎么办?便利商店里能解决大问题!
- 11天!日本史上最长黄金周来了!旅游万金句总结!
- 北川景子&DAIGO缘定1.11 召开记者会宣布结婚
- PIKO‘PPAP’ 洗脑歌登上美国告示牌
- 标签列表
-
- ifneq (61)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)