SignalSpy QML 类型

启用信号发出自省。 更多...

import 语句: import QtTest
继承: Item

特性

方法

详细描述

In the following example, a SignalSpy is installed to watch the "clicked" signal on a user-defined Button type. When the signal is emitted, the count property on the spy will be increased.

Button {
    id: button
    SignalSpy {
        id: spy
        target: button
        signalName: "clicked"
    }
    TestCase {
        name: "ButtonClick"
        function test_click() {
            compare(spy.count, 0)
            button.clicked();
            compare(spy.count, 1)
        }
    }
}
					

The above style of test is suitable for signals that are emitted synchronously. For asynchronous signals, the wait () method can be used to block the test until the signal occurs (or a timeout expires).

另请参阅 TestCase and Qt Quick Test .

特性文档编制

[read-only] count : int

This property defines the number of times that signalName has been emitted from target since the last call to clear ().

另请参阅 target , signalName ,和 clear ().


[read-only] signalArguments : list

This property holds a list of emitted signal arguments. Each emission of the signal will append one item to the list, containing the arguments of the signal. When connecting to a new target or new signalName or calling the clear () method, the signalArguments will be reset to empty.

另请参阅 signalName and clear ().


signalName : string

This property defines the name of the signal on target to listen for.

另请参阅 target and count .


target : var

This property defines the target object that will be used to listen for emissions of the signalName 信号。

另请参阅 signalName and count .


[read-only] valid : bool

This property defines the current signal connection status. It will be true when the signalName target is connected successfully, otherwise it will be false.

另请参阅 count , target , signalName ,和 clear ().


方法文档编制

clear ()

Clears count to 0, resets valid to false and clears the signalArguments to empty.

另请参阅 count and wait ().


wait ( timeout = 5000)

Waits for the signal signalName on target to be emitted, for up to timeout milliseconds. The test case will fail if the signal is not emitted.

SignalSpy {
    id: spy
    target: button
    signalName: "clicked"
}
function test_async_click() {
    ...
    // do something that will cause clicked() to be emitted
    ...
    spy.wait()
    compare(spy.count, 1)
}
												

There are two possible scenarios: the signal has already been emitted when wait() is called, or the signal has not yet been emitted. The wait() function handles the first scenario by immediately returning if the signal has already occurred.

The clear () method can be used to discard information about signals that have already occurred to synchronize wait() with future signal emissions.

另请参阅 clear () 和 TestCase::tryCompare ().