PySide6.QtTest.QSignalSpy¶
- class QSignalSpy¶
QSignalSpy
类允许对信号发射进行内省。更多…概要¶
方法¶
注意
本文档可能包含从C++自动翻译到Python的代码片段。我们始终欢迎对代码片段翻译的贡献。如果您发现翻译问题,您也可以通过在我们的https:/bugreports.qt.io/projects/PYSIDE上创建工单来告知我们。
详细描述¶
警告
本节包含从C++自动翻译到Python的代码片段,可能包含错误。
QSignalSpy
可以连接到任何对象的任何信号并记录其发射。QSignalSpy
本身是一个 QVariant 列表的列表。每次信号发射都会向列表中添加一个项目,包含信号的参数。以下示例记录了QCheckBox的
clicked()
信号的所有信号发射:box = ... spy = QSignalSpy(box, SIGNAL(clicked(bool))) # do something that triggers the signal box.animateClick() QCOMPARE(spy.count(), 1) # make sure the signal was emitted exactly one time arguments = spy.takeFirst() # take the first signal QVERIFY(arguments.at(0).toBool() == True) # verify the first argument
spy.takeFirst()
返回第一个发出的信号的参数,作为 QVariant 对象的列表。clicked()
信号有一个单一的布尔参数,它作为参数列表中的第一个条目存储。下面的示例捕获来自自定义对象的信号:
spy = QSignalSpy(myCustomObject, SIGNAL(mySignal(int,QString,double))) myCustomObject.doSomething() # trigger emission of the signal arguments = spy.takeFirst() QVERIFY(arguments.at(0).typeId() == QMetaType.Int) QVERIFY(arguments.at(1).typeId() == QMetaType.QString) QVERIFY(arguments.at(2).typeId() == QMetaType.Double)
注意
非标准数据类型需要在使用qRegisterMetaType()函数注册后,才能创建
QSignalSpy
。例如:qRegisterMetaType<SomeStruct>() spy = QSignalSpy(model, SIGNAL(whatever(SomeStruct)))
要检索实例,您可以使用qvariant_cast:
# get the first argument from the first received signal: result = SomeStruct(spy.at(0).at(0))
验证信号发射¶
QSignalSpy
类提供了一种优雅的机制来捕获对象发出的信号列表。然而,在构造后应验证其有效性。构造函数会进行多项健全性检查,例如验证要监视的信号是否确实存在。为了使测试失败的诊断更容易,应在继续测试之前通过调用QVERIFY(spy.isValid())
来检查这些检查的结果。另请参阅
QVERIFY()
- __init__(signal)¶
- Parameters:
signal –
PySideSignalInstance
构造一个新的QSignalSpy,用于监听信号的发射。
- __init__(obj, signal)
- Parameters:
obj –
QObject
signal –
QMetaMethod
警告
本节包含从C++自动翻译到Python的代码片段,可能包含错误。
构造一个新的
QSignalSpy
,用于监听来自QObjectobj
的signal
的发射。如果QSignalSpy
无法监听有效的信号(例如,因为obj
是None
或signal
不表示obj
的有效信号),将使用qWarning()输出解释性警告消息,并且后续对isValid()
的调用将返回false。当测试中大量使用Qt的元对象系统时,此构造函数非常方便使用。
基本用法示例:
object = QObject() mo = object.metaObject() signalIndex = mo.indexOfSignal("objectNameChanged(QString)") signal = mo.method(signalIndex) spy = QSignalSpy(object, signal) object.setObjectName("A object() name") QCOMPARE(spy.count(), 1)
想象一下,我们需要检查QWindow类中表示最小和最大尺寸的所有属性是否可正确写入。以下示例展示了其中一种方法:
def writeMinMaxDimensionalProps_data(self): QTest.addColumn<int>("propertyIndex") # Collect all relevant properties mo = QWindow.staticMetaObject() for i in range(mo.propertyOffset(), mo.propertyCount()): property = mo.property(i) # ...that have type int if property.type() == QVariant.Int: re = QRegularExpression("^minimum|maximum") name = property.name() # ...and start with "minimum" or "maximum" if re.match(name).hasMatch(): QTest.addRow("%s", name) << i def writeMinMaxDimensionalProps(self): QFETCH(int, propertyIndex) property = QWindow.staticMetaObject.property(propertyIndex) QVERIFY(property.isWritable()) QVERIFY(property.hasNotifySignal()) window = QWindow() spy = QSignalSpy(window, property.notifySignal()) QVERIFY(property.write(window, 42)) QCOMPARE(spy.count(), 1)
- __init__(obj, aSignal)
- Parameters:
obj –
QObject
aSignal – str
警告
本节包含从C++自动翻译到Python的代码片段,可能包含错误。
构造一个新的
QSignalSpy
,用于监听来自QObjectobject
的signal
信号。如果QSignalSpy
无法监听有效的信号(例如,因为object
是None
或signal
不表示object
的有效信号),将使用qWarning()输出解释性警告消息,并且后续对isValid()
的调用将返回false。示例:
spy = QSignalSpy(myPushButton, SIGNAL(clicked(bool)))
- at(arg__1)¶
- Parameters:
arg__1 – 整数
- Return type:
.QVariant 列表
- count()¶
- Return type:
整数
- isValid()¶
- Return type:
布尔
如果信号监听器监听的是有效信号,则返回
true
,否则返回 false。- signal()¶
- Return type:
返回间谍当前正在监听的归一化信号。
- size()¶
- Return type:
整数
- wait(timeout)¶
- Parameters:
timeout – int
- Return type:
布尔
这是一个重载函数,相当于将
timeout
传递给chrono重载:wait(std::chrono::milliseconds{timeout});
如果在
timeout
内至少发出一次信号,则返回true
,否则返回false
。