重定义鼠标响应+键盘响应事件

一,每个事件都被封装成相应的类:

pyqt中,每个事件类型都被封装成相应的事件类,如鼠标事件为QMouseEvent,键盘事件为QKeyEvent等。而它们的基类是QEvent。

二,基类QEvent的几个重要方法:

accept() 表示事件已处理,不需要向父窗口传播

ignore()表示事件未处理,继续向父窗口传播f

type()返回事件类型,如QtCore.QEvent.MouseButtonPress,一般由基事件调用。因为其它事件已经知道自己的事件类型了。

还有一个自定义事件的注册方法。

三,QMouseEvent鼠标事件:

buttons()  返回哪个鼠标按键被按住了。如Qt.LeftButton

globalPos()  返回鼠标相对屏幕的位置QPoint

pos()  返回鼠标相对处理事件的窗口的位置

四、处理鼠标事件的响应函数(在QWidget及其继承类中):

mousePressEvent(QMouseEvent)  #鼠标点击触发事件  

mouseReleaseEvent(event)  #鼠标释放触发事件

mouseMoveEvent(event)  #鼠标移动触发事件

# 事件。
"""重写鼠标事件,实现窗口拖动。"""
def mousePressEvent(self, event):
if event.buttons() == Qt.LeftButton:
self.setCursor(Qt.OpenHandCursor)
self.parent.m_drag = True
self.parent.m_DragPosition = event.globalPos()-self.parent.pos()
event.accept() def mouseMoveEvent(self, event):
try:
if event.buttons() and Qt.LeftButton:
self.parent.move(event.globalPos()-self.parent.m_DragPosition)#move将窗口移动到指定位置
event.accept()
except AttributeError:
pass def mouseReleaseEvent(self, event): if event.button()==Qt.LeftButton:
self.m_drag = False
self.unsetCursor()

效果如下:

重新定义鼠标事件:

"""重定义鼠标单击事件"""
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.lab1.setText("鼠标左键点击!")
# print(event.pos().x(),event.pos().y())
if event.button() == Qt.RightButton:
self.lab1.setText("鼠标右键点击!") """当鼠标左键点击拖动时触发事件,有无if判断条件效果都一样"""
def mouseMoveEvent(self, event):
# if event.buttons() == Qt.LeftButton:
# # print(type(event.pos().x())) #<class 'int'>
# self.lab2.setText(str(event.pos().x())+","+str(event.pos().y()))
self.pos = event.pos()
print(self.pos)
self.lab2.setText(str(event.pos().x()) + "," + str(event.pos().y()))
self.update()

完整代码:

 from PyQt5.QtCore import Qt
from PyQt5.QtGui import (QPainter, QColor, QPen)
import sys
from PyQt5.QtWidgets import (QApplication,QWidget,QLabel) class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUi()
#默认情况下禁用鼠标跟踪, 如果启用鼠标跟踪,即使没有按钮被按下,小部件也会接收鼠标移动事件。
#当然你也可以不写,只需要在执行的过程中按照鼠标左键也行
self.setMouseTracking(True) def initUi(self):
self.setGeometry(400,300,400,300)
self.setWindowTitle("键盘响应事件")
self.lab1 = QLabel("方向",self)
self.lab1.setGeometry(200,150,100,100)
self.lab2 = QLabel("显示鼠标坐标", self)
self.lab2.setGeometry(200, 80, 100, 100) """重定义键盘事件"""
def keyPressEvent(self,e ):
if e.key() == Qt.Key_Up:
self.lab1.setText("↑")
elif e.key() == Qt.Key_Down:
self.lab1.setText("↓")
elif e.key() == Qt.Key_Left:
self.lab1.setText("←")
else:
self.lab1.setText("→") """重定义鼠标单击事件"""
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.lab1.setText("鼠标左键点击!")
# print(event.pos().x(),event.pos().y())
if event.button() == Qt.RightButton:
self.lab1.setText("鼠标右键点击!") """当鼠标左键点击拖动时触发事件,有无if判断条件效果都一样"""
def mouseMoveEvent(self, event):
# if event.buttons() == Qt.LeftButton:
# # print(type(event.pos().x())) #<class 'int'>
# self.lab2.setText(str(event.pos().x())+","+str(event.pos().y()))
self.pos = event.pos()
print(self.pos)
self.lab2.setText(str(event.pos().x()) + "," + str(event.pos().y()))
self.update() if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())

重定义鼠标响应+键盘响应

所有的QT键盘事件代码如下:

https://pan.baidu.com/s/1Brry6fkUcxaP-uOdukD8Ng

