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
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
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
16
17
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);
28
29
32QSignalSpy spy(&model, SIGNAL(whatever(SomeStruct)));
34
35
37// get the first argument from the first received signal:
38SomeStruct result = qvariant_cast<SomeStruct>(spy.at(0).at(0));
40
41
43QSignalSpy spy(myPushButton, SIGNAL(clicked(bool)));
45
46
50
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);
61
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
93 QSignalSpy spy(&window, property.notifySignal());
94
95 QVERIFY(property.write(&window, 42));
96 QCOMPARE(spy.count(), 1);
97}
void clicked(bool checked=false)
This signal is emitted when the button is activated (i.e., pressed down then released while the mouse...
The QCheckBox widget provides a checkbox with a text label.
Definition qcheckbox.h:19
const_reference at(qsizetype i) const noexcept
Definition qlist.h:446
value_type takeFirst()
Definition qlist.h:566
qsizetype count() const noexcept
Definition qlist.h:398
\inmodule QtCore
Definition qobject.h:103
\inmodule QtCore \reentrant
\inmodule QtTest
Definition qsignalspy.h:22
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool toBool() const
Returns the variant as a bool if the variant has userType() Bool.
int typeId() const
Returns the storage type of the value stored in the variant.
Definition qvariant.h:340
\inmodule QtGui
Definition qwindow.h:63
auto signalIndex
auto signal
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]
QList< QVariant > arguments
QObject object
[6]
Q_TESTLIB_EXPORT QTestData & addRow(const char *format,...) Q_ATTRIBUTE_FORMAT_PRINTF(1
#define SIGNAL(a)
Definition qobjectdefs.h:53
GLuint name
GLsizei const GLint * box
GLuint64EXT * result
[6]
#define QFETCH(Type, name)
Definition qtestcase.h:278
#define QCOMPARE(actual, expected)
Definition qtestcase.h:81
#define QVERIFY(statement)
Definition qtestcase.h:58
const char property[13]
Definition qwizard.cpp:101
QSqlQueryModel * model
[16]
aWidget window() -> setWindowTitle("New Window Title")
[2]