Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QSignalSpy Class Reference

\inmodule QtTest More...

#include <qsignalspy.h>

+ Inheritance diagram for QSignalSpy:
+ Collaboration diagram for QSignalSpy:

Public Member Functions

 QSignalSpy (const QObject *obj, const char *aSignal)
 Constructs a new QSignalSpy that listens for emissions of the signal from the QObject object.
 
template<typename Func >
 QSignalSpy (const typename QtPrivate::FunctionPointer< Func >::Object *obj, Func signal0)
 
 QSignalSpy (const QObject *obj, QMetaMethod signal)
 
Q_TESTLIB_EXPORT ~QSignalSpy ()
 Destructor.
 
bool isValid () const
 Returns true if the signal spy listens to a valid signal, otherwise false.
 
QByteArray signal () const
 Returns the normalized signal the spy is currently listening to.
 
bool wait (int timeout)
 
Q_TESTLIB_EXPORT bool wait (std::chrono::milliseconds timeout=std::chrono::seconds{5})
 
- Public Member Functions inherited from QList< QList< QVariant > >
 QList (DataPointer dd) noexcept
 
 QList ()=default
 
 QList (qsizetype size)
 
 QList (qsizetype size, parameter_type t)
 
 QList (std::initializer_list< QList< QVariant > > args)
 
 QList (InputIterator i1, InputIterator i2)
 
 QList (const String &str)
 
 QList (qsizetype size, Qt::Initialization)
 
QList< QList< QVariant > > & operator= (std::initializer_list< QList< QVariant > > args)
 
void swap (QList &other) noexcept
 
QTypeTraits::compare_eq_result_container< QList, U > operator== (const QList &other) const
 
QTypeTraits::compare_eq_result_container< QList, U > operator!= (const QList &other) const
 
QTypeTraits::compare_lt_result_container< QList, U > operator< (const QList &other) const noexcept(noexcept(std::lexicographical_compare< typename QList< U >::const_iterator, typename QList::const_iterator >(std::declval< QList< U > >().begin(), std::declval< QList< U > >().end(), other.begin(), other.end())))
 
QTypeTraits::compare_lt_result_container< QList, U > operator> (const QList &other) const noexcept(noexcept(other< std::declval< QList< U > >()))
 
QTypeTraits::compare_lt_result_container< QList, U > operator<= (const QList &other) const noexcept(noexcept(other< std::declval< QList< U > >()))
 
QTypeTraits::compare_lt_result_container< QList, U > operator>= (const QList &other) const noexcept(noexcept(std::declval< QList< U > >()< other))
 
qsizetype size () const noexcept
 
qsizetype count () const noexcept
 
qsizetype count (const AT &t) const noexcept
 
qsizetype length () const noexcept
 
bool isEmpty () const noexcept
 
void resize (qsizetype size)
 
void resize (qsizetype size, parameter_type c)
 
void resizeForOverwrite (qsizetype size)
 
qsizetype capacity () const
 
void reserve (qsizetype size)
 
void squeeze ()
 
void detach ()
 
bool isDetached () const noexcept
 
bool isSharedWith (const QList< QList< QVariant > > &other) const
 
pointer data ()
 
const_pointer data () const noexcept
 
const_pointer constData () const noexcept
 
void clear ()
 
const_reference at (qsizetype i) const noexcept
 
reference operator[] (qsizetype i)
 
const_reference operator[] (qsizetype i) const noexcept
 
void append (parameter_type t)
 
void append (const_iterator i1, const_iterator i2)
 
void append (rvalue_ref t)
 
void append (const QList< QList< QVariant > > &l)
 
void append (QList< QList< QVariant > > &&l)
 
void prepend (rvalue_ref t)
 
void prepend (parameter_type t)
 
reference emplaceBack (Args &&... args)
 
reference emplaceFront (Args &&... args)
 
iterator insert (qsizetype i, parameter_type t)
 
iterator insert (qsizetype i, qsizetype n, parameter_type t)
 
iterator insert (const_iterator before, parameter_type t)
 
iterator insert (const_iterator before, qsizetype n, parameter_type t)
 
iterator insert (const_iterator before, rvalue_ref t)
 
iterator insert (qsizetype i, rvalue_ref t)
 
QListassign (qsizetype n, parameter_type t)
 
QListassign (InputIterator first, InputIterator last)
 
QListassign (std::initializer_list< QList< QVariant > > l)
 
iterator emplace (const_iterator before, Args &&... args)
 
