Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qsignalspy.qdoc
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \class QSignalSpy
6 \inmodule QtTest
7
8 \brief The QSignalSpy class enables introspection of signal emission.
9
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.
13
14 The following example records all signal emissions for the \c clicked() signal
15 of a QCheckBox:
16
17 \snippet code/doc_src_qsignalspy.cpp 0
18
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.
22
23 The example below catches a signal from a custom object:
24
25 \snippet code/doc_src_qsignalspy.cpp 1
26
27 \note Non-standard data types need to be registered, using
28 the qRegisterMetaType() function, before you can create a
29 QSignalSpy. For example:
30
31 \snippet code/doc_src_qsignalspy.cpp 2
32
33 To retrieve the instance, you can use qvariant_cast:
34
35 \snippet code/doc_src_qsignalspy.cpp 3
36
37 \section1 Verifying Signal Emissions
38
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
45 a test.
46
47 \sa QVERIFY()
48 */
49
50/*! \fn QSignalSpy::QSignalSpy(const QObject *object, const char *signal)
51
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
57 return false.
58
59 Example:
60 \snippet code/doc_src_qsignalspy.cpp 4
61*/
62
63/*! \fn template <typename PointerToMemberFunction> QSignalSpy::QSignalSpy(const QObject *object, PointerToMemberFunction signal)
64 \since 5.4
65
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
71 return false.
72
73 Example:
74 \snippet code/doc_src_qsignalspy.cpp 6
75*/
76
77/*! \fn QSignalSpy::QSignalSpy(const QObject *obj, const QMetaMethod &signal)
78 \since 5.14
79
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
85 return false.
86
87 This constructor is convenient to use when Qt's meta-object system is
88 heavily used in a test.
89
90 Basic usage example:
91 \snippet code/doc_src_qsignalspy.cpp 7
92
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
97*/
98
99/*! \fn QSignalSpy::isValid() const
100
101 Returns \c true if the signal spy listens to a valid signal, otherwise false.
102*/
103
104/*! \fn QSignalSpy::signal() const
105
106 Returns the normalized signal the spy is currently listening to.
107*/
108
109/*! \fn int QSignalSpy::qt_metacall(QMetaObject::Call call, int id, void **a)
110 \internal
111*/
112
113/*! \fn bool QSignalSpy::wait(int timeout)
114 \since 5.0
115
116 This is an overloaded function, equivalent passing \a timeout to the
117 chrono overload:
118 \code
119 wait(std::chrono::milliseconds{timeout});
120 \endcode
121
122 Returns \c true if the signal was emitted at least once in \a timeout,
123 otherwise returns \c false.
124*/
125
126/*! \fn bool QSignalSpy::wait(std::chrono::milliseconds timeout)
127 \since 6.6
128
129 Starts an event loop that runs until the given signal is received
130 or \a timeout has passed, whichever happens first.
131
132 \a timeout is any valid std::chrono::duration (std::chrono::seconds,
133 std::chrono::milliseconds ...etc).
134
135 Returns \c true if the signal was emitted at least once in \a timeout,
136 otherwise returns \c false.
137
138 Example:
139 \code
140 using namespace std::chrono_literals;
141 QSignalSpy spy(object, signal);
142 spy.wait(2s);
143 \endcode
144*/