QtBase  v6.3.1
doc_src_qsignalspy.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
52 QCheckBox *box = ...;
53 QSignalSpy spy(box, SIGNAL(clicked(bool)));
54 
55 // do something that triggers the signal
56 box->animateClick();
57 
58 QCOMPARE(spy.count(), 1); // make sure the signal was emitted exactly one time
59 QList<QVariant> arguments = spy.takeFirst(); // take the first signal
60 
61 QVERIFY(arguments.at(0).toBool() == true); // verify the first argument
63 
64 
66 QSignalSpy spy(myCustomObject, SIGNAL(mySignal(int,QString,double)));
67 
68 myCustomObject->doSomething(); // trigger emission of the signal
69 
75 
76 
79 QSignalSpy spy(&model, SIGNAL(whatever(SomeStruct)));
81 
82 
84 // get the first argument from the first received signal:
85 SomeStruct result = qvariant_cast<SomeStruct>(spy.at(0).at(0));
87 
88 
90 QSignalSpy spy(myPushButton, SIGNAL(clicked(bool)));
92 
94 QVERIFY(spy.wait(1000));
96 
100 
103 auto mo = object.metaObject();
104 auto signalIndex = mo->indexOfSignal("objectNameChanged(QString)");
105 auto signal = mo->method(signalIndex);
106 
107 QSignalSpy spy(&object, signal);
108 object.setObjectName("A new object name");
109 QCOMPARE(spy.count(), 1);
111 
113 void tst_QWindow::writeMinMaxDimensionalProps_data()
114  QTest::addColumn<int>("propertyIndex");
115 
116  // Collect all relevant properties
117  static const auto mo = QWindow::staticMetaObject;
118  for (int i = mo.propertyOffset(); i < mo.propertyCount(); ++i) {
119  auto property = mo.property(i);
120 
121  // ...that have type int
122  if (property.type() == QVariant::Int) {
123  static const QRegularExpression re("^minimum|maximum");
124  const auto name = property.name();
125 
126  // ...and start with "minimum" or "maximum"
127  if (re.match(name).hasMatch()) {
128  QTest::addRow("%s", name) << i;
129  }
130  }
131  }
132 }
133 
134 void tst_QWindow::writeMinMaxDimensionalProps()
135 {
136  QFETCH(int, propertyIndex);
137 
138  auto property = QWindow::staticMetaObject.property(propertyIndex);
139  QVERIFY(property.isWritable());
140  QVERIFY(property.hasNotifySignal());
141 
142  QWindow window;
143  QSignalSpy spy(&window, property.notifySignal());
144 
145  QVERIFY(property.write(&window, 42));
146  QCOMPARE(spy.count(), 1);
147 }
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
@ Double
void clicked(bool checked=false)
The QCheckBox widget provides a checkbox with a text label.
Definition: qcheckbox.h:55
const_reference at(qsizetype i) const noexcept
Definition: qlist.h:457
value_type takeFirst()
Definition: qlist.h:564
qsizetype count() const noexcept
Definition: qlist.h:415
The QObject class is the base class of all Qt objects.
Definition: qobject.h:125
QVariant property(const char *name) const
Definition: qobject.cpp:4118
The QRegularExpression class provides pattern matching using regular expressions.
QRegularExpressionMatch match(const QString &subject, qsizetype offset=0, MatchType matchType=NormalMatch, MatchOptions matchOptions=NoMatchOption) const
bool wait(int timeout=5000)
Definition: qsignalspy.h:133
The QString class provides a Unicode character string.
Definition: qstring.h:388
bool toBool() const
Definition: qvariant.cpp:1906
int typeId() const
Definition: qvariant.h:241
The QWindow class represents a window in the underlying windowing system.
Definition: qwindow.h:99
auto signalIndex
auto signal
for(int i=mo.propertyOffset();i< mo.propertyCount();++i)
[0]
QVERIFY(arguments.at(0).toBool()==true)
qRegisterMetaType< SomeStruct >()
[1]
auto mo
[7]
QSignalSpy spy(myCustomObject, SIGNAL(mySignal(int, QString, double)))
[0]
QList< QVariant > arguments
QSignalSpy spy & model(whatever(SomeStruct))
QCOMPARE(spy.count(), 1)
QObject object
[6]
int Int
Definition: ftraster.c:307
auto it unsigned count const
Definition: hb-iter.hh:848
[15]
Definition: tst_encoder.cpp:33
void addColumn(const char *name, T *=nullptr)
Definition: qtestcase.h:388
Q_TESTLIB_EXPORT QTestData & addRow(const char *format,...) Q_ATTRIBUTE_FORMAT_PRINTF(1
Definition: qtestcase.cpp:2690
#define QString()
Definition: parse-defines.h:51
#define SIGNAL(a)
Definition: qobjectdefs.h:88
GLuint name
GLuint64EXT * result
[6]
Definition: qopenglext.h:10932
#define QFETCH(Type, name)
Definition: qtestcase.h:230
const char property[13]
Definition: qwizard.cpp:136
aWidget window() -> setWindowTitle("New Window Title")
[2]
Definition: jquant2.c:237