iterator emplace (qsizetype i, Args &&... args)
 
void replace (qsizetype i, parameter_type t)
 
void replace (qsizetype i, rvalue_ref t)
 
void remove (qsizetype i, qsizetype n=1)
 
void removeFirst () noexcept
 
void removeLast () noexcept
 
value_type takeFirst ()
 
value_type takeLast ()
 
QList< QList< QVariant > > & fill (parameter_type t, qsizetype size=-1)
 
void removeAt (qsizetype i)
 
qsizetype removeAll (const AT &t)
 
bool removeOne (const AT &t)
 
qsizetype removeIf (Predicate pred)
 
QList< QVarianttakeAt (qsizetype i)
 
void move (qsizetype from, qsizetype to)
 
iterator begin ()
 
const_iterator begin () const noexcept
 
iterator end ()
 
const_iterator end () const noexcept
 
const_iterator cbegin () const noexcept
 
const_iterator cend () const noexcept
 
const_iterator constBegin () const noexcept
 
const_iterator constEnd () const noexcept
 
reverse_iterator rbegin ()
 
const_reverse_iterator rbegin () const noexcept
 
reverse_iterator rend ()
 
const_reverse_iterator rend () const noexcept
 
const_reverse_iterator crbegin () const noexcept
 
const_reverse_iterator crend () const noexcept
 
iterator erase (const_iterator begin, const_iterator end)
 
iterator erase (const_iterator pos)
 
QList< QVariant > & first ()
 
const QList< QVariant > & first () const noexcept
 
QList< QList< QVariant > > first (qsizetype n) const
 
const QList< QVariant > & constFirst () const noexcept
 
QList< QVariant > & last ()
 
const QList< QVariant > & last () const noexcept
 
QList< QList< QVariant > > last (qsizetype n) const
 
const QList< QVariant > & constLast () const noexcept
 
bool startsWith (parameter_type t) const
 
bool endsWith (parameter_type t) const
 
QList< QList< QVariant > > mid (qsizetype pos, qsizetype len=-1) const
 
QList< QList< QVariant > > sliced (qsizetype pos) const
 
QList< QList< QVariant > > sliced (qsizetype pos, qsizetype n) const
 
QList< QVariantvalue (qsizetype i) const
 
QList< QVariantvalue (qsizetype i, parameter_type defaultValue) const
 
void swapItemsAt (qsizetype i, qsizetype j)
 
void push_back (parameter_type t)
 
void push_back (rvalue_ref t)
 
void push_front (rvalue_ref t)
 
void push_front (parameter_type t)
 
void pop_back () noexcept
 
void pop_front () noexcept
 
reference emplace_back (Args &&... args)
 
bool empty () const noexcept
 
reference front ()
 
const_reference front () const noexcept
 
reference back ()
 
const_reference back () const noexcept
 
void shrink_to_fit ()
 
QList< QList< QVariant > > & operator+= (const QList< QList< QVariant > > &l)
 
QList< QList< QVariant > > & operator+= (QList< QList< QVariant > > &&l)
 
QList< QList< QVariant > > & operator+= (parameter_type t)
 
QList< QList< QVariant > > & operator+= (rvalue_ref t)
 
QList< QList< QVariant > > operator+ (const QList< QList< QVariant > > &l) const &
 
QList< QList< QVariant > > operator+ (const QList< QList< QVariant > > &l) &&
 
QList< QList< QVariant > > operator+ (QList< QList< QVariant > > &&l) const &
 
QList< QList< QVariant > > operator+ (QList< QList< QVariant > > &&l) &&
 
QList< QList< QVariant > > & operator<< (parameter_type t)
 
QList< QList< QVariant > > & operator<< (const QList< QList< QVariant > > &l)
 
QList< QList< QVariant > > & operator<< (QList< QList< QVariant > > &&l)
 
QList< QList< QVariant > > & operator<< (rvalue_ref t)
 
QList< QList< QVariant > > toList () const noexcept
 
QList< QList< QVariant > > toVector () const noexcept
 
- Public Member Functions inherited from QListSpecialMethodsBase< T >
template<typename AT = T>
qsizetype indexOf (const AT &t, qsizetype from=0) const noexcept
 
template<typename AT = T>
qsizetype lastIndexOf (const AT &t, qsizetype from=-1) const noexcept
 
template<typename AT = T>
bool contains (const AT &t) const noexcept
 

Friends

