1
1
import pytest
2
2
from pytestqt .qt_compat import QtGui
3
+ from pytestqt .qt_compat import QtTest
3
4
4
5
#===================================================================================================
5
6
# QtBot
6
7
#===================================================================================================
7
8
class QtBot (object ):
8
9
'''
9
- Responsible for sending events to Qt objects, simulating user input.
10
+ Instances of this class are responsible for sending events to `Qt` objects (usually widgets),
11
+ simulating user input.
12
+
13
+ .. important:: Instances of this class should be accessed only by using a ``qtbot`` fixture,
14
+ never instantiated directly.
15
+
16
+ **Widgets**
17
+
18
+ .. automethod:: addWidget
19
+ .. automethod:: waitForWindowShown
20
+
21
+ **QTest API**
22
+
23
+ Methods below are forwarded directly to the `QTest API`_. Consult documentation for more
24
+ information.
25
+
26
+ ---
27
+
28
+ Below are methods used to simulate sending key events to widgets:
29
+
30
+ .. staticmethod:: keyPress(widget, key[, modifier=Qt.NoModifier[, delay=-1]])
31
+ .. staticmethod:: keyClick (widget, key[, modifier=Qt.NoModifier[, delay=-1]])
32
+ .. staticmethod:: keyClicks (widget, key sequence[, modifier=Qt.NoModifier[, delay=-1]])
33
+ .. staticmethod:: keyEvent (action, widget, key[, modifier=Qt.NoModifier[, delay=-1]])
34
+ .. staticmethod:: keyPress (widget, key[, modifier=Qt.NoModifier[, delay=-1]])
35
+ .. staticmethod:: keyRelease (widget, key[, modifier=Qt.NoModifier[, delay=-1]])
36
+
37
+ Sends one or more keyword events to a widget.
38
+
39
+ :param QWidget widget: the widget that will receive the event
40
+
41
+ :param str|int key: key to send, it can be either a Qt.Key_* constant or a single character string.
42
+
43
+ .. _keyboard modifiers:
44
+
45
+ :param Qt.KeyboardModifier modifier: flags OR'ed together representing other modifier keys
46
+ also pressed. Possible flags are:
47
+
48
+ * ``Qt.NoModifier``: No modifier key is pressed.
49
+ * ``Qt.ShiftModifier``: A Shift key on the keyboard is pressed.
50
+ * ``Qt.ControlModifier``: A Ctrl key on the keyboard is pressed.
51
+ * ``Qt.AltModifier``: An Alt key on the keyboard is pressed.
52
+ * ``Qt.MetaModifier``: A Meta key on the keyboard is pressed.
53
+ * ``Qt.KeypadModifier``: A keypad button is pressed.
54
+ * ``Qt.GroupSwitchModifier``: X11 only. A Mode_switch key on the keyboard is pressed.
55
+
56
+ :param int delay: after the event, delay the test for this miliseconds (if > 0).
57
+
58
+
59
+ .. staticmethod:: keyToAscii (key)
60
+
61
+ Auxilliary method that converts the given constant ot its equivalent ascii.
62
+
63
+ :param Qt.Key_* key: one of the constants for keys in the Qt namespace.
64
+
65
+ :return type: str
66
+ :returns: the equivalent character string.
67
+
68
+ ---
69
+
70
+ Below are methods used to simulate sending mouse events to widgets.
71
+
72
+ .. staticmethod:: mouseClick (widget, button[, stateKey=0[, pos=QPoint()[, delay=-1]]])
73
+ .. staticmethod:: mouseDClick (widget, button[, stateKey=0[, pos=QPoint()[, delay=-1]]])
74
+ .. staticmethod:: mouseEvent (action, widget, button, stateKey, pos[, delay=-1])
75
+ .. staticmethod:: mouseMove (widget[, pos=QPoint()[, delay=-1]])
76
+ .. staticmethod:: mousePress (widget, button[, stateKey=0[, pos=QPoint()[, delay=-1]]])
77
+ .. staticmethod:: mouseRelease (widget, button[, stateKey=0[, pos=QPoint()[, delay=-1]]])
78
+
79
+ Sends a mouse moves and clicks to a widget.
80
+
81
+ :param QWidget widget: the widget that will receive the event
82
+
83
+ :param Qt.MouseButton button: flags OR'ed together representing the button pressed.
84
+ Possible flags are:
85
+
86
+ * ``Qt.NoButton``: The button state does not refer to any button (see QMouseEvent.button()).
87
+ * ``Qt.LeftButton``: The left button is pressed, or an event refers to the left button. (The left button may be the right button on left-handed mice.)
88
+ * ``Qt.RightButton``: The right button.
89
+ * ``Qt.MidButton``: The middle button.
90
+ * ``Qt.MiddleButton``: he middle button.
91
+ * ``Qt.XButton1``: The first X button.
92
+ * ``Qt.XButton2``: The second X button.
93
+
94
+ :param Qt.KeyboardModifier modifier: flags OR'ed together representing other modifier keys
95
+ also pressed. See `keyboard modifiers`_.
96
+
97
+ :param QPoint position: position of the mouse pointer.
98
+
99
+ :param int delay: after the event, delay the test for this miliseconds (if > 0).
100
+
101
+
102
+ .. _QTest API: http://doc.qt.digia.com/4.7/qtest.html
10
103
11
- Instances of this class should be accessed by the ``qtbot`` pytest fixture.
12
104
'''
13
105
14
106
def __init__ (self , app ):
@@ -38,6 +130,18 @@ def addWidget(self, widget):
38
130
Widget to keep track of.
39
131
'''
40
132
self ._widgets .append (widget )
133
+
134
+
135
+ def waitForWindowShown (self , widget ):
136
+ '''
137
+ Waits until the window is shown in the screen. This is mainly useful for asynchronous
138
+ systems like X11, where a window will be mapped to screen some time after being asked to
139
+ show itself on the screen.
140
+
141
+ :param QWidget widget:
142
+ Widget to wait on.
143
+ '''
144
+ QtTest .QTest .qWaitForWindowShown (widget )
41
145
42
146
43
147
#===================================================================================================
@@ -76,3 +180,41 @@ def qtbot(request):
76
180
request .addfinalizer (result ._close )
77
181
return result
78
182
183
+
184
+
185
+
186
+ #===================================================================================================
187
+ # Inject QtTest Functions
188
+ #===================================================================================================
189
+ def _createQTestProxyMethod (method_name ):
190
+
191
+ def result (* args , ** kwargs ):
192
+ method = getattr (QtTest .QTest , method_name )
193
+ return method (* args , ** kwargs )
194
+
195
+ result .__name__ = method_name
196
+ return staticmethod (result )
197
+
198
+ # inject methods from QTest into QtBot
199
+ method_names = set ([
200
+ 'keyPress' ,
201
+ 'keyClick' ,
202
+ 'keyClicks' ,
203
+ 'keyEvent' ,
204
+ 'keyPress' ,
205
+ 'keyRelease' ,
206
+ 'keyToAscii' ,
207
+
208
+ 'mouseClick' ,
209
+ 'mouseDClick' ,
210
+ 'mouseEvent' ,
211
+ 'mouseMove' ,
212
+ 'mousePress' ,
213
+ 'mouseRelease' ,
214
+
215
+
216
+ ])
217
+ for method_name in method_names :
218
+ method = _createQTestProxyMethod (method_name )
219
+ setattr (QtBot , method_name , method )
220
+
0 commit comments