1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
8 \brief The QSignalSpy class enables introspection of signal emission.
10 QSignalSpy can connect to any signal of any object and records its emission.
11 QSignalSpy itself is a list of QVariant lists. Each emission of the signal
12 will append one item to the list, containing the arguments of the signal.
14 The following example records all signal emissions for the \c clicked() signal
17 \snippet code/doc_src_qsignalspy.cpp 0
19 \c{spy.takeFirst()} returns the arguments for the first emitted signal, as a
20 list of QVariant objects. The \c clicked() signal has a single bool argument,
21 which is stored as the first entry in the list of arguments.
23 The example below catches a signal from a custom object:
25 \snippet code/doc_src_qsignalspy.cpp 1
27 \note Non-standard data types need to be registered, using
28 the qRegisterMetaType() function, before you can create a
29 QSignalSpy. For example:
31 \snippet code/doc_src_qsignalspy.cpp 2
33 To retrieve the instance, you can use qvariant_cast:
35 \snippet code/doc_src_qsignalspy.cpp 3
37 \section1 Verifying Signal Emissions
39 The QSignalSpy class provides an elegant mechanism for capturing the list
40 of signals emitted by an object. However, you should verify its validity
41 after construction. The constructor does a number of sanity checks, such as
42 verifying that the signal to be spied upon actually exists. To make the
43 diagnosis of test failures easier, the results of these checks should be
44 checked by calling \c QVERIFY(spy.isValid()) before proceeding further with
50/*! \fn QSignalSpy::QSignalSpy(const QObject *object, const char *signal)
52 Constructs a new QSignalSpy that listens for emissions of the \a signal
53 from the QObject \a object. If QSignalSpy is not able to listen for a
54 valid signal (for example, because \a object is \nullptr or \a signal does
55 not denote a valid signal of \a object), an explanatory warning message
56 will be output using qWarning() and subsequent calls to \c isValid() will
60 \snippet code/doc_src_qsignalspy.cpp 4
63/*! \fn template <typename PointerToMemberFunction> QSignalSpy::QSignalSpy(const QObject *object, PointerToMemberFunction signal)
66 Constructs a new QSignalSpy that listens for emissions of the \a signal
67 from the QObject \a object. If QSignalSpy is not able to listen for a
68 valid signal (for example, because \a object is \nullptr or \a signal does
69 not denote a valid signal of \a object), an explanatory warning message
70 will be output using qWarning() and subsequent calls to \c isValid() will
74 \snippet code/doc_src_qsignalspy.cpp 6
77/*! \fn QSignalSpy::QSignalSpy(const QObject *obj, const QMetaMethod &signal)
80 Constructs a new QSignalSpy that listens for emissions of the \a signal
81 from the QObject \a obj. If QSignalSpy is not able to listen for a
82 valid signal (for example, because \a obj is \nullptr or \a signal does
83 not denote a valid signal of \a obj), an explanatory warning message
84 will be output using qWarning() and subsequent calls to \c isValid() will
87 This constructor is convenient to use when Qt's meta-object system is
88 heavily used in a test.
91 \snippet code/doc_src_qsignalspy.cpp 7
93 Imagine we need to check whether all properties of the QWindow class
94 that represent minimum and maximum dimensions are properly writable.
95 The following example demonstrates one of the approaches:
96 \snippet code/doc_src_qsignalspy.cpp 8
99/*! \fn QSignalSpy::isValid() const
101 Returns \c true if the signal spy listens to a valid signal, otherwise false.
104/*! \fn QSignalSpy::signal() const
106 Returns the normalized signal the spy is currently listening to.
109/*! \fn int QSignalSpy::qt_metacall(QMetaObject::Call call, int id, void **a)
113/*! \fn bool QSignalSpy::wait(int timeout)
116 This is an overloaded function, equivalent passing \a timeout to the
119 wait(std::chrono::milliseconds{timeout});
122 Returns \c true if the signal was emitted at least once in \a timeout,
123 otherwise returns \c false.
126/*! \fn bool QSignalSpy::wait(std::chrono::milliseconds timeout)
129 Starts an event loop that runs until the given signal is received
130 or \a timeout has passed, whichever happens first.
132 \a timeout is any valid std::chrono::duration (std::chrono::seconds,
133 std::chrono::milliseconds ...etc).
135 Returns \c true if the signal was emitted at least once in \a timeout,
136 otherwise returns \c false.
140 using namespace std::chrono_literals;
141 QSignalSpy spy(object, signal);