class QSignalSpyPrivate
 

Additional Inherited Members

- Public Types inherited from QList< QList< QVariant > >
using Type
 
using value_type
 
using pointer
 
using const_pointer
 
using reference
 
using const_reference
 
using size_type
 
using difference_type
 
using parameter_type
 
using rvalue_ref
 
using Iterator
 
using ConstIterator
 
using reverse_iterator
 
using const_reverse_iterator
 
- Static Public Member Functions inherited from QList< QList< QVariant > >
static qsizetype max_size () noexcept
 
static QList< QList< QVariant > > fromList (const QList< QList< QVariant > > &list) noexcept
 
static QList< QList< QVariant > > fromVector (const QList< QList< QVariant > > &vector) noexcept
 
static QList< QList< QVariant > > fromReadOnlyData (const QList< QVariant >(&t)[N]) noexcept
 
- Protected Types inherited from QListSpecialMethodsBase< T >
using Self = QList<T>
 
- Protected Member Functions inherited from QListSpecialMethods< T >
 ~QListSpecialMethods ()=default
 
- Protected Member Functions inherited from QListSpecialMethodsBase< T >
 ~QListSpecialMethodsBase ()=default
 
Selfself ()
 
const Selfself () const
 

Detailed Description

\inmodule QtTest

The QSignalSpy class enables introspection of signal emission.

QSignalSpy can connect to any signal of any object and records its emission. QSignalSpy itself is a list of QVariant lists. Each emission of the signal will append one item to the list, containing the arguments of the signal.

The following example records all signal emissions for the clicked() signal of a QCheckBox:

