|  | Home · All Classes · Modules

# QTest Class Reference
[QtTest module]

The QTest namespace contains all the functions and declarations that are related to the QTestLib tool. More...

### Types

- enum KeyAction { Press, Release, Click }
- enum MouseAction { MousePress, MouseRelease, MouseClick, MouseDClick, MouseMove }
- class QTouchEventSequence

### Static Methods

- keyClick (QWidget, Qt.Key, Qt.KeyboardModifiers modifier = Qt.NoModifier, int delay = -1)
- keyClick (QWidget, str, Qt.KeyboardModifiers modifier = Qt.NoModifier, int delay = -1)
- keyClicks (QWidget, QStringQString, Qt.KeyboardModifiers modifier = Qt.NoModifier, int delay = -1)
- keyEvent (KeyAction, QWidget, Qt.Key, Qt.KeyboardModifiers modifier = Qt.NoModifier, int delay = -1)
- keyEvent (KeyAction, QWidget, str, Qt.KeyboardModifiers modifier = Qt.NoModifier, int delay = -1)
- keyPress (QWidget, Qt.Key, Qt.KeyboardModifiers modifier = Qt.NoModifier, int delay = -1)
- keyPress (QWidget, str, Qt.KeyboardModifiers modifier = Qt.NoModifier, int delay = -1)
- keyRelease (QWidget, Qt.Key, Qt.KeyboardModifiers modifier = Qt.NoModifier, int delay = -1)
- keyRelease (QWidget, str, Qt.KeyboardModifiers modifier = Qt.NoModifier, int delay = -1)
- mouseClick (QWidget, Qt.MouseButton, Qt.KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay = -1)
- mouseDClick (QWidget, Qt.MouseButton, Qt.KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay = -1)
- mouseMove (QWidget, QPoint pos = QPoint(), int delay = -1)
- mousePress (QWidget, Qt.MouseButton, Qt.KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay = -1)
- mouseRelease (QWidget, Qt.MouseButton, Qt.KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay = -1)
- qSleep (int)
- qWait (int)
- bool qWaitForWindowShown (QWidget)

## Detailed Description

The QTest namespace contains all the functions and declarations that are related to the QTestLib tool.

Please refer to the QTestLib Manual documentation for information on how to write unit tests.

## Type Documentation

### QTest.KeyAction

This enum describes possible actions for key handling.

Constant | Value | Description
--- | --- | ---
`QTest.Press` | `0` | The key is pressed.
`QTest.Release` | `1` | The key is released.
`QTest.Click` | `2` | The key is clicked (pressed and released).

### QTest.MouseAction

This enum describes possible actions for mouse handling.

Constant | Value | Description
--- | --- | ---
`QTest.MousePress` | `0` | A mouse button is pressed.
`QTest.MouseRelease` | `1` | A mouse button is released.
`QTest.MouseClick` | `2` | A mouse button is clicked (pressed and released).
`QTest.MouseDClick` | `3` | A mouse button is double clicked (pressed and released twice).
`QTest.MouseMove` | `4` | The mouse pointer has moved.

## Method Documentation

### QTest.keyClick (QWidget, Qt.Key, Qt.KeyboardModifiers modifier = Qt.NoModifier, int delay = -1)

Simulates clicking of key with an optional modifier on a widget. If delay is larger than 0, the test will wait for delay milliseconds.

Examples:

     QTest.keyClick(myWidget, Qt.Key_Escape);

     QTest.keyClick(myWidget, Qt.Key_Escape, Qt.ShiftModifier, 200);

The first example above simulates clicking the `escape` key on `myWidget` without any keyboard modifiers and without delay. The second example simulates clicking `shift-escape` on `myWidget` with a following 200 ms delay of the test.

See also QTest.keyClicks().

### QTest.keyClick (QWidget, str, Qt.KeyboardModifiers modifier = Qt.NoModifier, int delay = -1)

This is an overloaded function.

Simulates clicking of key with an optional modifier on a widget. If delay is larger than 0, the test will wait for delay milliseconds.

Example:

     QTest.keyClick(myWidget, 'a');

The example above simulates clicking `a` on `myWidget` without any keyboard modifiers and without delay of the test.

See also QTest.keyClicks().

### QTest.keyClicks (QWidget, QStringQString, Qt.KeyboardModifiers modifier = Qt.NoModifier, int delay = -1)

Simulates clicking a sequence of keys on a widget. Optionally, a keyboard modifier can be specified as well as a delay (in milliseconds) of the test before each key click.

Example:

     QTest.keyClicks(myWidget, "hello world");

The example above simulates clicking the sequence of keys representing "hello world" on `myWidget` without any keyboard modifiers and without delay of the test.

See also QTest.keyClick().

### QTest.keyEvent (KeyAction, QWidget, Qt.Key, Qt.KeyboardModifiers modifier = Qt.NoModifier, int delay = -1)

