Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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]
5
QCheckBox
*
box
= ...;
6
QSignalSpy
spy
(
box
, SIGNAL(clicked(
bool
)));
7
8
// do something that triggers the signal
9
box->animateClick();
10
11
QCOMPARE
(
spy
.count(), 1);
// make sure the signal was emitted exactly one time
12
QList
<
QVariant
>
arguments
= spy.takeFirst();
// take the first signal
13
14
QVERIFY
(
arguments
.at(0).toBool() ==
true
);
// verify the first argument
15
//! [0]
16
17
18
//! [1]
19
QSignalSpy
spy
(myCustomObject, SIGNAL(mySignal(
int
,QString,
double
)));
20
21
myCustomObject->doSomething();
// trigger emission of the signal
22
23
QList
<
QVariant
>
arguments
= spy.takeFirst();
24
QVERIFY
(arguments.at(0).typeId() == QMetaType::Int);
25
QVERIFY
(arguments.at(1).typeId() == QMetaType::QString);
26
QVERIFY
(arguments.at(2).typeId() == QMetaType::Double);
27
//! [1]
28
29
30
//! [2]
31
qRegisterMetaType<SomeStruct>();
32
QSignalSpy
spy
(&model, SIGNAL(whatever(SomeStruct)));
33
//! [2]
34
35
36
//! [3]
37
// get the first argument from the first received signal:
38
SomeStruct
result
= qvariant_cast<SomeStruct>(spy.at(0).at(0));
39
//! [3]
40
41
42
//! [4]
43
QSignalSpy
spy
(myPushButton, SIGNAL(clicked(
bool
)));
44
//! [4]
45
46
47
//! [6]
48
QSignalSpy
spy
(myPushButton, &QPushButton::clicked);
49
//! [6]
50
51
//! [7]
52
QObject
object
;
53
auto
mo
= object.metaObject();
54
auto
signalIndex
= mo->indexOfSignal(
"objectNameChanged(QString)"
);
55
auto
signal
= mo->method(signalIndex);
56
57
QSignalSpy
spy
(&object, signal);
58
object.setObjectName(
"A new object name"
);
59
QCOMPARE
(
spy
.count(), 1);
60
//! [7]
61
62
//! [8]
63
void
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
84
void
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]
signalIndex
auto signalIndex
Definition
doc_src_qsignalspy.cpp:54
mo
auto mo
[7]
Definition
doc_src_qsignalspy.cpp:53
signal
auto signal
Definition
doc_src_qsignalspy.cpp:55
spy
QSignalSpy spy(myCustomObject, SIGNAL(mySignal(int, QString, double)))
[0]
box
QCheckBox * box
[0]
Definition
doc_src_qsignalspy.cpp:5
arguments
QList< QVariant > arguments
Definition
doc_src_qsignalspy.cpp:12
QCOMPARE
QCOMPARE(spy.count(), 1)
result
auto result
[6]
Definition
src_concurrent_qtconcurrenttask.cpp:24
object
CallableWithState object
Definition
src_concurrent_qtconcurrenttask.cpp:65
QVERIFY
QVERIFY(db.isValid())
qtbase
src
testlib
doc
snippets
code
doc_src_qsignalspy.cpp
Generated on
for Qt by
1.18.0