@@ -25,7 +25,7 @@ def result(*args, **kwargs):
25
25
functools .update_wrapper (result , qtest_method )
26
26
return staticmethod (result )
27
27
else :
28
- return None # pragma: no cover
28
+ return None # pragma: no cover
29
29
30
30
# inject methods from QTest into QtBot
31
31
method_names = [
@@ -54,15 +54,16 @@ def result(*args, **kwargs):
54
54
55
55
@_inject_qtest_methods
56
56
class QtBot (object ):
57
+
57
58
"""
58
- Instances of this class are responsible for sending events to `Qt` objects (usually widgets),
59
- simulating user input.
60
-
61
- .. important:: Instances of this class should be accessed only by using a ``qtbot`` fixture,
59
+ Instances of this class are responsible for sending events to `Qt` objects (usually widgets),
60
+ simulating user input.
61
+
62
+ .. important:: Instances of this class should be accessed only by using a ``qtbot`` fixture,
62
63
never instantiated directly.
63
-
64
+
64
65
**Widgets**
65
-
66
+
66
67
.. automethod:: addWidget
67
68
.. automethod:: waitForWindowShown
68
69
.. automethod:: stopForInteraction
@@ -72,155 +73,150 @@ class QtBot(object):
72
73
.. automethod:: waitSignal
73
74
74
75
**Raw QTest API**
75
-
76
+
76
77
Methods below provide very low level functions, as sending a single mouse click or a key event.
77
78
Thos methods are just forwarded directly to the `QTest API`_. Consult the documentation for more
78
79
information.
79
-
80
+
80
81
---
81
-
82
+
82
83
Below are methods used to simulate sending key events to widgets:
83
-
84
+
84
85
.. staticmethod:: keyPress(widget, key[, modifier=Qt.NoModifier[, delay=-1]])
85
86
.. staticmethod:: keyClick (widget, key[, modifier=Qt.NoModifier[, delay=-1]])
86
87
.. staticmethod:: keyClicks (widget, key sequence[, modifier=Qt.NoModifier[, delay=-1]])
87
88
.. staticmethod:: keyEvent (action, widget, key[, modifier=Qt.NoModifier[, delay=-1]])
88
89
.. staticmethod:: keyPress (widget, key[, modifier=Qt.NoModifier[, delay=-1]])
89
90
.. staticmethod:: keyRelease (widget, key[, modifier=Qt.NoModifier[, delay=-1]])
90
-
91
+
91
92
Sends one or more keyword events to a widget.
92
-
93
+
93
94
:param QWidget widget: the widget that will receive the event
94
-
95
+
95
96
:param str|int key: key to send, it can be either a Qt.Key_* constant or a single character string.
96
-
97
+
97
98
.. _keyboard modifiers:
98
-
99
+
99
100
:param Qt.KeyboardModifier modifier: flags OR'ed together representing other modifier keys
100
101
also pressed. Possible flags are:
101
-
102
+
102
103
* ``Qt.NoModifier``: No modifier key is pressed.
103
104
* ``Qt.ShiftModifier``: A Shift key on the keyboard is pressed.
104
105
* ``Qt.ControlModifier``: A Ctrl key on the keyboard is pressed.
105
106
* ``Qt.AltModifier``: An Alt key on the keyboard is pressed.
106
107
* ``Qt.MetaModifier``: A Meta key on the keyboard is pressed.
107
108
* ``Qt.KeypadModifier``: A keypad button is pressed.
108
109
* ``Qt.GroupSwitchModifier``: X11 only. A Mode_switch key on the keyboard is pressed.
109
-
110
+
110
111
:param int delay: after the event, delay the test for this miliseconds (if > 0).
111
-
112
-
112
+
113
+
113
114
.. staticmethod:: keyToAscii (key)
114
-
115
+
115
116
Auxilliary method that converts the given constant ot its equivalent ascii.
116
-
117
+
117
118
:param Qt.Key_* key: one of the constants for keys in the Qt namespace.
118
-
119
- :return type: str
119
+
120
+ :return type: str
120
121
:returns: the equivalent character string.
121
122
122
123
.. note:: this method is not available in PyQt.
123
-
124
+
124
125
---
125
-
126
+
126
127
Below are methods used to simulate sending mouse events to widgets.
127
-
128
+
128
129
.. staticmethod:: mouseClick (widget, button[, stateKey=0[, pos=QPoint()[, delay=-1]]])
129
130
.. staticmethod:: mouseDClick (widget, button[, stateKey=0[, pos=QPoint()[, delay=-1]]])
130
131
.. staticmethod:: mouseEvent (action, widget, button, stateKey, pos[, delay=-1])
131
132
.. staticmethod:: mouseMove (widget[, pos=QPoint()[, delay=-1]])
132
133
.. staticmethod:: mousePress (widget, button[, stateKey=0[, pos=QPoint()[, delay=-1]]])
133
134
.. staticmethod:: mouseRelease (widget, button[, stateKey=0[, pos=QPoint()[, delay=-1]]])
134
-
135
+
135
136
Sends a mouse moves and clicks to a widget.
136
-
137
+
137
138
:param QWidget widget: the widget that will receive the event
138
-
139
- :param Qt.MouseButton button: flags OR'ed together representing the button pressed.
139
+
140
+ :param Qt.MouseButton button: flags OR'ed together representing the button pressed.
140
141
Possible flags are:
141
-
142
+
142
143
* ``Qt.NoButton``: The button state does not refer to any button (see QMouseEvent.button()).
143
144
* ``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.)
144
145
* ``Qt.RightButton``: The right button.
145
146
* ``Qt.MidButton``: The middle button.
146
147
* ``Qt.MiddleButton``: The middle button.
147
148
* ``Qt.XButton1``: The first X button.
148
149
* ``Qt.XButton2``: The second X button.
149
-
150
+
150
151
:param Qt.KeyboardModifier modifier: flags OR'ed together representing other modifier keys
151
152
also pressed. See `keyboard modifiers`_.
152
-
153
+
153
154
:param QPoint position: position of the mouse pointer.
154
-
155
+
155
156
:param int delay: after the event, delay the test for this miliseconds (if > 0).
156
-
157
-
157
+
158
+
158
159
.. _QTest API: http://doc.qt.digia.com/4.8/qtest.html
159
-
160
+
160
161
"""
161
-
162
+
162
163
def __init__ (self , app ):
163
164
"""
164
- :param QApplication app:
165
+ :param QApplication app:
165
166
The current QApplication instance.
166
167
"""
167
168
self ._app = app
168
169
self ._widgets = []
169
-
170
-
170
+
171
171
def _close (self ):
172
172
"""
173
173
Clear up method. Called at the end of each test that uses a ``qtbot`` fixture.
174
174
"""
175
175
for w in self ._widgets :
176
176
w .close ()
177
177
self ._widgets [:] = []
178
-
179
-
178
+
180
179
def addWidget (self , widget ):
181
180
"""
182
181
Adds a widget to be tracked by this bot. This is not required, but will ensure that the
183
182
widget gets closed by the end of the test, so it is highly recommended.
184
-
185
- :param QWidget widget:
183
+
184
+ :param QWidget widget:
186
185
Widget to keep track of.
187
186
"""
188
187
self ._widgets .append (widget )
189
-
190
-
188
+
191
189
def waitForWindowShown (self , widget ):
192
190
"""
193
- Waits until the window is shown in the screen. This is mainly useful for asynchronous
194
- systems like X11, where a window will be mapped to screen some time after being asked to
195
- show itself on the screen.
196
-
191
+ Waits until the window is shown in the screen. This is mainly useful for asynchronous
192
+ systems like X11, where a window will be mapped to screen some time after being asked to
193
+ show itself on the screen.
194
+
197
195
:param QWidget widget:
198
196
Widget to wait on.
199
197
"""
200
198
QtTest .QTest .qWaitForWindowShown (widget )
201
-
202
-
199
+
203
200
def stopForInteraction (self ):
204
201
"""
205
202
Stops the current test flow, letting the user interact with any visible widget.
206
-
203
+
207
204
This is mainly useful so that you can verify the current state of the program while writing
208
205
tests.
209
-
206
+
210
207
Closing the windows should resume the test run, with ``qtbot`` attempting to restore visibility
211
208
of the widgets as they were before this call.
212
209
213
210
.. note:: As a convenience, it is also aliased as `stop`.
214
211
"""
215
212
widget_visibility = [widget .isVisible () for widget in self ._widgets ]
216
-
217
- self ._app .exec_ ()
213
+
214
+ self ._app .exec_ ()
218
215
219
216
for index , visible in enumerate (widget_visibility ):
220
217
widget = self ._widgets [index ]
221
218
widget .setVisible (visible )
222
219
223
-
224
220
stop = stopForInteraction
225
221
226
222
def waitSignal (self , signal = None , timeout = 1000 ):
@@ -251,7 +247,7 @@ def waitSignal(self, signal=None, timeout=1000):
251
247
``SignalBlocker`` object. Call ``SignalBlocker.wait()`` to wait.
252
248
253
249
.. note::
254
- Cannot have both ``signals`` and ``timeout`` equal ``None``, or
250
+ Cannot have both ``signals`` and ``timeout`` equal ``None``, or
255
251
else you will block indefinitely. We throw an error if this occurs.
256
252
257
253
"""
@@ -262,6 +258,7 @@ def waitSignal(self, signal=None, timeout=1000):
262
258
263
259
264
260
class SignalBlocker (object ):
261
+
265
262
"""
266
263
Returned by :meth:`QtBot.waitSignal` method.
267
264
@@ -373,8 +370,8 @@ def qapp():
373
370
@pytest .yield_fixture
374
371
def qtbot (qapp ):
375
372
"""
376
- Fixture used to create a QtBot instance for using during testing.
377
-
373
+ Fixture used to create a QtBot instance for using during testing.
374
+
378
375
Make sure to call addWidget for each top-level widget you create to ensure
379
376
that they are properly closed after the test ends.
380
377
"""
@@ -386,4 +383,3 @@ def qtbot(qapp):
386
383
pytest .fail (format_captured_exceptions (exceptions ))
387
384
388
385
result ._close ()
389
-
0 commit comments