Sends a Qt key event to widget with the given key and an associated action. Optionally, a keyboard modifier can be specified, as well as a delay (in milliseconds) of the test before sending the event.

### QTest.keyEvent (KeyAction, QWidget, str, Qt.KeyboardModifiers modifier = Qt.NoModifier, int delay = -1)

This is an overloaded function.

Sends a Qt key event to widget with the given key ascii and an associated action. Optionally, a keyboard modifier can be specified, as well as a delay (in milliseconds) of the test before sending the event.

### QTest.keyPress (QWidget, Qt.Key, Qt.KeyboardModifiers modifier = Qt.NoModifier, int delay = -1)

Simulates pressing a key with an optional modifier on a widget. If delay is larger than 0, the test will wait for delay milliseconds.

Note: At some point you should release the key using keyRelease().

See also QTest.keyRelease() and QTest.keyClick().

### QTest.keyPress (QWidget, str, Qt.KeyboardModifiers modifier = Qt.NoModifier, int delay = -1)

This is an overloaded function.

Simulates pressing a key with an optional modifier on a widget. If delay is larger than 0, the test will wait for delay milliseconds.

Note: At some point you should release the key using keyRelease().

See also QTest.keyRelease() and QTest.keyClick().

### QTest.keyRelease (QWidget, Qt.Key, Qt.KeyboardModifiers modifier = Qt.NoModifier, int delay = -1)

Simulates releasing a key with an optional modifier on a widget. If delay is larger than 0, the test will wait for delay milliseconds.

See also QTest.keyPress() and QTest.keyClick().

### QTest.keyRelease (QWidget, str, Qt.KeyboardModifiers modifier = Qt.NoModifier, int delay = -1)

This is an overloaded function.

Simulates releasing a key with an optional modifier on a widget. If delay is larger than 0, the test will wait for delay milliseconds.

See also QTest.keyClick().

### QTest.mouseClick (QWidget, Qt.MouseButton, Qt.KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay = -1)

Simulates clicking a mouse button with an optional modifier on a widget. The position of the click is defined by pos; the default position is the center of the widget. If delay is specified, the test will wait for the specified amount of milliseconds before pressing and before releasing the button.

See also QTest.mousePress() and QTest.mouseRelease().

### QTest.mouseDClick (QWidget, Qt.MouseButton, Qt.KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay = -1)

Simulates double clicking a mouse button with an optional modifier on a widget. The position of the click is defined by pos; the default position is the center of the widget. If delay is specified, the test will wait for the specified amount of milliseconds before each press and release.

See also QTest.mouseClick().

### QTest.mouseMove (QWidget, QPoint pos = QPoint(), int delay = -1)

Moves the mouse pointer to a widget. If pos is not specified, the mouse pointer moves to the center of the widget. If a delay (in milliseconds) is given, the test will wait before moving the mouse pointer.

### QTest.mousePress (QWidget, Qt.MouseButton, Qt.KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay = -1)

Simulates pressing a mouse button with an optional modifier on a widget. The position is defined by pos; the default position is the center of the widget. If delay is specified, the test will wait for the specified amount of milliseconds before the press.

See also QTest.mouseRelease() and QTest.mouseClick().

### QTest.mouseRelease (QWidget, Qt.MouseButton, Qt.KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay = -1)

Simulates releasing a mouse button with an optional modifier on a widget. The position of the release is defined by pos; the default position is the center of the widget. If delay is specified, the test will wait for the specified amount of milliseconds before releasing the button.

See also QTest.mousePress() and QTest.mouseClick().

### QTest.qSleep (int)

Sleeps for ms milliseconds, blocking execution of the test. qSleep() will not do any event processing and leave your test unresponsive. Network communication might time out while sleeping. Use qWait() to do non-blocking sleeping.

ms must be greater than 0.

Note: The qSleep() function calls either `nanosleep()` on unix or `Sleep()` on windows, so the accuracy of time spent in qSleep() depends on the operating system.

Example:

     QTest.qSleep(250);

See also qWait().

### QTest.qWait (int)

Waits for ms milliseconds. While waiting, events will be processed and your test will stay responsive to user interface events or network communication.

Example:

     int i = 0;
     while (myNetworkServerNotResponding() && i++ < 50)
         QTest.qWait(250);

The code above will wait until the network server is responding for a maximum of about 12.5 seconds.

See also QTest.qSleep().

### bool QTest.qWaitForWindowShown (QWidget)

Waits until the window is shown in the screen. This is mainly useful for asynchronous systems like X11, where a window will be mapped to screen some time after being asked to show itself on the screen. Returns true.

Example:

     QWidget widget;
     widget.show();
     QTest.qWaitForWindowShown(&widget);

This function was introduced in Qt 4.6.