Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
doc_src_qsignalspy.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4//! [0]
6QSignalSpy spy(box, SIGNAL(clicked(bool)));
7
8// do something that triggers the signal
9box->animateClick();
10
11QCOMPARE(spy.count(), 1); // make sure the signal was emitted exactly one time
12QList<QVariant> arguments = spy.takeFirst(); // take the first signal
13
14QVERIFY(arguments.at(0).toBool() == true); // verify the first argument
15//! [0]
16
17
18//! [1]
19QSignalSpy spy(myCustomObject, SIGNAL(mySignal(int,QString,double)));
20
21myCustomObject->doSomething(); // trigger emission of the signal
22
23QList<QVariant> arguments = spy.takeFirst();
24QVERIFY(arguments.at(0).typeId() == QMetaType::Int);
25QVERIFY(arguments.at(1).typeId() == QMetaType::QString);
26QVERIFY(arguments.at(2).typeId() == QMetaType::Double);
27//! [1]
28
29
30//! [2]
31qRegisterMetaType<SomeStruct>();
32QSignalSpy spy(&model, SIGNAL(whatever(SomeStruct)));
33//! [2]
34
35
36//! [3]
37// get the first argument from the first received signal:
38SomeStruct result = qvariant_cast<SomeStruct>(spy.at(0).at(0));
39//! [3]
40
41
42//! [4]
43QSignalSpy spy(myPushButton, SIGNAL(clicked(bool)));
44//! [4]
45
46
47//! [6]
48QSignalSpy spy(myPushButton, &QPushButton::clicked);
49//! [6]
50
51//! [7]
53auto mo = object.metaObject();
54auto signalIndex = mo->indexOfSignal("objectNameChanged(QString)");
55auto signal = mo->method(signalIndex);
56
57QSignalSpy spy(&object, signal);
58object.setObjectName("A new object name");
59QCOMPARE(spy.count(), 1);
60//! [7]
61
62//! [8]
63void tst_QWindow::writeMinMaxDimensionalProps_data()
64 QTest::addColumn<int>("propertyIndex");
65
66 // Collect all relevant properties
67 static const auto mo = QWindow::staticMetaObject;
68 for (int i = mo.propertyOffset(); i < mo.propertyCount(); ++i) {
69 auto property = mo.property(i);
70
71 // ...that have type int
72 if (property.type() == QVariant::Int) {
73 static const QRegularExpression re("^minimum|maximum");
74 const auto name = property.name();
75
76 // ...and start with "minimum" or "maximum"
77 if (re.match(name).hasMatch()) {
78 QTest::addRow("%s", name) << i;
79 }
80 }
81 }
82}
83
84void tst_QWindow::writeMinMaxDimensionalProps()
85{
86 QFETCH(int, propertyIndex);
87
88 auto property = QWindow::staticMetaObject.property(propertyIndex);
89 QVERIFY(property.isWritable());
90 QVERIFY(property.hasNotifySignal());
91
92 QWindow window;
93 QSignalSpy spy(&window, property.notifySignal());
94
95 QVERIFY(property.write(&window, 42));
96 QCOMPARE(spy.count(), 1);
97}
98//! [8]
auto signalIndex
auto mo
[7]
auto signal
QSignalSpy spy(myCustomObject, SIGNAL(mySignal(int, QString, double)))
[0]
QCheckBox * box
[0]
QList< QVariant > arguments
QCOMPARE(spy.count(), 1)
CallableWithState object
QVERIFY(db.isValid())