QCheckBox *box = ...;
QSignalSpy spy(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
QList<QVariant> arguments = spy.takeFirst(); // take the first signal
QVERIFY(arguments.at(0).toBool() == true); // verify the first argument

{spy.takeFirst()} returns the arguments for the first emitted signal, as a list of QVariant objects. The clicked() signal has a single bool argument, which is stored as the first entry in the list of arguments.

The example below catches a signal from a custom object:

QSignalSpy spy(myCustomObject, SIGNAL(mySignal(int,QString,double)));
myCustomObject->doSomething(); // trigger emission of the signal
QList<QVariant> arguments = spy.takeFirst();
QVERIFY(arguments.at(0).typeId() == QMetaType::Int);
QVERIFY(arguments.at(1).typeId() == QMetaType::QString);
QVERIFY(arguments.at(2).typeId() == QMetaType::Double);
Note
Non-standard data types need to be registered, using the qRegisterMetaType() function, before you can create a QSignalSpy. For example:

To retrieve the instance, you can use qvariant_cast:

// get the first argument from the first received signal:
SomeStruct result = qvariant_cast<SomeStruct>(spy.at(0).at(0));

Definition at line 21 of file qsignalspy.h.

Constructor & Destructor Documentation

◆ QSignalSpy() [1/3]

QSignalSpy::QSignalSpy ( const QObject * object,
const char * signal )
inlineexplicit

Constructs a new QSignalSpy that listens for emissions of the signal from the QObject object.

If QSignalSpy is not able to listen for a valid signal (for example, because object is \nullptr or signal does not denote a valid signal of object), an explanatory warning message will be output using qWarning() and subsequent calls to isValid() will return false.

Example:

Definition at line 30 of file qsignalspy.h.

◆ QSignalSpy() [2/3]

template<typename Func >
QSignalSpy::QSignalSpy ( const typename QtPrivate::FunctionPointer< Func >::Object * obj,
Func signal0 )
inline

Definition at line 37 of file qsignalspy.h.

◆ QSignalSpy() [3/3]

QSignalSpy::QSignalSpy ( const QObject * obj,
QMetaMethod signal )
inline
Since
5.14

Constructs a new QSignalSpy that listens for emissions of the signal from the QObject obj. If QSignalSpy is not able to listen for a valid signal (for example, because obj is \nullptr or signal does not denote a valid signal of obj), an explanatory warning message will be output using qWarning() and subsequent calls to isValid() will return false.

This constructor is convenient to use when Qt's meta-object system is heavily used in a test.

Basic usage example:

auto mo = object.metaObject();
auto signalIndex = mo->indexOfSignal("objectNameChanged(QString)");
auto signal = mo->method(signalIndex);
QSignalSpy spy(&object, signal);
object.setObjectName("A new object name");

Imagine we need to check whether all properties of the QWindow class that represent minimum and maximum dimensions are properly writable. The following example demonstrates one of the approaches:

void tst_QWindow::writeMinMaxDimensionalProps_data()
QTest::addColumn<int>("propertyIndex");
// Collect all relevant properties
static const auto mo = QWindow::staticMetaObject;
for (int i = mo.propertyOffset(); i < mo.propertyCount(); ++i) {
auto property = mo.property(i);
// ...that have type int
if (property.type() == QVariant::Int) {
static const QRegularExpression re("^minimum|maximum");
const auto name = property.name();
// ...and start with "minimum" or "maximum"
if (re.match(name).hasMatch()) {
QTest::addRow("%s", name) << i;
}
}
}
}
void tst_QWindow::writeMinMaxDimensionalProps()
{
QFETCH(int, propertyIndex);
auto property = QWindow::staticMetaObject.property(propertyIndex);
QVERIFY(property.isWritable());
QVERIFY(property.hasNotifySignal());
QSignalSpy spy(&window, property.notifySignal());
QVERIFY(property.write(&window, 42));
}
value_type takeFirst()
Definition qlist.h:566
qsizetype count() const noexcept
Definition qlist.h:398
\inmodule QtTest
Definition qsignalspy.h:22
int typeId() const
Returns the storage type of the value stored in the variant.
Definition qvariant.h:340
auto signalIndex
for(int i=mo.propertyOffset();i< mo.propertyCount();++i)
[3]
qRegisterMetaType< SomeStruct >()
[1]
auto mo
[7]
QSignalSpy spy(myCustomObject, SIGNAL(mySignal(int, QString, double)))
[0]
QObject object
[6]
Q_TESTLIB_EXPORT QTestData & addRow(const char *format,...) Q_ATTRIBUTE_FORMAT_PRINTF(1
#define QFETCH(Type, name)
Definition qtestcase.h:278
#define QCOMPARE(actual, expected)
Definition qtestcase.h:81
#define QVERIFY(statement)
Definition qtestcase.h:58

Definition at line 40 of file qsignalspy.h.

◆ ~QSignalSpy()

QSignalSpy::~QSignalSpy ( )
default

Destructor.

Member Function Documentation

◆ isValid()

QSignalSpy::isValid ( ) const
inline

Returns true if the signal spy listens to a valid signal, otherwise false.

Definition at line 44 of file qsignalspy.h.

References QByteArray::isEmpty().

Referenced by QQuickControlsTestUtils::clickButton(), and QQuickControlsTestUtils::doubleClickButton().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ signal()

QSignalSpy::signal ( ) const
inline

Returns the normalized signal the spy is currently listening to.

Definition at line 45 of file qsignalspy.h.

◆ wait() [1/2]

bool QSignalSpy::wait ( int timeout)
inline
Since
5.0

This is an overloaded function, equivalent passing timeout to the chrono overload:

wait(std::chrono::milliseconds{timeout});
bool wait(int timeout)
Definition qsignalspy.h:47
GLbitfield GLuint64 timeout
[4]

Returns true if the signal was emitted at least once in timeout, otherwise returns false.

Definition at line 47 of file qsignalspy.h.

References wait().

Referenced by wait().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ wait() [2/2]

bool QSignalSpy::wait ( std::chrono::milliseconds timeout = std::chrono::seconds{5})
Since
6.6

Starts an event loop that runs until the given signal is received or timeout has passed, whichever happens first.

timeout is any valid std::chrono::duration (std::chrono::seconds, std::chrono::milliseconds ...etc).

Returns true if the signal was emitted at least once in timeout, otherwise returns false.

Example:

using namespace std::chrono_literals;
\inmodule QtTest
Definition qsignalspy.h:22
QByteArray signal() const
Returns the normalized signal the spy is currently listening to.
Definition qsignalspy.h:45
QSignalSpy spy(myCustomObject, SIGNAL(mySignal(int, QString, double)))
[0]
GLdouble s
[6]
Definition qopenglext.h:235

Definition at line 145 of file qsignalspy.cpp.

References QTestEventLoop::enterLoop(), Q_ASSERT, QMutexLocker< Mutex >::relock(), QList< QList< QVariant > >::size(), and QMutexLocker< Mutex >::unlock().

+ Here is the call graph for this function:

Friends And Related Symbol Documentation

◆ QSignalSpyPrivate

friend class QSignalSpyPrivate
friend

Definition at line 27 of file qsignalspy.h.


The documentation for this class was generated from the following files: