PyQt5编程(16):事件处理

通过重载实现特定的函数,可以在事件到达特定的事件处理器之前截获并处理。比如,通过重载keyPressEvent () 函数,可处理击键事件。所有事件均继承自QEvent,同时重载了以下方法:

accept() - Sets the accept flag of the event object, the equivalent of calling setAccepted(True).  This flag is usually set by default;
ignore() - Clears the accept flag parameter of the event object, the equivalent of calling setAccepted(False);
setAccepted(bool accepted) - if the parameter is set to True, the flag that allows further processing of the event will be set (similar to the call method accept ()), and if False - reset (similar to calling the ignore () method);
isAccepted() -  returns the current state of the flag that allows further processing of the event;
spontaneous () - returns true if the event originated outside the application (a system event); otherwise returns false;
type() - returns the type of the event.
0 - Not an event.
1 - Timer - Regular timer events.
2 - MouseButtonPress -  Mouse press.
3-  MouseButtonRelease -Mouse release.
4 - MouseButtonDblClick  - Double click.
5 - MouseMove - move the mouse.
6 - KeyPress - the key on the keyboard is pressed.
7 - KyeRelease - the key on the keyboard is released.
8 - FocusIn - keyboard focus is received.
9 - FocusOut - keyboard input focus is lost.
10 - Enter - the mouse pointer enters the component area.
11 - Leave - the mouse pointer leaves the component area.
12 - Paint - redrawing the component.
13 - Move - the position of the component has changed.
14 - Resize - the size of the component has changed.
17 - Show - the component is displayed.
18 - Hide -  the component is hidden.
19 - Close -the window is closed.
24 - WindowActive - the windows has become active.
25 - WindowDeactive - the windows has become inactive.
26 - ShowToParent - the child component is displayed.
27 - HideToParent - the child component is hidden.
31 - Wheel - the mouse wheel is scrolled.
40 - Clipboard - the contents of the clipboard are changed.
60 - DragEnter - the mouse pointer enters the component area during the drag-and-drop operation.
61 - DragMove - drag operation is performed.
62 - DragLeave - the mouse pointer leaves the component area during the replay operation.
63 - Drop - the drag-and-drop operation is completed.
68 - ChildAdd - the child is added.
69 - ChildPolished - the child component is configured.
71 - ChildRemoved - the child component has been removed.
74 - PolishRequest - the component  will be configured.
75 - Polish - the component is configured.
82 - ContextMenu - context menu event.
99 - ActivationChange - the status of the activity of the top-level window has changed.
103 - WindowBlocked - the window is blocked by the modal window.
104 - WindowUnblocked - the current window is unlocked after the modal window.
105 - WindowStateChange - the status of the window has changed.
121 - ApplicationActivate - the application became available to the user.
122 - ApplicationDeactivate - the application became unavailable to the user..
1000 - User - user event.
65535 - MaxUser - the maximum identifier of the user event.

QEvent 类的静态函数 registerEventType([int number])可用来注册一个自定义事件,函数返回事件的ID.

参数number的取值在QEvent.User (1000)-QEvent.MaxUseg(65535)之间。
拦截事件是通过重载某一事件event(self, event)实现的。参数event是一个包括有该事件其他信息的对象,不同事件的对象是有差别的。例如,MouseButtonPress事件的对象为QMouseEvent类的实例,而
Keypress事件的对象为QKeyEvent类的实例。
在重载event()方法时,如果返回True,父类将无法再接收到此事件。如果要将事件交由父类继续处理,需要调用父类的event(),并将当前事件对象作为参数。通常的做法为(假设该类继承自QWidget):
return QtWidgets.QWidget.event(self,e)
下面的例子为截取键盘按键,单击并关闭窗口的示例。

-- coding: utf-8 --

from PyQt5 import QtCore, QtWidgets

class MyWindow(QtWidgets.QWidget):

def __init__(self, parent=None):
    QtWidgets.QWidget.__init__(self, parent)
    self.resize(300, 100)
def event(self, e):
    if e.type() == QtCore.QEvent.KeyPress:
        print("按下键盘皱键")
        print("ASCII码:", e.key(), ", 字母:", e.text())
    elif e.type() == QtCore.QEvent.Close:
        print("窗口关闭")
    elif e.type() == QtCore.QEvent.MouseButtonPress:
        print("点鼠标. 坐标值:", e.x(), e.y())
    return QtWidgets.QWidget.event(self, e) 

if name == "main":
import sys
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())

你可能感兴趣的:(PyQt5编程(16):事件处理)