6.0.0-rc4Source

QSignalSpy Class

The QSignalSpy class records the emissions of a signal. More…

Header#include <QSignalSpy>
CMaketarget_link_libraries(app PRIVATE Nt::Test)
ModuleNt Test C++ Classes
InheritsQObject, QList

List of all members, including inherited members

Public Functions

QSignalSpy(const Object *object, void (SignalClass::*signal)(Args...))
boolisValid() const
boolwait(int timeout = 5000)
boolwait(std::chrono::milliseconds timeout)

Detailed Description

A QSignalSpy is a list with one item for each emission, and each item is the list of that emission's arguments:

QSignalSpy spy(&counter, &Counter::valueChanged);
counter.setValue(5);
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.takeFirst().at(0).toInt(), 5);

See also QTest::qWaitFor().

Differences from Qt

There is no constructor taking a SIGNAL() string or a QMetaMethod and no signal() function. Arguments are stored with QVariant::fromValue(), so every signal can be spied on as long as its arguments can be copied; they need not be registered meta types.

Member Function Documentation

template <typename Object, typename SignalClass, typename... Args>QSignalSpy::QSignalSpy(const Object *object, void (SignalClass::*signal)(Args...))

Constructs a spy that records each emission of signal from object. Use isValid() to check that it is listening.

signal is a pointer to the signal, as passed to QObject::connect().

bool QSignalSpy::isValid() const

Returns true if the spy is listening to a signal.

bool QSignalSpy::wait(int timeout = 5000)

Processes events until the signal is emitted or timeout milliseconds have passed. Returns true if it was emitted in that time.

bool QSignalSpy::wait(std::chrono::milliseconds timeout)

This is an overloaded function.

Waits for the signal for at most timeout.