本文是对《python qt gui快速编程》的第10章的例子events用python3+pyqt5进行改写,涉及到重新实现qwidget的事件处理程序。本例子涉及到上下文菜单,鼠标事件,键盘事件,可作为重新实现事件处理程序的参考。
注:在创建上下文菜单最简单的方式使用qwidget.addaction()把动作添加到窗口部件中,再把窗口部件的上下文菜单策略设置为qt.actionscontextmenu即可,但是如果像本例子一样要根据不同的状态来提供不同的选项,则要重新实现上下文菜单事件处理程序。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
#!/usr/bin/env python3 import sys from pyqt5.qtcore import (qevent, qtimer, qt) from pyqt5.qtwidgets import (qapplication, qmenu, qwidget) from pyqt5.qtgui import qpainter class widget(qwidget): def __init__( self , parent = none): super (widget, self ).__init__(parent) self .justdoubleclicked = false self .key = "" self .text = "" self .message = "" self .resize( 400 , 300 ) self .move( 100 , 100 ) self .setwindowtitle( "events" ) qtimer.singleshot( 0 , self .givehelp) # avoids first resize msg def givehelp( self ): self .text = "click to toggle mouse tracking" self .update() def closeevent( self , event): print ( "closed" ) def contextmenuevent( self , event): menu = qmenu( self ) oneaction = menu.addaction( "&one" ) twoaction = menu.addaction( "&two" ) oneaction.triggered.connect( self .one) twoaction.triggered.connect( self .two) if not self .message: menu.addseparator() threeaction = menu.addaction( "thre&e" ) threeaction.triggered.connect( self .three) menu.exec_(event.globalpos()) def one( self ): self .message = "menu option one" self .update() def two( self ): self .message = "menu option two" self .update() def three( self ): self .message = "menu option three" self .update() def paintevent( self , event): text = self .text i = text.find( "\n\n" ) if i > = 0 : text = text[ 0 :i] if self .key: text + = "\n\nyou pressed: {0}" . format ( self .key) painter = qpainter( self ) painter.setrenderhint(qpainter.textantialiasing) painter.drawtext( self .rect(), qt.aligncenter, text) if self .message: painter.drawtext( self .rect(), qt.alignbottom|qt.alignhcenter, self .message) qtimer.singleshot( 5000 , self .clearmessage) qtimer.singleshot( 5000 , self .update) def clearmessage( self ): self .message = "" def resizeevent( self , event): self .text = "resized to qsize({0}, {1})" . format ( event.size().width(), event.size().height()) self .update() def mousereleaseevent( self , event): if self .justdoubleclicked: self .justdoubleclicked = false else : self .setmousetracking( not self .hasmousetracking()) if self .hasmousetracking(): self .text = "mouse tracking is on.\n" + \ "try moving the mouse!\n" + \ "single click to switch it off" else : self .text = "mouse tracking is off.\n" + \ "single click to switch it on" self .update() def mousemoveevent( self , event): if not self .justdoubleclicked: globalpos = self .maptoglobal(event.pos()) self .text = "the mouse is at\nqpoint({0}, {1}) " + \ "in widget coords, and\n" + \ "qpoint({2}, {3}) in screen coords" . format ( event.pos().x(), event.pos().y(), globalpos.x(), globalpos.y()) self .update() def mousedoubleclickevent( self , event): self .justdoubleclicked = true self .text = "double-clicked." self .update() def keypressevent( self , event): self .key = "" if event.key() = = qt.key_home: self .key = "home" elif event.key() = = qt.key_end: self .key = "end" elif event.key() = = qt.key_pageup: if event.modifiers() & qt.controlmodifier: self .key = "ctrl+pageup" else : self .key = "pageup" elif event.key() = = qt.key_pagedown: if event.modifiers() & qt.controlmodifier: self .key = "ctrl+pagedown" else : self .key = "pagedown" elif qt.key_a < = event.key() < = qt.key_z: if event.modifiers() & qt.shiftmodifier: self .key = "shift+" self .key + = event.text() if self .key: self .key = self .key self .update() else : qwidget.keypressevent( self , event) def event( self , event): if (event. type () = = qevent.keypress and event.key() = = qt.key_tab): self .key = "tab captured in event()" self .update() return true return qwidget.event( self , event) if __name__ = = "__main__" : app = qapplication(sys.argv) form = widget() form.show() app.exec_() |
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/xiaoyangyang20/article/details/54706445