1 /****************************************************************************
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtTest module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
45 #include <QtCore/qbytearray.h>
46 #include <QtCore/qlist.h>
47 #include <QtCore/qobject.h>
48 #include <QtCore/qmetaobject.h>
49 #include <QtCore/qvariant.h>
59 /* ### Qt5: change the class to use regular BC mechanisms, such that we can
60 * implement things like suggested in task 160192. */
61 class QSignalSpy
: public QObject
, public QList
<QList
<QVariant
> >
64 QSignalSpy(QObject
*obj
, const char *aSignal
)
67 const int memberOffset
= QObject::staticMetaObject
.methodCount();
69 static const int memberOffset
= QObject::staticMetaObject
.methodCount();
74 if (((aSignal
[0] - '0') & 0x03) != QSIGNAL_CODE
) {
75 qWarning("QSignalSpy: Not a valid signal, use the SIGNAL macro");
79 QByteArray ba
= QMetaObject::normalizedSignature(aSignal
+ 1);
80 const QMetaObject
*mo
= obj
->metaObject();
81 int sigIndex
= mo
->indexOfMethod(ba
.constData());
83 qWarning("QSignalSpy: No such signal: '%s'", ba
.constData());
87 if (!QMetaObject::connect(obj
, sigIndex
, this, memberOffset
,
88 Qt::DirectConnection
, 0)) {
89 qWarning("QSignalSpy: QMetaObject::connect returned false. Unable to connect.");
93 initArgs(mo
->method(sigIndex
));
96 inline bool isValid() const { return !sig
.isEmpty(); }
97 inline QByteArray
signal() const { return sig
; }
100 int qt_metacall(QMetaObject::Call call
, int methodId
, void **a
)
102 methodId
= QObject::qt_metacall(call
, methodId
, a
);
106 if (call
== QMetaObject::InvokeMetaMethod
) {
116 void initArgs(const QMetaMethod
&member
)
118 QList
<QByteArray
> params
= member
.parameterTypes();
119 for (int i
= 0; i
< params
.count(); ++i
) {
120 int tp
= QMetaType::type(params
.at(i
).constData());
121 if (tp
== QMetaType::Void
)
122 qWarning("Don't know how to handle '%s', use qRegisterMetaType to register it.",
123 params
.at(i
).constData());
128 void appendArgs(void **a
)
130 QList
<QVariant
> list
;
131 for (int i
= 0; i
< args
.count(); ++i
) {
132 QMetaType::Type type
= static_cast<QMetaType::Type
>(args
.at(i
));
133 list
<< QVariant(type
, a
[i
+ 1]);
138 // the full, normalized signal name
140 // holds the QMetaType types for the argument list of the signal