【PyQt5-Qt Designer】鼠标+键盘事件的更多相关文章

  1. QT 11 鼠标键盘事件添加

    鼠标事件 void mousePressEvent(QMouseEvent *event); //单击 void mouseReleaseEvent(QMouseEvent *event); //释放 ...

  2. 【转】Qt鼠标键盘事件

    http://blog.csdn.net/lovebird_27/article/details/50351336 Qt 程序需要在main()函数创建一个QCoreApplication对象,然后调 ...

  3. Linux 模拟 鼠标 键盘 事件

    /************************************************************************ * Linux 模拟 鼠标 键盘 事件 * 说明: ...

  4. Python——pyHook监听鼠标键盘事件

    pyHook包为Windows中的全局鼠标和键盘事件提供回调. 底层C库报告的信息包括事件的时间,事件发生的窗口名称,事件的值,任何键盘修饰符等. 而正常工作需要pythoncom等操作系统的API的 ...

  5. Python - selenium_WebDriver 鼠标键盘事件

    from selenium import webdriver #引入ActionChains类 提供了鼠标的操作方法 from selenium.webdriver.common.action_cha ...

  6. Tkinter 鼠标键盘事件(一)

    一: 鼠标事件 <Button-1>                                                                     鼠标左键单击 ...

  7. Selenium4.0+Python3系列(四) - 常见元素操作(含鼠标键盘事件)

    一.写在前面 上篇文章介绍的是关于浏览器的常见操作,接下来,我们将继续分享关于元素的常见操作,建议收藏.转发! 二.元素的状态 在操作元素之前,我们需要了解元素的常见状态. 1.常见元素状态判断,傻傻 ...

  8. Qt中的键盘事件,以及焦点的设置(比较详细)

    Qt键盘事件属于Qt事件系统,所以事件系统中所有规则对按键事件都有效.下面关注点在按键特有的部分: focus 一个拥有焦点(focus)的QWidget才可以接受键盘事件.有输入焦点的窗口是活动窗口 ...

  9. 由chrome剪贴板问题研究到了js模拟鼠标键盘事件

    写在前面 最近公司在搞浏览器兼容的事情,所有浏览器兼容的问题不得不一个人包了.下面来说一下今天遇到的一个问题吧 大家都知道IE下面如果要获得剪贴板里面的信息的话,代码应该如下所示 window.cli ...

随机推荐

  1. URLEncoder 和URLDecoder

    通常在字符串的编码转换上,可以使用这两个类: public static void main(String[] args) { String str = "你好吗?我很好!"; t ...

  2. SQL group BY 合并字段用逗号隔开

    1.关联多表后 根据某个字段作为分组条件,其他合并到新列中,效果如下图 --------> 代码: ),KOrderID) , , '') from VOrder2 tb group by KU ...

  3. Sass的安装(windows 10)

    1.下载ruby 下载地址:https://rubyinstaller.org/downloads/ 如果觉得下载速度过慢,可以在我的百度去下载: 链接:https://pan.baidu.com/s ...

  4. CSS让页面平滑滚动

    我们以往实现平滑滚动往往用的是jQuery, 如实现平滑回到顶部,就写如下代码: $('.js_go_to_top').click(function () { $(".js_scroll_a ...

  5. elasticsearch client 为空 错误信息:java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecut‌​or()Ljava/util/concu‌​rrent/Executor

    错误信息:java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecut‌​or() ...

  6. Java如何获取URL连接的日期?

    Java编程中,如何获取URL连接的日期? 以下示例演示如何使用HttpURLConnection类的httpCon.getDate()方法获取URL连接的日期. package com.yiibai ...

  7. Linux部署Web应用程序超链接下载中文名称文件404问题解决办法

    Web应用程序目录下有帮助文档,是中文名称的Word文件 超链接内容如下: <a href="jsp/plugin/用户手册.doc">用户手册</a> 开 ...

  8. MyBatis Generator使用com.mysql.cj.jdbc.Driver遇到的问题

    MyBatis Generator使用com.mysql.cj.jdbc.Driver Mybatis Generator 1.3.5 新建了一个decision库,并创建了一张user表 impor ...

  9. 7. Oracle数据加载和卸载

    在日常工作中:经常会遇到这样的需求: Oracle 数据表跟文本或者文件格式进行交互:即将指定文件内容导入对应的 Oracle 数据表中:或者从 Oracle 数据表导出. 其他数据库中的表跟Orac ...

  10. 通过JVM 参数 实现spring 应用的二进制代码与配置分离。

    原创文章,转载请注明出处 分离的好处就不说了.说下分离的思路.通过JVM 参数-D 添加 config.path 的property 到系统中.系统通过System.getProperty(confi ...