Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/kernel/qobject.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||
4 | ** Copyright (C) 2016 Intel Corporation. | - | ||||||||||||
5 | ** Copyright (C) 2013 Olivier Goffart <ogoffart@woboq.com> | - | ||||||||||||
6 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||
7 | ** | - | ||||||||||||
8 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||
9 | ** | - | ||||||||||||
10 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||
11 | ** Commercial License Usage | - | ||||||||||||
12 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
13 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
14 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
15 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
16 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||
17 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||
18 | ** | - | ||||||||||||
19 | ** GNU Lesser General Public License Usage | - | ||||||||||||
20 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
21 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||
22 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||
23 | ** packaging of this file. Please review the following information to | - | ||||||||||||
24 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||
25 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||
26 | ** | - | ||||||||||||
27 | ** GNU General Public License Usage | - | ||||||||||||
28 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||
29 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||
30 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||
31 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||
32 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||
33 | ** included in the packaging of this file. Please review the following | - | ||||||||||||
34 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||
35 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||
36 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||
37 | ** | - | ||||||||||||
38 | ** $QT_END_LICENSE$ | - | ||||||||||||
39 | ** | - | ||||||||||||
40 | ****************************************************************************/ | - | ||||||||||||
41 | - | |||||||||||||
42 | #include "qobject.h" | - | ||||||||||||
43 | #include "qobject_p.h" | - | ||||||||||||
44 | #include "qmetaobject_p.h" | - | ||||||||||||
45 | - | |||||||||||||
46 | #include "qabstracteventdispatcher.h" | - | ||||||||||||
47 | #include "qabstracteventdispatcher_p.h" | - | ||||||||||||
48 | #include "qcoreapplication.h" | - | ||||||||||||
49 | #include "qcoreapplication_p.h" | - | ||||||||||||
50 | #include "qvariant.h" | - | ||||||||||||
51 | #include "qmetaobject.h" | - | ||||||||||||
52 | #include <qregexp.h> | - | ||||||||||||
53 | #include <qregularexpression.h> | - | ||||||||||||
54 | #include <qthread.h> | - | ||||||||||||
55 | #include <private/qthread_p.h> | - | ||||||||||||
56 | #include <qdebug.h> | - | ||||||||||||
57 | #include <qpair.h> | - | ||||||||||||
58 | #include <qvarlengtharray.h> | - | ||||||||||||
59 | #include <qset.h> | - | ||||||||||||
60 | #include <qsemaphore.h> | - | ||||||||||||
61 | #include <qsharedpointer.h> | - | ||||||||||||
62 | - | |||||||||||||
63 | #include <private/qorderedmutexlocker_p.h> | - | ||||||||||||
64 | #include <private/qhooks_p.h> | - | ||||||||||||
65 | - | |||||||||||||
66 | #include <new> | - | ||||||||||||
67 | - | |||||||||||||
68 | #include <ctype.h> | - | ||||||||||||
69 | #include <limits.h> | - | ||||||||||||
70 | - | |||||||||||||
71 | QT_BEGIN_NAMESPACE | - | ||||||||||||
72 | - | |||||||||||||
73 | static int DIRECT_CONNECTION_ONLY = 0; | - | ||||||||||||
74 | - | |||||||||||||
75 | - | |||||||||||||
76 | QDynamicMetaObjectData::~QDynamicMetaObjectData() | - | ||||||||||||
77 | { | - | ||||||||||||
78 | } | - | ||||||||||||
79 | - | |||||||||||||
80 | QAbstractDynamicMetaObject::~QAbstractDynamicMetaObject() | - | ||||||||||||
81 | { | - | ||||||||||||
82 | } | - | ||||||||||||
83 | - | |||||||||||||
84 | - | |||||||||||||
85 | struct QSlotObjectBaseDeleter { // for use with QScopedPointer<QSlotObjectBase,...> | - | ||||||||||||
86 | static void cleanup(QtPrivate::QSlotObjectBase *slot) { | - | ||||||||||||
87 | if (slot) slot->destroyIfLastRef(); | - | ||||||||||||
88 | } | - | ||||||||||||
89 | }; | - | ||||||||||||
90 | static int *queuedConnectionTypes(const QList<QByteArray> &typeNames) | - | ||||||||||||
91 | { | - | ||||||||||||
92 | int *types = new int [typeNames.count() + 1]; | - | ||||||||||||
93 | Q_CHECK_PTR(types); | - | ||||||||||||
94 | for (int i = 0; i < typeNames.count(); ++i) { | - | ||||||||||||
95 | const QByteArray typeName = typeNames.at(i); | - | ||||||||||||
96 | if (typeName.endsWith('*')) | - | ||||||||||||
97 | types[i] = QMetaType::VoidStar; | - | ||||||||||||
98 | else | - | ||||||||||||
99 | types[i] = QMetaType::type(typeName); | - | ||||||||||||
100 | - | |||||||||||||
101 | if (!types[i]) { | - | ||||||||||||
102 | qWarning("QObject::connect: Cannot queue arguments of type '%s'\n" | - | ||||||||||||
103 | "(Make sure '%s' is registered using qRegisterMetaType().)", | - | ||||||||||||
104 | typeName.constData(), typeName.constData()); | - | ||||||||||||
105 | delete [] types; | - | ||||||||||||
106 | return 0; | - | ||||||||||||
107 | } | - | ||||||||||||
108 | } | - | ||||||||||||
109 | types[typeNames.count()] = 0; | - | ||||||||||||
110 | - | |||||||||||||
111 | return types; | - | ||||||||||||
112 | } | - | ||||||||||||
113 | - | |||||||||||||
114 | static int *queuedConnectionTypes(const QArgumentType *argumentTypes, int argc) | - | ||||||||||||
115 | { | - | ||||||||||||
116 | QScopedArrayPointer<int> types(new int [argc + 1]); | - | ||||||||||||
117 | for (int i = 0; i < argc; ++i) { | - | ||||||||||||
118 | const QArgumentType &type = argumentTypes[i]; | - | ||||||||||||
119 | if (type.type()) | - | ||||||||||||
120 | types[i] = type.type(); | - | ||||||||||||
121 | else if (type.name().endsWith('*')) | - | ||||||||||||
122 | types[i] = QMetaType::VoidStar; | - | ||||||||||||
123 | else | - | ||||||||||||
124 | types[i] = QMetaType::type(type.name()); | - | ||||||||||||
125 | - | |||||||||||||
126 | if (!types[i]) { | - | ||||||||||||
127 | qWarning("QObject::connect: Cannot queue arguments of type '%s'\n" | - | ||||||||||||
128 | "(Make sure '%s' is registered using qRegisterMetaType().)", | - | ||||||||||||
129 | type.name().constData(), type.name().constData()); | - | ||||||||||||
130 | return 0; | - | ||||||||||||
131 | } | - | ||||||||||||
132 | } | - | ||||||||||||
133 | types[argc] = 0; | - | ||||||||||||
134 | - | |||||||||||||
135 | return types.take(); | - | ||||||||||||
136 | } | - | ||||||||||||
137 | - | |||||||||||||
138 | static QBasicMutex _q_ObjectMutexPool[131]; | - | ||||||||||||
139 | - | |||||||||||||
140 | /** | - | ||||||||||||
141 | * \internal | - | ||||||||||||
142 | * mutex to be locked when accessing the connectionlists or the senders list | - | ||||||||||||
143 | */ | - | ||||||||||||
144 | static inline QMutex *signalSlotLock(const QObject *o) | - | ||||||||||||
145 | { | - | ||||||||||||
146 | return static_cast<QMutex *>(&_q_ObjectMutexPool[ | - | ||||||||||||
147 | uint(quintptr(o)) % sizeof(_q_ObjectMutexPool)/sizeof(QBasicMutex)]); | - | ||||||||||||
148 | } | - | ||||||||||||
149 | - | |||||||||||||
150 | // ### Qt >= 5.6, remove qt_add/removeObject | - | ||||||||||||
151 | extern "C" Q_CORE_EXPORT void qt_addObject(QObject *) | - | ||||||||||||
152 | {} | - | ||||||||||||
153 | - | |||||||||||||
154 | extern "C" Q_CORE_EXPORT void qt_removeObject(QObject *) | - | ||||||||||||
155 | {} | - | ||||||||||||
156 | - | |||||||||||||
157 | struct QConnectionSenderSwitcher { | - | ||||||||||||
158 | QObject *receiver; | - | ||||||||||||
159 | QObjectPrivate::Sender *previousSender; | - | ||||||||||||
160 | QObjectPrivate::Sender currentSender; | - | ||||||||||||
161 | bool switched; | - | ||||||||||||
162 | - | |||||||||||||
163 | inline QConnectionSenderSwitcher() : switched(false) {} | - | ||||||||||||
164 | - | |||||||||||||
165 | inline QConnectionSenderSwitcher(QObject *receiver, QObject *sender, int signal_absolute_id) | - | ||||||||||||
166 | { | - | ||||||||||||
167 | switchSender(receiver, sender, signal_absolute_id); | - | ||||||||||||
168 | } | - | ||||||||||||
169 | - | |||||||||||||
170 | inline void switchSender(QObject *receiver, QObject *sender, int signal_absolute_id) | - | ||||||||||||
171 | { | - | ||||||||||||
172 | this->receiver = receiver; | - | ||||||||||||
173 | currentSender.sender = sender; | - | ||||||||||||
174 | currentSender.signal = signal_absolute_id; | - | ||||||||||||
175 | currentSender.ref = 1; | - | ||||||||||||
176 | previousSender = QObjectPrivate::setCurrentSender(receiver, ¤tSender); | - | ||||||||||||
177 | switched = true; | - | ||||||||||||
178 | } | - | ||||||||||||
179 | - | |||||||||||||
180 | inline ~QConnectionSenderSwitcher() | - | ||||||||||||
181 | { | - | ||||||||||||
182 | if (switched) | - | ||||||||||||
183 | QObjectPrivate::resetCurrentSender(receiver, ¤tSender, previousSender); | - | ||||||||||||
184 | } | - | ||||||||||||
185 | private: | - | ||||||||||||
186 | Q_DISABLE_COPY(QConnectionSenderSwitcher) | - | ||||||||||||
187 | }; | - | ||||||||||||
188 | - | |||||||||||||
189 | - | |||||||||||||
190 | void (*QAbstractDeclarativeData::destroyed)(QAbstractDeclarativeData *, QObject *) = 0; | - | ||||||||||||
191 | void (*QAbstractDeclarativeData::destroyed_qml1)(QAbstractDeclarativeData *, QObject *) = 0; | - | ||||||||||||
192 | void (*QAbstractDeclarativeData::parentChanged)(QAbstractDeclarativeData *, QObject *, QObject *) = 0; | - | ||||||||||||
193 | void (*QAbstractDeclarativeData::signalEmitted)(QAbstractDeclarativeData *, QObject *, int, void **) = 0; | - | ||||||||||||
194 | int (*QAbstractDeclarativeData::receivers)(QAbstractDeclarativeData *, const QObject *, int) = 0; | - | ||||||||||||
195 | bool (*QAbstractDeclarativeData::isSignalConnected)(QAbstractDeclarativeData *, const QObject *, int) = 0; | - | ||||||||||||
196 | void (*QAbstractDeclarativeData::setWidgetParent)(QObject *, QObject *) = 0; | - | ||||||||||||
197 | - | |||||||||||||
198 | QObjectData::~QObjectData() {} | - | ||||||||||||
199 | - | |||||||||||||
200 | QMetaObject *QObjectData::dynamicMetaObject() const | - | ||||||||||||
201 | { | - | ||||||||||||
202 | return metaObject->toDynamicMetaObject(q_ptr); | - | ||||||||||||
203 | } | - | ||||||||||||
204 | - | |||||||||||||
205 | QObjectPrivate::QObjectPrivate(int version) | - | ||||||||||||
206 | : threadData(0), connectionLists(0), senders(0), currentSender(0), currentChildBeingDeleted(0) | - | ||||||||||||
207 | { | - | ||||||||||||
208 | #ifdef QT_BUILD_INTERNAL | - | ||||||||||||
209 | // Don't check the version parameter in internal builds. | - | ||||||||||||
210 | // This allows incompatible versions to be loaded, possibly for testing. | - | ||||||||||||
211 | Q_UNUSED(version); | - | ||||||||||||
212 | #else | - | ||||||||||||
213 | if (Q_UNLIKELY(version != QObjectPrivateVersion))) | - | ||||||||||||
214 | qFatal("Cannot mix incompatible Qt library (version 0x%x) with this library (version 0x%x)", | - | ||||||||||||
215 | version, QObjectPrivateVersion); | - | ||||||||||||
216 | #endif | - | ||||||||||||
217 | - | |||||||||||||
218 | // QObjectData initialization | - | ||||||||||||
219 | q_ptr = 0; | - | ||||||||||||
220 | parent = 0; // no parent yet. It is set by setParent() | - | ||||||||||||
221 | isWidget = false; // assume not a widget object | - | ||||||||||||
222 | blockSig = false; // not blocking signals | - | ||||||||||||
223 | wasDeleted = false; // double-delete catcher | - | ||||||||||||
224 | isDeletingChildren = false; // set by deleteChildren() | - | ||||||||||||
225 | sendChildEvents = true; // if we should send ChildInsert and ChildRemove events to parent | - | ||||||||||||
226 | receiveChildEvents = true; | - | ||||||||||||
227 | postedEvents = 0; | - | ||||||||||||
228 | extraData = 0; | - | ||||||||||||
229 | connectedSignals[0] = connectedSignals[1] = 0; | - | ||||||||||||
230 | metaObject = 0; | - | ||||||||||||
231 | isWindow = false; | - | ||||||||||||
232 | } executed 1911164 times by 907 tests: end of block Executed by:
| 1911164 | ||||||||||||
233 | - | |||||||||||||
234 | QObjectPrivate::~QObjectPrivate() | - | ||||||||||||
235 | { | - | ||||||||||||
236 | if (extraData && !extraData->runningTimers.isEmpty()) { | - | ||||||||||||
237 | if (Q_LIKELY(threadData->thread == QThread::currentThread())) { | - | ||||||||||||
238 | // unregister pending timers | - | ||||||||||||
239 | if (threadData->eventDispatcher.load()) | - | ||||||||||||
240 | threadData->eventDispatcher.load()->unregisterTimers(q_ptr); | - | ||||||||||||
241 | - | |||||||||||||
242 | // release the timer ids back to the pool | - | ||||||||||||
243 | for (int i = 0; i < extraData->runningTimers.size(); ++i) | - | ||||||||||||
244 | QAbstractEventDispatcherPrivate::releaseTimerId(extraData->runningTimers.at(i)); | - | ||||||||||||
245 | } else { | - | ||||||||||||
246 | qWarning("QObject::~QObject: Timers cannot be stopped from another thread"); | - | ||||||||||||
247 | } | - | ||||||||||||
248 | } | - | ||||||||||||
249 | - | |||||||||||||
250 | if (postedEvents) | - | ||||||||||||
251 | QCoreApplication::removePostedEvents(q_ptr, 0); | - | ||||||||||||
252 | - | |||||||||||||
253 | threadData->deref(); | - | ||||||||||||
254 | - | |||||||||||||
255 | if (metaObject) metaObject->objectDestroyed(q_ptr); | - | ||||||||||||
256 | - | |||||||||||||
257 | #ifndef QT_NO_USERDATA | - | ||||||||||||
258 | if (extraData) | - | ||||||||||||
259 | qDeleteAll(extraData->userData); | - | ||||||||||||
260 | #endif | - | ||||||||||||
261 | delete extraData; | - | ||||||||||||
262 | } | - | ||||||||||||
263 | - | |||||||||||||
264 | /*! | - | ||||||||||||
265 | \internal | - | ||||||||||||
266 | For a given metaobject, compute the signal offset, and the method offset (including signals) | - | ||||||||||||
267 | */ | - | ||||||||||||
268 | static void computeOffsets(const QMetaObject *metaobject, int *signalOffset, int *methodOffset) | - | ||||||||||||
269 | { | - | ||||||||||||
270 | *signalOffset = *methodOffset = 0; | - | ||||||||||||
271 | const QMetaObject *m = metaobject->d.superdata; | - | ||||||||||||
272 | while (m) { | - | ||||||||||||
273 | const QMetaObjectPrivate *d = QMetaObjectPrivate::get(m); | - | ||||||||||||
274 | *methodOffset += d->methodCount; | - | ||||||||||||
275 | Q_ASSERT(d->revision >= 4); | - | ||||||||||||
276 | *signalOffset += d->signalCount; | - | ||||||||||||
277 | m = m->d.superdata; | - | ||||||||||||
278 | } | - | ||||||||||||
279 | } | - | ||||||||||||
280 | - | |||||||||||||
281 | /* | - | ||||||||||||
282 | This vector contains the all connections from an object. | - | ||||||||||||
283 | - | |||||||||||||
284 | Each object may have one vector containing the lists of | - | ||||||||||||
285 | connections for a given signal. The index in the vector correspond | - | ||||||||||||
286 | to the signal index. The signal index is the one returned by | - | ||||||||||||
287 | QObjectPrivate::signalIndex (not QMetaObject::indexOfSignal). | - | ||||||||||||
288 | Negative index means connections to all signals. | - | ||||||||||||
289 | - | |||||||||||||
290 | This vector is protected by the object mutex (signalSlotMutexes()) | - | ||||||||||||
291 | - | |||||||||||||
292 | Each Connection is also part of a 'senders' linked list. The mutex | - | ||||||||||||
293 | of the receiver must be locked when touching the pointers of this | - | ||||||||||||
294 | linked list. | - | ||||||||||||
295 | */ | - | ||||||||||||
296 | class QObjectConnectionListVector : public QVector<QObjectPrivate::ConnectionList> | - | ||||||||||||
297 | { | - | ||||||||||||
298 | public: | - | ||||||||||||
299 | bool orphaned; //the QObject owner of this vector has been destroyed while the vector was inUse | - | ||||||||||||
300 | bool dirty; //some Connection have been disconnected (their receiver is 0) but not removed from the list yet | - | ||||||||||||
301 | int inUse; //number of functions that are currently accessing this object or its connections | - | ||||||||||||
302 | QObjectPrivate::ConnectionList allsignals; | - | ||||||||||||
303 | - | |||||||||||||
304 | QObjectConnectionListVector() | - | ||||||||||||
305 | : QVector<QObjectPrivate::ConnectionList>(), orphaned(false), dirty(false), inUse(0) | - | ||||||||||||
306 | { } | - | ||||||||||||
307 | - | |||||||||||||
308 | QObjectPrivate::ConnectionList &operator[](int at) | - | ||||||||||||
309 | { | - | ||||||||||||
310 | if (at < 0) | - | ||||||||||||
311 | return allsignals; | - | ||||||||||||
312 | return QVector<QObjectPrivate::ConnectionList>::operator[](at); | - | ||||||||||||
313 | } | - | ||||||||||||
314 | }; | - | ||||||||||||
315 | - | |||||||||||||
316 | // Used by QAccessibleWidget | - | ||||||||||||
317 | bool QObjectPrivate::isSender(const QObject *receiver, const char *signal) const | - | ||||||||||||
318 | { | - | ||||||||||||
319 | Q_Q(const QObject); | - | ||||||||||||
320 | int signal_index = signalIndex(signal); | - | ||||||||||||
321 | if (signal_index < 0) | - | ||||||||||||
322 | return false; | - | ||||||||||||
323 | QMutexLocker locker(signalSlotLock(q)); | - | ||||||||||||
324 | if (connectionLists) { | - | ||||||||||||
325 | if (signal_index < connectionLists->count()) { | - | ||||||||||||
326 | const QObjectPrivate::Connection *c = | - | ||||||||||||
327 | connectionLists->at(signal_index).first; | - | ||||||||||||
328 | - | |||||||||||||
329 | while (c) { | - | ||||||||||||
330 | if (c->receiver == receiver) | - | ||||||||||||
331 | return true; | - | ||||||||||||
332 | c = c->nextConnectionList; | - | ||||||||||||
333 | } | - | ||||||||||||
334 | } | - | ||||||||||||
335 | } | - | ||||||||||||
336 | return false; | - | ||||||||||||
337 | } | - | ||||||||||||
338 | - | |||||||||||||
339 | // Used by QAccessibleWidget | - | ||||||||||||
340 | QObjectList QObjectPrivate::receiverList(const char *signal) const | - | ||||||||||||
341 | { | - | ||||||||||||
342 | Q_Q(const QObject); | - | ||||||||||||
343 | QObjectList returnValue; | - | ||||||||||||
344 | int signal_index = signalIndex(signal); | - | ||||||||||||
345 | if (signal_index < 0) | - | ||||||||||||
346 | return returnValue; | - | ||||||||||||
347 | QMutexLocker locker(signalSlotLock(q)); | - | ||||||||||||
348 | if (connectionLists) { | - | ||||||||||||
349 | if (signal_index < connectionLists->count()) { | - | ||||||||||||
350 | const QObjectPrivate::Connection *c = connectionLists->at(signal_index).first; | - | ||||||||||||
351 | - | |||||||||||||
352 | while (c) { | - | ||||||||||||
353 | if (c->receiver) | - | ||||||||||||
354 | returnValue << c->receiver; | - | ||||||||||||
355 | c = c->nextConnectionList; | - | ||||||||||||
356 | } | - | ||||||||||||
357 | } | - | ||||||||||||
358 | } | - | ||||||||||||
359 | return returnValue; | - | ||||||||||||
360 | } | - | ||||||||||||
361 | - | |||||||||||||
362 | // Used by QAccessibleWidget | - | ||||||||||||
363 | QObjectList QObjectPrivate::senderList() const | - | ||||||||||||
364 | { | - | ||||||||||||
365 | QObjectList returnValue; | - | ||||||||||||
366 | QMutexLocker locker(signalSlotLock(q_func())); | - | ||||||||||||
367 | for (Connection *c = senders; c; c = c->next) | - | ||||||||||||
368 | returnValue << c->sender; | - | ||||||||||||
369 | return returnValue; | - | ||||||||||||
370 | } | - | ||||||||||||
371 | - | |||||||||||||
372 | /*! | - | ||||||||||||
373 | \internal | - | ||||||||||||
374 | Add the connection \a c to the list of connections of the sender's object | - | ||||||||||||
375 | for the specified \a signal | - | ||||||||||||
376 | - | |||||||||||||
377 | The signalSlotLock() of the sender and receiver must be locked while calling | - | ||||||||||||
378 | this function | - | ||||||||||||
379 | - | |||||||||||||
380 | Will also add the connection in the sender's list of the receiver. | - | ||||||||||||
381 | */ | - | ||||||||||||
382 | void QObjectPrivate::addConnection(int signal, Connection *c) | - | ||||||||||||
383 | { | - | ||||||||||||
384 | Q_ASSERT(c->sender == q_ptr); | - | ||||||||||||
385 | if (!connectionLists) | - | ||||||||||||
386 | connectionLists = new QObjectConnectionListVector(); | - | ||||||||||||
387 | if (signal >= connectionLists->count()) | - | ||||||||||||
388 | connectionLists->resize(signal + 1); | - | ||||||||||||
389 | - | |||||||||||||
390 | ConnectionList &connectionList = (*connectionLists)[signal]; | - | ||||||||||||
391 | if (connectionList.last) { | - | ||||||||||||
392 | connectionList.last->nextConnectionList = c; | - | ||||||||||||
393 | } else { | - | ||||||||||||
394 | connectionList.first = c; | - | ||||||||||||
395 | } | - | ||||||||||||
396 | connectionList.last = c; | - | ||||||||||||
397 | - | |||||||||||||
398 | cleanConnectionLists(); | - | ||||||||||||
399 | - | |||||||||||||
400 | c->prev = &(QObjectPrivate::get(c->receiver)->senders); | - | ||||||||||||
401 | c->next = *c->prev; | - | ||||||||||||
402 | *c->prev = c; | - | ||||||||||||
403 | if (c->next) | - | ||||||||||||
404 | c->next->prev = &c->next; | - | ||||||||||||
405 | - | |||||||||||||
406 | if (signal < 0) { | - | ||||||||||||
407 | connectedSignals[0] = connectedSignals[1] = ~0; | - | ||||||||||||
408 | } else if (signal < (int)sizeof(connectedSignals) * 8) { | - | ||||||||||||
409 | connectedSignals[signal >> 5] |= (1 << (signal & 0x1f)); | - | ||||||||||||
410 | } | - | ||||||||||||
411 | } | - | ||||||||||||
412 | - | |||||||||||||
413 | void QObjectPrivate::cleanConnectionLists() | - | ||||||||||||
414 | { | - | ||||||||||||
415 | if (connectionLists->dirty && !connectionLists->inUse) { | - | ||||||||||||
416 | // remove broken connections | - | ||||||||||||
417 | for (int signal = -1; signal < connectionLists->count(); ++signal) { | - | ||||||||||||
418 | QObjectPrivate::ConnectionList &connectionList = | - | ||||||||||||
419 | (*connectionLists)[signal]; | - | ||||||||||||
420 | - | |||||||||||||
421 | // Set to the last entry in the connection list that was *not* | - | ||||||||||||
422 | // deleted. This is needed to update the list's last pointer | - | ||||||||||||
423 | // at the end of the cleanup. | - | ||||||||||||
424 | QObjectPrivate::Connection *last = 0; | - | ||||||||||||
425 | - | |||||||||||||
426 | QObjectPrivate::Connection **prev = &connectionList.first; | - | ||||||||||||
427 | QObjectPrivate::Connection *c = *prev; | - | ||||||||||||
428 | while (c) { | - | ||||||||||||
429 | if (c->receiver) { | - | ||||||||||||
430 | last = c; | - | ||||||||||||
431 | prev = &c->nextConnectionList; | - | ||||||||||||
432 | c = *prev; | - | ||||||||||||
433 | } else { | - | ||||||||||||
434 | QObjectPrivate::Connection *next = c->nextConnectionList; | - | ||||||||||||
435 | *prev = next; | - | ||||||||||||
436 | c->deref(); | - | ||||||||||||
437 | c = next; | - | ||||||||||||
438 | } | - | ||||||||||||
439 | } | - | ||||||||||||
440 | - | |||||||||||||
441 | // Correct the connection list's last pointer. | - | ||||||||||||
442 | // As conectionList.last could equal last, this could be a noop | - | ||||||||||||
443 | connectionList.last = last; | - | ||||||||||||
444 | } | - | ||||||||||||
445 | connectionLists->dirty = false; | - | ||||||||||||
446 | } | - | ||||||||||||
447 | } | - | ||||||||||||
448 | - | |||||||||||||
449 | /*! | - | ||||||||||||
450 | \internal | - | ||||||||||||
451 | */ | - | ||||||||||||
452 | QMetaCallEvent::QMetaCallEvent(ushort method_offset, ushort method_relative, QObjectPrivate::StaticMetaCallFunction callFunction, | - | ||||||||||||
453 | const QObject *sender, int signalId, | - | ||||||||||||
454 | int nargs, int *types, void **args, QSemaphore *semaphore) | - | ||||||||||||
455 | : QEvent(MetaCall), slotObj_(0), sender_(sender), signalId_(signalId), | - | ||||||||||||
456 | nargs_(nargs), types_(types), args_(args), semaphore_(semaphore), | - | ||||||||||||
457 | callFunction_(callFunction), method_offset_(method_offset), method_relative_(method_relative) | - | ||||||||||||
458 | { } | - | ||||||||||||
459 | - | |||||||||||||
460 | /*! | - | ||||||||||||
461 | \internal | - | ||||||||||||
462 | */ | - | ||||||||||||
463 | QMetaCallEvent::QMetaCallEvent(QtPrivate::QSlotObjectBase *slotO, const QObject *sender, int signalId, | - | ||||||||||||
464 | int nargs, int *types, void **args, QSemaphore *semaphore) | - | ||||||||||||
465 | : QEvent(MetaCall), slotObj_(slotO), sender_(sender), signalId_(signalId), | - | ||||||||||||
466 | nargs_(nargs), types_(types), args_(args), semaphore_(semaphore), | - | ||||||||||||
467 | callFunction_(0), method_offset_(0), method_relative_(ushort(-1)) | - | ||||||||||||
468 | { | - | ||||||||||||
469 | if (slotObj_) | - | ||||||||||||
470 | slotObj_->ref(); | - | ||||||||||||
471 | } | - | ||||||||||||
472 | - | |||||||||||||
473 | /*! | - | ||||||||||||
474 | \internal | - | ||||||||||||
475 | */ | - | ||||||||||||
476 | QMetaCallEvent::~QMetaCallEvent() | - | ||||||||||||
477 | { | - | ||||||||||||
478 | if (types_) { | - | ||||||||||||
479 | for (int i = 0; i < nargs_; ++i) { | - | ||||||||||||
480 | if (types_[i] && args_[i]) | - | ||||||||||||
481 | QMetaType::destroy(types_[i], args_[i]); | - | ||||||||||||
482 | } | - | ||||||||||||
483 | free(types_); | - | ||||||||||||
484 | free(args_); | - | ||||||||||||
485 | } | - | ||||||||||||
486 | #ifndef QT_NO_THREAD | - | ||||||||||||
487 | if (semaphore_) | - | ||||||||||||
488 | semaphore_->release(); | - | ||||||||||||
489 | #endif | - | ||||||||||||
490 | if (slotObj_) | - | ||||||||||||
491 | slotObj_->destroyIfLastRef(); | - | ||||||||||||
492 | } | - | ||||||||||||
493 | - | |||||||||||||
494 | /*! | - | ||||||||||||
495 | \internal | - | ||||||||||||
496 | */ | - | ||||||||||||
497 | void QMetaCallEvent::placeMetaCall(QObject *object) | - | ||||||||||||
498 | { | - | ||||||||||||
499 | if (slotObj_) { | - | ||||||||||||
500 | slotObj_->call(object, args_); | - | ||||||||||||
501 | } else if (callFunction_ && method_offset_ <= object->metaObject()->methodOffset()) { | - | ||||||||||||
502 | callFunction_(object, QMetaObject::InvokeMetaMethod, method_relative_, args_); | - | ||||||||||||
503 | } else { | - | ||||||||||||
504 | QMetaObject::metacall(object, QMetaObject::InvokeMetaMethod, method_offset_ + method_relative_, args_); | - | ||||||||||||
505 | } | - | ||||||||||||
506 | } | - | ||||||||||||
507 | - | |||||||||||||
508 | /*! | - | ||||||||||||
509 | \class QSignalBlocker | - | ||||||||||||
510 | \brief Exception-safe wrapper around QObject::blockSignals() | - | ||||||||||||
511 | \since 5.3 | - | ||||||||||||
512 | \ingroup objectmodel | - | ||||||||||||
513 | \inmodule QtCore | - | ||||||||||||
514 | - | |||||||||||||
515 | \reentrant | - | ||||||||||||
516 | - | |||||||||||||
517 | QSignalBlocker can be used whereever you would otherwise use a | - | ||||||||||||
518 | pair of calls to blockSignals(). It blocks signals in its | - | ||||||||||||
519 | constructor and in the destructor it resets the state to what | - | ||||||||||||
520 | it was before the constructor ran. | - | ||||||||||||
521 | - | |||||||||||||
522 | \code | - | ||||||||||||
523 | { | - | ||||||||||||
524 | const QSignalBlocker blocker(someQObject); | - | ||||||||||||
525 | // no signals here | - | ||||||||||||
526 | } | - | ||||||||||||
527 | \endcode | - | ||||||||||||
528 | is thus equivalent to | - | ||||||||||||
529 | \code | - | ||||||||||||
530 | const bool wasBlocked = someQObject->blockSignals(true); | - | ||||||||||||
531 | // no signals here | - | ||||||||||||
532 | someQObject->blockSignals(wasBlocked); | - | ||||||||||||
533 | \endcode | - | ||||||||||||
534 | - | |||||||||||||
535 | except the code using QSignalBlocker is safe in the face of | - | ||||||||||||
536 | exceptions. | - | ||||||||||||
537 | - | |||||||||||||
538 | \sa QMutexLocker, QEventLoopLocker | - | ||||||||||||
539 | */ | - | ||||||||||||
540 | - | |||||||||||||
541 | /*! | - | ||||||||||||
542 | \fn QSignalBlocker::QSignalBlocker(QObject *object) | - | ||||||||||||
543 | - | |||||||||||||
544 | Constructor. Calls \a{object}->blockSignals(true). | - | ||||||||||||
545 | */ | - | ||||||||||||
546 | - | |||||||||||||
547 | /*! | - | ||||||||||||
548 | \fn QSignalBlocker::QSignalBlocker(QObject &object) | - | ||||||||||||
549 | \overload | - | ||||||||||||
550 | - | |||||||||||||
551 | Calls \a{object}.blockSignals(true). | - | ||||||||||||
552 | */ | - | ||||||||||||
553 | - | |||||||||||||
554 | /*! | - | ||||||||||||
555 | \fn QSignalBlocker::QSignalBlocker(QSignalBlocker &&other) | - | ||||||||||||
556 | - | |||||||||||||
557 | Move-constructs a signal blocker from \a other. \a other will have | - | ||||||||||||
558 | a no-op destructor, while repsonsibility for restoring the | - | ||||||||||||
559 | QObject::signalsBlocked() state is transferred to the new object. | - | ||||||||||||
560 | */ | - | ||||||||||||
561 | - | |||||||||||||
562 | /*! | - | ||||||||||||
563 | \fn QSignalBlocker &QSignalBlocker::operator=(QSignalBlocker &&other) | - | ||||||||||||
564 | - | |||||||||||||
565 | Move-assigns this signal blocker from \a other. \a other will have | - | ||||||||||||
566 | a no-op destructor, while repsonsibility for restoring the | - | ||||||||||||
567 | QObject::signalsBlocked() state is transferred to this object. | - | ||||||||||||
568 | - | |||||||||||||
569 | The object's signals this signal blocker was blocking prior to | - | ||||||||||||
570 | being moved to, if any, are unblocked \e except in the case where | - | ||||||||||||
571 | both instances block the same object's signals and \c *this is | - | ||||||||||||
572 | unblocked while \a other is not, at the time of the move. | - | ||||||||||||
573 | */ | - | ||||||||||||
574 | - | |||||||||||||
575 | /*! | - | ||||||||||||
576 | \fn QSignalBlocker::~QSignalBlocker() | - | ||||||||||||
577 | - | |||||||||||||
578 | Destructor. Restores the QObject::signalsBlocked() state to what it | - | ||||||||||||
579 | was before the constructor ran, unless unblock() has been called | - | ||||||||||||
580 | without a following reblock(), in which case it does nothing. | - | ||||||||||||
581 | */ | - | ||||||||||||
582 | - | |||||||||||||
583 | /*! | - | ||||||||||||
584 | \fn void QSignalBlocker::reblock() | - | ||||||||||||
585 | - | |||||||||||||
586 | Re-blocks signals after a previous unblock(). | - | ||||||||||||
587 | - | |||||||||||||
588 | The numbers of reblock() and unblock() calls are not counted, so | - | ||||||||||||
589 | every reblock() undoes any number of unblock() calls. | - | ||||||||||||
590 | */ | - | ||||||||||||
591 | - | |||||||||||||
592 | /*! | - | ||||||||||||
593 | \fn void QSignalBlocker::unblock() | - | ||||||||||||
594 | - | |||||||||||||
595 | Temporarily restores the QObject::signalsBlocked() state to what | - | ||||||||||||
596 | it was before this QSignaBlocker's constructor ran. To undo, use | - | ||||||||||||
597 | reblock(). | - | ||||||||||||
598 | - | |||||||||||||
599 | The numbers of reblock() and unblock() calls are not counted, so | - | ||||||||||||
600 | every unblock() undoes any number of reblock() calls. | - | ||||||||||||
601 | */ | - | ||||||||||||
602 | - | |||||||||||||
603 | /*! | - | ||||||||||||
604 | \class QObject | - | ||||||||||||
605 | \inmodule QtCore | - | ||||||||||||
606 | \brief The QObject class is the base class of all Qt objects. | - | ||||||||||||
607 | - | |||||||||||||
608 | \ingroup objectmodel | - | ||||||||||||
609 | - | |||||||||||||
610 | \reentrant | - | ||||||||||||
611 | - | |||||||||||||
612 | QObject is the heart of the Qt \l{Object Model}. The central | - | ||||||||||||
613 | feature in this model is a very powerful mechanism for seamless | - | ||||||||||||
614 | object communication called \l{signals and slots}. You can | - | ||||||||||||
615 | connect a signal to a slot with connect() and destroy the | - | ||||||||||||
616 | connection with disconnect(). To avoid never ending notification | - | ||||||||||||
617 | loops you can temporarily block signals with blockSignals(). The | - | ||||||||||||
618 | protected functions connectNotify() and disconnectNotify() make | - | ||||||||||||
619 | it possible to track connections. | - | ||||||||||||
620 | - | |||||||||||||
621 | QObjects organize themselves in \l {Object Trees & Ownership} | - | ||||||||||||
622 | {object trees}. When you create a QObject with another object as | - | ||||||||||||
623 | parent, the object will automatically add itself to the parent's | - | ||||||||||||
624 | children() list. The parent takes ownership of the object; i.e., | - | ||||||||||||
625 | it will automatically delete its children in its destructor. You | - | ||||||||||||
626 | can look for an object by name and optionally type using | - | ||||||||||||
627 | findChild() or findChildren(). | - | ||||||||||||
628 | - | |||||||||||||
629 | Every object has an objectName() and its class name can be found | - | ||||||||||||
630 | via the corresponding metaObject() (see QMetaObject::className()). | - | ||||||||||||
631 | You can determine whether the object's class inherits another | - | ||||||||||||
632 | class in the QObject inheritance hierarchy by using the | - | ||||||||||||
633 | inherits() function. | - | ||||||||||||
634 | - | |||||||||||||
635 | When an object is deleted, it emits a destroyed() signal. You can | - | ||||||||||||
636 | catch this signal to avoid dangling references to QObjects. | - | ||||||||||||
637 | - | |||||||||||||
638 | QObjects can receive events through event() and filter the events | - | ||||||||||||
639 | of other objects. See installEventFilter() and eventFilter() for | - | ||||||||||||
640 | details. A convenience handler, childEvent(), can be reimplemented | - | ||||||||||||
641 | to catch child events. | - | ||||||||||||
642 | - | |||||||||||||
643 | Last but not least, QObject provides the basic timer support in | - | ||||||||||||
644 | Qt; see QTimer for high-level support for timers. | - | ||||||||||||
645 | - | |||||||||||||
646 | Notice that the Q_OBJECT macro is mandatory for any object that | - | ||||||||||||
647 | implements signals, slots or properties. You also need to run the | - | ||||||||||||
648 | \l{moc}{Meta Object Compiler} on the source file. We strongly | - | ||||||||||||
649 | recommend the use of this macro in all subclasses of QObject | - | ||||||||||||
650 | regardless of whether or not they actually use signals, slots and | - | ||||||||||||
651 | properties, since failure to do so may lead certain functions to | - | ||||||||||||
652 | exhibit strange behavior. | - | ||||||||||||
653 | - | |||||||||||||
654 | All Qt widgets inherit QObject. The convenience function | - | ||||||||||||
655 | isWidgetType() returns whether an object is actually a widget. It | - | ||||||||||||
656 | is much faster than | - | ||||||||||||
657 | \l{qobject_cast()}{qobject_cast}<QWidget *>(\e{obj}) or | - | ||||||||||||
658 | \e{obj}->\l{inherits()}{inherits}("QWidget"). | - | ||||||||||||
659 | - | |||||||||||||
660 | Some QObject functions, e.g. children(), return a QObjectList. | - | ||||||||||||
661 | QObjectList is a typedef for QList<QObject *>. | - | ||||||||||||
662 | - | |||||||||||||
663 | \section1 Thread Affinity | - | ||||||||||||
664 | - | |||||||||||||
665 | A QObject instance is said to have a \e{thread affinity}, or that | - | ||||||||||||
666 | it \e{lives} in a certain thread. When a QObject receives a | - | ||||||||||||
667 | \l{Qt::QueuedConnection}{queued signal} or a \l{The Event | - | ||||||||||||
668 | System#Sending Events}{posted event}, the slot or event handler | - | ||||||||||||
669 | will run in the thread that the object lives in. | - | ||||||||||||
670 | - | |||||||||||||
671 | \note If a QObject has no thread affinity (that is, if thread() | - | ||||||||||||
672 | returns zero), or if it lives in a thread that has no running event | - | ||||||||||||
673 | loop, then it cannot receive queued signals or posted events. | - | ||||||||||||
674 | - | |||||||||||||
675 | By default, a QObject lives in the thread in which it is created. | - | ||||||||||||
676 | An object's thread affinity can be queried using thread() and | - | ||||||||||||
677 | changed using moveToThread(). | - | ||||||||||||
678 | - | |||||||||||||
679 | All QObjects must live in the same thread as their parent. Consequently: | - | ||||||||||||
680 | - | |||||||||||||
681 | \list | - | ||||||||||||
682 | \li setParent() will fail if the two QObjects involved live in | - | ||||||||||||
683 | different threads. | - | ||||||||||||
684 | \li When a QObject is moved to another thread, all its children | - | ||||||||||||
685 | will be automatically moved too. | - | ||||||||||||
686 | \li moveToThread() will fail if the QObject has a parent. | - | ||||||||||||
687 | \li If QObjects are created within QThread::run(), they cannot | - | ||||||||||||
688 | become children of the QThread object because the QThread does | - | ||||||||||||
689 | not live in the thread that calls QThread::run(). | - | ||||||||||||
690 | \endlist | - | ||||||||||||
691 | - | |||||||||||||
692 | \note A QObject's member variables \e{do not} automatically become | - | ||||||||||||
693 | its children. The parent-child relationship must be set by either | - | ||||||||||||
694 | passing a pointer to the child's \l{QObject()}{constructor}, or by | - | ||||||||||||
695 | calling setParent(). Without this step, the object's member variables | - | ||||||||||||
696 | will remain in the old thread when moveToThread() is called. | - | ||||||||||||
697 | - | |||||||||||||
698 | \target No copy constructor | - | ||||||||||||
699 | \section1 No Copy Constructor or Assignment Operator | - | ||||||||||||
700 | - | |||||||||||||
701 | QObject has neither a copy constructor nor an assignment operator. | - | ||||||||||||
702 | This is by design. Actually, they are declared, but in a | - | ||||||||||||
703 | \c{private} section with the macro Q_DISABLE_COPY(). In fact, all | - | ||||||||||||
704 | Qt classes derived from QObject (direct or indirect) use this | - | ||||||||||||
705 | macro to declare their copy constructor and assignment operator to | - | ||||||||||||
706 | be private. The reasoning is found in the discussion on | - | ||||||||||||
707 | \l{Identity vs Value} {Identity vs Value} on the Qt \l{Object | - | ||||||||||||
708 | Model} page. | - | ||||||||||||
709 | - | |||||||||||||
710 | The main consequence is that you should use pointers to QObject | - | ||||||||||||
711 | (or to your QObject subclass) where you might otherwise be tempted | - | ||||||||||||
712 | to use your QObject subclass as a value. For example, without a | - | ||||||||||||
713 | copy constructor, you can't use a subclass of QObject as the value | - | ||||||||||||
714 | to be stored in one of the container classes. You must store | - | ||||||||||||
715 | pointers. | - | ||||||||||||
716 | - | |||||||||||||
717 | \section1 Auto-Connection | - | ||||||||||||
718 | - | |||||||||||||
719 | Qt's meta-object system provides a mechanism to automatically connect | - | ||||||||||||
720 | signals and slots between QObject subclasses and their children. As long | - | ||||||||||||
721 | as objects are defined with suitable object names, and slots follow a | - | ||||||||||||
722 | simple naming convention, this connection can be performed at run-time | - | ||||||||||||
723 | by the QMetaObject::connectSlotsByName() function. | - | ||||||||||||
724 | - | |||||||||||||
725 | \l uic generates code that invokes this function to enable | - | ||||||||||||
726 | auto-connection to be performed between widgets on forms created | - | ||||||||||||
727 | with \e{Qt Designer}. More information about using auto-connection with \e{Qt Designer} is | - | ||||||||||||
728 | given in the \l{Using a Designer UI File in Your Application} section of | - | ||||||||||||
729 | the \e{Qt Designer} manual. | - | ||||||||||||
730 | - | |||||||||||||
731 | \section1 Dynamic Properties | - | ||||||||||||
732 | - | |||||||||||||
733 | From Qt 4.2, dynamic properties can be added to and removed from QObject | - | ||||||||||||
734 | instances at run-time. Dynamic properties do not need to be declared at | - | ||||||||||||
735 | compile-time, yet they provide the same advantages as static properties | - | ||||||||||||
736 | and are manipulated using the same API - using property() to read them | - | ||||||||||||
737 | and setProperty() to write them. | - | ||||||||||||
738 | - | |||||||||||||
739 | From Qt 4.3, dynamic properties are supported by | - | ||||||||||||
740 | \l{Qt Designer's Widget Editing Mode#The Property Editor}{Qt Designer}, | - | ||||||||||||
741 | and both standard Qt widgets and user-created forms can be given dynamic | - | ||||||||||||
742 | properties. | - | ||||||||||||
743 | - | |||||||||||||
744 | \section1 Internationalization (I18n) | - | ||||||||||||
745 | - | |||||||||||||
746 | All QObject subclasses support Qt's translation features, making it possible | - | ||||||||||||
747 | to translate an application's user interface into different languages. | - | ||||||||||||
748 | - | |||||||||||||
749 | To make user-visible text translatable, it must be wrapped in calls to | - | ||||||||||||
750 | the tr() function. This is explained in detail in the | - | ||||||||||||
751 | \l{Writing Source Code for Translation} document. | - | ||||||||||||
752 | - | |||||||||||||
753 | \sa QMetaObject, QPointer, QObjectCleanupHandler, Q_DISABLE_COPY() | - | ||||||||||||
754 | \sa {Object Trees & Ownership} | - | ||||||||||||
755 | */ | - | ||||||||||||
756 | - | |||||||||||||
757 | /*! | - | ||||||||||||
758 | \relates QObject | - | ||||||||||||
759 | - | |||||||||||||
760 | Returns a pointer to the object named \a name that inherits \a | - | ||||||||||||
761 | type and with a given \a parent. | - | ||||||||||||
762 | - | |||||||||||||
763 | Returns 0 if there is no such child. | - | ||||||||||||
764 | - | |||||||||||||
765 | \snippet code/src_corelib_kernel_qobject.cpp 0 | - | ||||||||||||
766 | */ | - | ||||||||||||
767 | - | |||||||||||||
768 | void *qt_find_obj_child(QObject *parent, const char *type, const QString &name) | - | ||||||||||||
769 | { | - | ||||||||||||
770 | QObjectList list = parent->children(); | - | ||||||||||||
771 | if (list.size() == 0) return 0; | - | ||||||||||||
772 | for (int i = 0; i < list.size(); ++i) { | - | ||||||||||||
773 | QObject *obj = list.at(i); | - | ||||||||||||
774 | if (name == obj->objectName() && obj->inherits(type)) | - | ||||||||||||
775 | return obj; | - | ||||||||||||
776 | } | - | ||||||||||||
777 | return 0; | - | ||||||||||||
778 | } | - | ||||||||||||
779 | - | |||||||||||||
780 | - | |||||||||||||
781 | /***************************************************************************** | - | ||||||||||||
782 | QObject member functions | - | ||||||||||||
783 | *****************************************************************************/ | - | ||||||||||||
784 | - | |||||||||||||
785 | // check the constructor's parent thread argument | - | ||||||||||||
786 | static bool check_parent_thread(QObject *parent, | - | ||||||||||||
787 | QThreadData *parentThreadData, | - | ||||||||||||
788 | QThreadData *currentThreadData) | - | ||||||||||||
789 | { | - | ||||||||||||
790 | if (parent && parentThreadData != currentThreadData) { | - | ||||||||||||
791 | QThread *parentThread = parentThreadData->thread; | - | ||||||||||||
792 | QThread *currentThread = currentThreadData->thread; | - | ||||||||||||
793 | qWarning("QObject: Cannot create children for a parent that is in a different thread.\n" | - | ||||||||||||
794 | "(Parent is %s(%p), parent's thread is %s(%p), current thread is %s(%p)", | - | ||||||||||||
795 | parent->metaObject()->className(), | - | ||||||||||||
796 | parent, | - | ||||||||||||
797 | parentThread ? parentThread->metaObject()->className() : "QThread", | - | ||||||||||||
798 | parentThread, | - | ||||||||||||
799 | currentThread ? currentThread->metaObject()->className() : "QThread", | - | ||||||||||||
800 | currentThread); | - | ||||||||||||
801 | return false; | - | ||||||||||||
802 | } | - | ||||||||||||
803 | return true; | - | ||||||||||||
804 | } | - | ||||||||||||
805 | - | |||||||||||||
806 | /*! | - | ||||||||||||
807 | Constructs an object with parent object \a parent. | - | ||||||||||||
808 | - | |||||||||||||
809 | The parent of an object may be viewed as the object's owner. For | - | ||||||||||||
810 | instance, a \l{QDialog}{dialog box} is the parent of the \uicontrol{OK} | - | ||||||||||||
811 | and \uicontrol{Cancel} buttons it contains. | - | ||||||||||||
812 | - | |||||||||||||
813 | The destructor of a parent object destroys all child objects. | - | ||||||||||||
814 | - | |||||||||||||
815 | Setting \a parent to 0 constructs an object with no parent. If the | - | ||||||||||||
816 | object is a widget, it will become a top-level window. | - | ||||||||||||
817 | - | |||||||||||||
818 | \sa parent(), findChild(), findChildren() | - | ||||||||||||
819 | */ | - | ||||||||||||
820 | - | |||||||||||||
821 | QObject::QObject(QObject *parent) | - | ||||||||||||
822 | : d_ptr(new QObjectPrivate) | - | ||||||||||||
823 | { | - | ||||||||||||
824 | Q_D(QObject); | - | ||||||||||||
825 | d_ptr->q_ptr = this; | - | ||||||||||||
826 | d->threadData = (parent && !parent->thread()) ? parent->d_func()->threadData : QThreadData::current(); | - | ||||||||||||
827 | d->threadData->ref(); | - | ||||||||||||
828 | if (parent) { | - | ||||||||||||
829 | QT_TRY { | - | ||||||||||||
830 | if (!check_parent_thread(parent, parent ? parent->d_func()->threadData : 0, d->threadData)) | - | ||||||||||||
831 | parent = 0; | - | ||||||||||||
832 | setParent(parent); | - | ||||||||||||
833 | } QT_CATCH(...) { | - | ||||||||||||
834 | d->threadData->deref(); | - | ||||||||||||
835 | QT_RETHROW; | - | ||||||||||||
836 | } | - | ||||||||||||
837 | } | - | ||||||||||||
838 | qt_addObject(this); | - | ||||||||||||
839 | if (Q_UNLIKELY(qtHookData[QHooks::AddQObject])) | - | ||||||||||||
840 | reinterpret_cast<QHooks::AddQObjectCallback>(qtHookData[QHooks::AddQObject])(this); | - | ||||||||||||
841 | } | - | ||||||||||||
842 | - | |||||||||||||
843 | /*! | - | ||||||||||||
844 | \internal | - | ||||||||||||
845 | */ | - | ||||||||||||
846 | QObject::QObject(QObjectPrivate &dd, QObject *parent) | - | ||||||||||||
847 | : d_ptr(&dd) | - | ||||||||||||
848 | { | - | ||||||||||||
849 | Q_D(QObject); | - | ||||||||||||
850 | d_ptr->q_ptr = this; | - | ||||||||||||
851 | d->threadData = (parent && !parent->thread()) ? parent->d_func()->threadData : QThreadData::current(); | - | ||||||||||||
852 | d->threadData->ref(); | - | ||||||||||||
853 | if (parent) { | - | ||||||||||||
854 | QT_TRY { | - | ||||||||||||
855 | if (!check_parent_thread(parent, parent ? parent->d_func()->threadData : 0, d->threadData)) | - | ||||||||||||
856 | parent = 0; | - | ||||||||||||
857 | if (d->isWidget) { | - | ||||||||||||
858 | if (parent) { | - | ||||||||||||
859 | d->parent = parent; | - | ||||||||||||
860 | d->parent->d_func()->children.append(this); | - | ||||||||||||
861 | } | - | ||||||||||||
862 | // no events sent here, this is done at the end of the QWidget constructor | - | ||||||||||||
863 | } else { | - | ||||||||||||
864 | setParent(parent); | - | ||||||||||||
865 | } | - | ||||||||||||
866 | } QT_CATCH(...) { | - | ||||||||||||
867 | d->threadData->deref(); | - | ||||||||||||
868 | QT_RETHROW; | - | ||||||||||||
869 | } | - | ||||||||||||
870 | } | - | ||||||||||||
871 | qt_addObject(this); | - | ||||||||||||
872 | if (Q_UNLIKELY(qtHookData[QHooks::AddQObject])) | - | ||||||||||||
873 | reinterpret_cast<QHooks::AddQObjectCallback>(qtHookData[QHooks::AddQObject])(this); | - | ||||||||||||
874 | } | - | ||||||||||||
875 | - | |||||||||||||
876 | /*! | - | ||||||||||||
877 | Destroys the object, deleting all its child objects. | - | ||||||||||||
878 | - | |||||||||||||
879 | All signals to and from the object are automatically disconnected, and | - | ||||||||||||
880 | any pending posted events for the object are removed from the event | - | ||||||||||||
881 | queue. However, it is often safer to use deleteLater() rather than | - | ||||||||||||
882 | deleting a QObject subclass directly. | - | ||||||||||||
883 | - | |||||||||||||
884 | \warning All child objects are deleted. If any of these objects | - | ||||||||||||
885 | are on the stack or global, sooner or later your program will | - | ||||||||||||
886 | crash. We do not recommend holding pointers to child objects from | - | ||||||||||||
887 | outside the parent. If you still do, the destroyed() signal gives | - | ||||||||||||
888 | you an opportunity to detect when an object is destroyed. | - | ||||||||||||
889 | - | |||||||||||||
890 | \warning Deleting a QObject while pending events are waiting to | - | ||||||||||||
891 | be delivered can cause a crash. You must not delete the QObject | - | ||||||||||||
892 | directly if it exists in a different thread than the one currently | - | ||||||||||||
893 | executing. Use deleteLater() instead, which will cause the event | - | ||||||||||||
894 | loop to delete the object after all pending events have been | - | ||||||||||||
895 | delivered to it. | - | ||||||||||||
896 | - | |||||||||||||
897 | \sa deleteLater() | - | ||||||||||||
898 | */ | - | ||||||||||||
899 | - | |||||||||||||
900 | QObject::~QObject() | - | ||||||||||||
901 | { | - | ||||||||||||
902 | Q_D(QObject); | - | ||||||||||||
903 | d->wasDeleted = true; | - | ||||||||||||
904 | d->blockSig = 0; // unblock signals so we always emit destroyed() | - | ||||||||||||
905 | - | |||||||||||||
906 | QtSharedPointer::ExternalRefCountData *sharedRefcount = d->sharedRefcount.load(); | - | ||||||||||||
907 | if (sharedRefcount) { | - | ||||||||||||
908 | if (sharedRefcount->strongref.load() > 0) { | - | ||||||||||||
909 | qWarning("QObject: shared QObject was deleted directly. The program is malformed and may crash."); | - | ||||||||||||
910 | // but continue deleting, it's too late to stop anyway | - | ||||||||||||
911 | } | - | ||||||||||||
912 | - | |||||||||||||
913 | // indicate to all QWeakPointers that this QObject has now been deleted | - | ||||||||||||
914 | sharedRefcount->strongref.store(0); | - | ||||||||||||
915 | if (!sharedRefcount->weakref.deref()) | - | ||||||||||||
916 | delete sharedRefcount; | - | ||||||||||||
917 | } | - | ||||||||||||
918 | - | |||||||||||||
919 | if (!d->isWidget && d->isSignalConnected(0)) { | - | ||||||||||||
920 | emit destroyed(this); | - | ||||||||||||
921 | } | - | ||||||||||||
922 | - | |||||||||||||
923 | if (d->declarativeData) { | - | ||||||||||||
924 | if (static_cast<QAbstractDeclarativeDataImpl*>(d->declarativeData)->ownedByQml1) { | - | ||||||||||||
925 | if (QAbstractDeclarativeData::destroyed_qml1) | - | ||||||||||||
926 | QAbstractDeclarativeData::destroyed_qml1(d->declarativeData, this); | - | ||||||||||||
927 | } else { | - | ||||||||||||
928 | if (QAbstractDeclarativeData::destroyed) | - | ||||||||||||
929 | QAbstractDeclarativeData::destroyed(d->declarativeData, this); | - | ||||||||||||
930 | } | - | ||||||||||||
931 | } | - | ||||||||||||
932 | - | |||||||||||||
933 | // set ref to zero to indicate that this object has been deleted | - | ||||||||||||
934 | if (d->currentSender != 0) | - | ||||||||||||
935 | d->currentSender->ref = 0; | - | ||||||||||||
936 | d->currentSender = 0; | - | ||||||||||||
937 | - | |||||||||||||
938 | if (d->connectionLists || d->senders) { | - | ||||||||||||
939 | QMutex *signalSlotMutex = signalSlotLock(this); | - | ||||||||||||
940 | QMutexLocker locker(signalSlotMutex); | - | ||||||||||||
941 | - | |||||||||||||
942 | // disconnect all receivers | - | ||||||||||||
943 | if (d->connectionLists) { | - | ||||||||||||
944 | ++d->connectionLists->inUse; | - | ||||||||||||
945 | int connectionListsCount = d->connectionLists->count(); | - | ||||||||||||
946 | for (int signal = -1; signal < connectionListsCount; ++signal) { | - | ||||||||||||
947 | QObjectPrivate::ConnectionList &connectionList = | - | ||||||||||||
948 | (*d->connectionLists)[signal]; | - | ||||||||||||
949 | - | |||||||||||||
950 | while (QObjectPrivate::Connection *c = connectionList.first) { | - | ||||||||||||
951 | if (!c->receiver) { | - | ||||||||||||
952 | connectionList.first = c->nextConnectionList; | - | ||||||||||||
953 | c->deref(); | - | ||||||||||||
954 | continue; | - | ||||||||||||
955 | } | - | ||||||||||||
956 | - | |||||||||||||
957 | QMutex *m = signalSlotLock(c->receiver); | - | ||||||||||||
958 | bool needToUnlock = QOrderedMutexLocker::relock(signalSlotMutex, m); | - | ||||||||||||
959 | - | |||||||||||||
960 | if (c->receiver) { | - | ||||||||||||
961 | *c->prev = c->next; | - | ||||||||||||
962 | if (c->next) c->next->prev = c->prev; | - | ||||||||||||
963 | } | - | ||||||||||||
964 | c->receiver = 0; | - | ||||||||||||
965 | if (needToUnlock) | - | ||||||||||||
966 | m->unlock(); | - | ||||||||||||
967 | - | |||||||||||||
968 | connectionList.first = c->nextConnectionList; | - | ||||||||||||
969 | - | |||||||||||||
970 | // The destroy operation must happen outside the lock | - | ||||||||||||
971 | if (c->isSlotObject) { | - | ||||||||||||
972 | c->isSlotObject = false; | - | ||||||||||||
973 | locker.unlock(); | - | ||||||||||||
974 | c->slotObj->destroyIfLastRef(); | - | ||||||||||||
975 | locker.relock(); | - | ||||||||||||
976 | } | - | ||||||||||||
977 | c->deref(); | - | ||||||||||||
978 | } | - | ||||||||||||
979 | } | - | ||||||||||||
980 | - | |||||||||||||
981 | if (!--d->connectionLists->inUse) { | - | ||||||||||||
982 | delete d->connectionLists; | - | ||||||||||||
983 | } else { | - | ||||||||||||
984 | d->connectionLists->orphaned = true; | - | ||||||||||||
985 | } | - | ||||||||||||
986 | d->connectionLists = 0; | - | ||||||||||||
987 | } | - | ||||||||||||
988 | - | |||||||||||||
989 | /* Disconnect all senders: | - | ||||||||||||
990 | * This loop basically just does | - | ||||||||||||
991 | * for (node = d->senders; node; node = node->next) { ... } | - | ||||||||||||
992 | * | - | ||||||||||||
993 | * We need to temporarily unlock the receiver mutex to destroy the functors or to lock the | - | ||||||||||||
994 | * sender's mutex. And when the mutex is released, node->next might be destroyed by another | - | ||||||||||||
995 | * thread. That's why we set node->prev to &node, that way, if node is destroyed, node will | - | ||||||||||||
996 | * be updated. | - | ||||||||||||
997 | */ | - | ||||||||||||
998 | QObjectPrivate::Connection *node = d->senders; | - | ||||||||||||
999 | while (node) { | - | ||||||||||||
1000 | QObject *sender = node->sender; | - | ||||||||||||
1001 | // Send disconnectNotify before removing the connection from sender's connection list. | - | ||||||||||||
1002 | // This ensures any eventual destructor of sender will block on getting receiver's lock | - | ||||||||||||
1003 | // and not finish until we release it. | - | ||||||||||||
1004 | sender->disconnectNotify(QMetaObjectPrivate::signal(sender->metaObject(), node->signal_index)); | - | ||||||||||||
1005 | QMutex *m = signalSlotLock(sender); | - | ||||||||||||
1006 | node->prev = &node; | - | ||||||||||||
1007 | bool needToUnlock = QOrderedMutexLocker::relock(signalSlotMutex, m); | - | ||||||||||||
1008 | //the node has maybe been removed while the mutex was unlocked in relock? | - | ||||||||||||
1009 | if (!node || node->sender != sender) { | - | ||||||||||||
1010 | // We hold the wrong mutex | - | ||||||||||||
1011 | Q_ASSERT(needToUnlock); | - | ||||||||||||
1012 | m->unlock(); | - | ||||||||||||
1013 | continue; | - | ||||||||||||
1014 | } | - | ||||||||||||
1015 | node->receiver = 0; | - | ||||||||||||
1016 | QObjectConnectionListVector *senderLists = sender->d_func()->connectionLists; | - | ||||||||||||
1017 | if (senderLists) | - | ||||||||||||
1018 | senderLists->dirty = true; | - | ||||||||||||
1019 | - | |||||||||||||
1020 | QtPrivate::QSlotObjectBase *slotObj = Q_NULLPTR; | - | ||||||||||||
1021 | if (node->isSlotObject) { | - | ||||||||||||
1022 | slotObj = node->slotObj; | - | ||||||||||||
1023 | node->isSlotObject = false; | - | ||||||||||||
1024 | } | - | ||||||||||||
1025 | - | |||||||||||||
1026 | node = node->next; | - | ||||||||||||
1027 | if (needToUnlock) | - | ||||||||||||
1028 | m->unlock(); | - | ||||||||||||
1029 | - | |||||||||||||
1030 | if (slotObj) { | - | ||||||||||||
1031 | if (node) | - | ||||||||||||
1032 | node->prev = &node; | - | ||||||||||||
1033 | locker.unlock(); | - | ||||||||||||
1034 | slotObj->destroyIfLastRef(); | - | ||||||||||||
1035 | locker.relock(); | - | ||||||||||||
1036 | } | - | ||||||||||||
1037 | } | - | ||||||||||||
1038 | } | - | ||||||||||||
1039 | - | |||||||||||||
1040 | if (!d->children.isEmpty()) | - | ||||||||||||
1041 | d->deleteChildren(); | - | ||||||||||||
1042 | - | |||||||||||||
1043 | qt_removeObject(this); | - | ||||||||||||
1044 | if (Q_UNLIKELY(qtHookData[QHooks::RemoveQObject])) | - | ||||||||||||
1045 | reinterpret_cast<QHooks::RemoveQObjectCallback>(qtHookData[QHooks::RemoveQObject])(this); | - | ||||||||||||
1046 | - | |||||||||||||
1047 | if (d->parent) // remove it from parent object | - | ||||||||||||
1048 | d->setParent_helper(0); | - | ||||||||||||
1049 | } | - | ||||||||||||
1050 | - | |||||||||||||
1051 | QObjectPrivate::Connection::~Connection() | - | ||||||||||||
1052 | { | - | ||||||||||||
1053 | if (ownArgumentTypes) { | - | ||||||||||||
1054 | const int *v = argumentTypes.load(); | - | ||||||||||||
1055 | if (v != &DIRECT_CONNECTION_ONLY) | - | ||||||||||||
1056 | delete [] v; | - | ||||||||||||
1057 | } | - | ||||||||||||
1058 | if (isSlotObject) | - | ||||||||||||
1059 | slotObj->destroyIfLastRef(); | - | ||||||||||||
1060 | } | - | ||||||||||||
1061 | - | |||||||||||||
1062 | - | |||||||||||||
1063 | /*! | - | ||||||||||||
1064 | \fn QMetaObject *QObject::metaObject() const | - | ||||||||||||
1065 | - | |||||||||||||
1066 | Returns a pointer to the meta-object of this object. | - | ||||||||||||
1067 | - | |||||||||||||
1068 | A meta-object contains information about a class that inherits | - | ||||||||||||
1069 | QObject, e.g. class name, superclass name, properties, signals and | - | ||||||||||||
1070 | slots. Every QObject subclass that contains the Q_OBJECT macro will have a | - | ||||||||||||
1071 | meta-object. | - | ||||||||||||
1072 | - | |||||||||||||
1073 | The meta-object information is required by the signal/slot | - | ||||||||||||
1074 | connection mechanism and the property system. The inherits() | - | ||||||||||||
1075 | function also makes use of the meta-object. | - | ||||||||||||
1076 | - | |||||||||||||
1077 | If you have no pointer to an actual object instance but still | - | ||||||||||||
1078 | want to access the meta-object of a class, you can use \l | - | ||||||||||||
1079 | staticMetaObject. | - | ||||||||||||
1080 | - | |||||||||||||
1081 | Example: | - | ||||||||||||
1082 | - | |||||||||||||
1083 | \snippet code/src_corelib_kernel_qobject.cpp 1 | - | ||||||||||||
1084 | - | |||||||||||||
1085 | \sa staticMetaObject | - | ||||||||||||
1086 | */ | - | ||||||||||||
1087 | - | |||||||||||||
1088 | /*! | - | ||||||||||||
1089 | \variable QObject::staticMetaObject | - | ||||||||||||
1090 | - | |||||||||||||
1091 | This variable stores the meta-object for the class. | - | ||||||||||||
1092 | - | |||||||||||||
1093 | A meta-object contains information about a class that inherits | - | ||||||||||||
1094 | QObject, e.g. class name, superclass name, properties, signals and | - | ||||||||||||
1095 | slots. Every class that contains the Q_OBJECT macro will also have | - | ||||||||||||
1096 | a meta-object. | - | ||||||||||||
1097 | - | |||||||||||||
1098 | The meta-object information is required by the signal/slot | - | ||||||||||||
1099 | connection mechanism and the property system. The inherits() | - | ||||||||||||
1100 | function also makes use of the meta-object. | - | ||||||||||||
1101 | - | |||||||||||||
1102 | If you have a pointer to an object, you can use metaObject() to | - | ||||||||||||
1103 | retrieve the meta-object associated with that object. | - | ||||||||||||
1104 | - | |||||||||||||
1105 | Example: | - | ||||||||||||
1106 | - | |||||||||||||
1107 | \snippet code/src_corelib_kernel_qobject.cpp 2 | - | ||||||||||||
1108 | - | |||||||||||||
1109 | \sa metaObject() | - | ||||||||||||
1110 | */ | - | ||||||||||||
1111 | - | |||||||||||||
1112 | /*! \fn T *qobject_cast<T *>(QObject *object) | - | ||||||||||||
1113 | \relates QObject | - | ||||||||||||
1114 | - | |||||||||||||
1115 | Returns the given \a object cast to type T if the object is of type | - | ||||||||||||
1116 | T (or of a subclass); otherwise returns 0. If \a object is 0 then | - | ||||||||||||
1117 | it will also return 0. | - | ||||||||||||
1118 | - | |||||||||||||
1119 | The class T must inherit (directly or indirectly) QObject and be | - | ||||||||||||
1120 | declared with the \l Q_OBJECT macro. | - | ||||||||||||
1121 | - | |||||||||||||
1122 | A class is considered to inherit itself. | - | ||||||||||||
1123 | - | |||||||||||||
1124 | Example: | - | ||||||||||||
1125 | - | |||||||||||||
1126 | \snippet code/src_corelib_kernel_qobject.cpp 3 | - | ||||||||||||
1127 | - | |||||||||||||
1128 | The qobject_cast() function behaves similarly to the standard C++ | - | ||||||||||||
1129 | \c dynamic_cast(), with the advantages that it doesn't require | - | ||||||||||||
1130 | RTTI support and it works across dynamic library boundaries. | - | ||||||||||||
1131 | - | |||||||||||||
1132 | qobject_cast() can also be used in conjunction with interfaces; | - | ||||||||||||
1133 | see the \l{tools/plugandpaint/app}{Plug & Paint} example for details. | - | ||||||||||||
1134 | - | |||||||||||||
1135 | \warning If T isn't declared with the Q_OBJECT macro, this | - | ||||||||||||
1136 | function's return value is undefined. | - | ||||||||||||
1137 | - | |||||||||||||
1138 | \sa QObject::inherits() | - | ||||||||||||
1139 | */ | - | ||||||||||||
1140 | - | |||||||||||||
1141 | /*! | - | ||||||||||||
1142 | \fn bool QObject::inherits(const char *className) const | - | ||||||||||||
1143 | - | |||||||||||||
1144 | Returns \c true if this object is an instance of a class that | - | ||||||||||||
1145 | inherits \a className or a QObject subclass that inherits \a | - | ||||||||||||
1146 | className; otherwise returns \c false. | - | ||||||||||||
1147 | - | |||||||||||||
1148 | A class is considered to inherit itself. | - | ||||||||||||
1149 | - | |||||||||||||
1150 | Example: | - | ||||||||||||
1151 | - | |||||||||||||
1152 | \snippet code/src_corelib_kernel_qobject.cpp 4 | - | ||||||||||||
1153 | - | |||||||||||||
1154 | If you need to determine whether an object is an instance of a particular | - | ||||||||||||
1155 | class for the purpose of casting it, consider using qobject_cast<Type *>(object) | - | ||||||||||||
1156 | instead. | - | ||||||||||||
1157 | - | |||||||||||||
1158 | \sa metaObject(), qobject_cast() | - | ||||||||||||
1159 | */ | - | ||||||||||||
1160 | - | |||||||||||||
1161 | /*! | - | ||||||||||||
1162 | \property QObject::objectName | - | ||||||||||||
1163 | - | |||||||||||||
1164 | \brief the name of this object | - | ||||||||||||
1165 | - | |||||||||||||
1166 | You can find an object by name (and type) using findChild(). | - | ||||||||||||
1167 | You can find a set of objects with findChildren(). | - | ||||||||||||
1168 | - | |||||||||||||
1169 | \snippet code/src_corelib_kernel_qobject.cpp 5 | - | ||||||||||||
1170 | - | |||||||||||||
1171 | By default, this property contains an empty string. | - | ||||||||||||
1172 | - | |||||||||||||
1173 | \sa metaObject(), QMetaObject::className() | - | ||||||||||||
1174 | */ | - | ||||||||||||
1175 | - | |||||||||||||
1176 | QString QObject::objectName() const | - | ||||||||||||
1177 | { | - | ||||||||||||
1178 | Q_D(const QObject); | - | ||||||||||||
1179 | return d->extraData ? d->extraData->objectName : QString(); | - | ||||||||||||
1180 | } | - | ||||||||||||
1181 | - | |||||||||||||
1182 | /* | - | ||||||||||||
1183 | Sets the object's name to \a name. | - | ||||||||||||
1184 | */ | - | ||||||||||||
1185 | void QObject::setObjectName(const QString &name) | - | ||||||||||||
1186 | { | - | ||||||||||||
1187 | Q_D(QObject); | - | ||||||||||||
1188 | if (!d->extraData) | - | ||||||||||||
1189 | d->extraData = new QObjectPrivate::ExtraData; | - | ||||||||||||
1190 | - | |||||||||||||
1191 | if (d->extraData->objectName != name) { | - | ||||||||||||
1192 | d->extraData->objectName = name; | - | ||||||||||||
1193 | emit objectNameChanged(d->extraData->objectName, QPrivateSignal()); | - | ||||||||||||
1194 | } | - | ||||||||||||
1195 | } | - | ||||||||||||
1196 | - | |||||||||||||
1197 | /*! \fn void QObject::objectNameChanged(const QString &objectName) | - | ||||||||||||
1198 | - | |||||||||||||
1199 | This signal is emitted after the object's name has been changed. The new object name is passed as \a objectName. | - | ||||||||||||
1200 | - | |||||||||||||
1201 | \sa QObject::objectName | - | ||||||||||||
1202 | */ | - | ||||||||||||
1203 | - | |||||||||||||
1204 | /*! | - | ||||||||||||
1205 | \fn bool QObject::isWidgetType() const | - | ||||||||||||
1206 | - | |||||||||||||
1207 | Returns \c true if the object is a widget; otherwise returns \c false. | - | ||||||||||||
1208 | - | |||||||||||||
1209 | Calling this function is equivalent to calling | - | ||||||||||||
1210 | \c{inherits("QWidget")}, except that it is much faster. | - | ||||||||||||
1211 | */ | - | ||||||||||||
1212 | - | |||||||||||||
1213 | /*! | - | ||||||||||||
1214 | \fn bool QObject::isWindowType() const | - | ||||||||||||
1215 | - | |||||||||||||
1216 | Returns \c true if the object is a window; otherwise returns \c false. | - | ||||||||||||
1217 | - | |||||||||||||
1218 | Calling this function is equivalent to calling | - | ||||||||||||
1219 | \c{inherits("QWindow")}, except that it is much faster. | - | ||||||||||||
1220 | */ | - | ||||||||||||
1221 | - | |||||||||||||
1222 | /*! | - | ||||||||||||
1223 | This virtual function receives events to an object and should | - | ||||||||||||
1224 | return true if the event \a e was recognized and processed. | - | ||||||||||||
1225 | - | |||||||||||||
1226 | The event() function can be reimplemented to customize the | - | ||||||||||||
1227 | behavior of an object. | - | ||||||||||||
1228 | - | |||||||||||||
1229 | Make sure you call the parent event class implementation | - | ||||||||||||
1230 | for all the events you did not handle. | - | ||||||||||||
1231 | - | |||||||||||||
1232 | Example: | - | ||||||||||||
1233 | - | |||||||||||||
1234 | \snippet code/src_corelib_kernel_qobject.cpp 52 | - | ||||||||||||
1235 | - | |||||||||||||
1236 | \sa installEventFilter(), timerEvent(), QCoreApplication::sendEvent(), | - | ||||||||||||
1237 | QCoreApplication::postEvent() | - | ||||||||||||
1238 | */ | - | ||||||||||||
1239 | - | |||||||||||||
1240 | bool QObject::event(QEvent *e) | - | ||||||||||||
1241 | { | - | ||||||||||||
1242 | switch (e->type()) { | - | ||||||||||||
1243 | case QEvent::Timer: | - | ||||||||||||
1244 | timerEvent((QTimerEvent*)e); | - | ||||||||||||
1245 | break; | - | ||||||||||||
1246 | - | |||||||||||||
1247 | case QEvent::ChildAdded: | - | ||||||||||||
1248 | case QEvent::ChildPolished: | - | ||||||||||||
1249 | case QEvent::ChildRemoved: | - | ||||||||||||
1250 | childEvent((QChildEvent*)e); | - | ||||||||||||
1251 | break; | - | ||||||||||||
1252 | - | |||||||||||||
1253 | case QEvent::DeferredDelete: | - | ||||||||||||
1254 | qDeleteInEventHandler(this); | - | ||||||||||||
1255 | break; | - | ||||||||||||
1256 | - | |||||||||||||
1257 | case QEvent::MetaCall: | - | ||||||||||||
1258 | { | - | ||||||||||||
1259 | QMetaCallEvent *mce = static_cast<QMetaCallEvent*>(e); | - | ||||||||||||
1260 | - | |||||||||||||
1261 | QConnectionSenderSwitcher sw(this, const_cast<QObject*>(mce->sender()), mce->signalId()); | - | ||||||||||||
1262 | - | |||||||||||||
1263 | mce->placeMetaCall(this); | - | ||||||||||||
1264 | break; | - | ||||||||||||
1265 | } | - | ||||||||||||
1266 | - | |||||||||||||
1267 | case QEvent::ThreadChange: { | - | ||||||||||||
1268 | Q_D(QObject); | - | ||||||||||||
1269 | QThreadData *threadData = d->threadData; | - | ||||||||||||
1270 | QAbstractEventDispatcher *eventDispatcher = threadData->eventDispatcher.load(); | - | ||||||||||||
1271 | if (eventDispatcher) { | - | ||||||||||||
1272 | QList<QAbstractEventDispatcher::TimerInfo> timers = eventDispatcher->registeredTimers(this); | - | ||||||||||||
1273 | if (!timers.isEmpty()) { | - | ||||||||||||
1274 | // do not to release our timer ids back to the pool (since the timer ids are moving to a new thread). | - | ||||||||||||
1275 | eventDispatcher->unregisterTimers(this); | - | ||||||||||||
1276 | QMetaObject::invokeMethod(this, "_q_reregisterTimers", Qt::QueuedConnection, | - | ||||||||||||
1277 | Q_ARG(void*, (new QList<QAbstractEventDispatcher::TimerInfo>(timers)))); | - | ||||||||||||
1278 | } | - | ||||||||||||
1279 | } | - | ||||||||||||
1280 | break; | - | ||||||||||||
1281 | } | - | ||||||||||||
1282 | - | |||||||||||||
1283 | default: | - | ||||||||||||
1284 | if (e->type() >= QEvent::User) { | - | ||||||||||||
1285 | customEvent(e); | - | ||||||||||||
1286 | break; | - | ||||||||||||
1287 | } | - | ||||||||||||
1288 | return false; | - | ||||||||||||
1289 | } | - | ||||||||||||
1290 | return true; | - | ||||||||||||
1291 | } | - | ||||||||||||
1292 | - | |||||||||||||
1293 | /*! | - | ||||||||||||
1294 | \fn void QObject::timerEvent(QTimerEvent *event) | - | ||||||||||||
1295 | - | |||||||||||||
1296 | This event handler can be reimplemented in a subclass to receive | - | ||||||||||||
1297 | timer events for the object. | - | ||||||||||||
1298 | - | |||||||||||||
1299 | QTimer provides a higher-level interface to the timer | - | ||||||||||||
1300 | functionality, and also more general information about timers. The | - | ||||||||||||
1301 | timer event is passed in the \a event parameter. | - | ||||||||||||
1302 | - | |||||||||||||
1303 | \sa startTimer(), killTimer(), event() | - | ||||||||||||
1304 | */ | - | ||||||||||||
1305 | - | |||||||||||||
1306 | void QObject::timerEvent(QTimerEvent *) | - | ||||||||||||
1307 | { | - | ||||||||||||
1308 | } | - | ||||||||||||
1309 | - | |||||||||||||
1310 | - | |||||||||||||
1311 | /*! | - | ||||||||||||
1312 | This event handler can be reimplemented in a subclass to receive | - | ||||||||||||
1313 | child events. The event is passed in the \a event parameter. | - | ||||||||||||
1314 | - | |||||||||||||
1315 | QEvent::ChildAdded and QEvent::ChildRemoved events are sent to | - | ||||||||||||
1316 | objects when children are added or removed. In both cases you can | - | ||||||||||||
1317 | only rely on the child being a QObject, or if isWidgetType() | - | ||||||||||||
1318 | returns \c true, a QWidget. (This is because, in the | - | ||||||||||||
1319 | \l{QEvent::ChildAdded}{ChildAdded} case, the child is not yet | - | ||||||||||||
1320 | fully constructed, and in the \l{QEvent::ChildRemoved}{ChildRemoved} | - | ||||||||||||
1321 | case it might have been destructed already). | - | ||||||||||||
1322 | - | |||||||||||||
1323 | QEvent::ChildPolished events are sent to widgets when children | - | ||||||||||||
1324 | are polished, or when polished children are added. If you receive | - | ||||||||||||
1325 | a child polished event, the child's construction is usually | - | ||||||||||||
1326 | completed. However, this is not guaranteed, and multiple polish | - | ||||||||||||
1327 | events may be delivered during the execution of a widget's | - | ||||||||||||
1328 | constructor. | - | ||||||||||||
1329 | - | |||||||||||||
1330 | For every child widget, you receive one | - | ||||||||||||
1331 | \l{QEvent::ChildAdded}{ChildAdded} event, zero or more | - | ||||||||||||
1332 | \l{QEvent::ChildPolished}{ChildPolished} events, and one | - | ||||||||||||
1333 | \l{QEvent::ChildRemoved}{ChildRemoved} event. | - | ||||||||||||
1334 | - | |||||||||||||
1335 | The \l{QEvent::ChildPolished}{ChildPolished} event is omitted if | - | ||||||||||||
1336 | a child is removed immediately after it is added. If a child is | - | ||||||||||||
1337 | polished several times during construction and destruction, you | - | ||||||||||||
1338 | may receive several child polished events for the same child, | - | ||||||||||||
1339 | each time with a different virtual table. | - | ||||||||||||
1340 | - | |||||||||||||
1341 | \sa event() | - | ||||||||||||
1342 | */ | - | ||||||||||||
1343 | - | |||||||||||||
1344 | void QObject::childEvent(QChildEvent * /* event */) | - | ||||||||||||
1345 | { | - | ||||||||||||
1346 | } | - | ||||||||||||
1347 | - | |||||||||||||
1348 | - | |||||||||||||
1349 | /*! | - | ||||||||||||
1350 | This event handler can be reimplemented in a subclass to receive | - | ||||||||||||
1351 | custom events. Custom events are user-defined events with a type | - | ||||||||||||
1352 | value at least as large as the QEvent::User item of the | - | ||||||||||||
1353 | QEvent::Type enum, and is typically a QEvent subclass. The event | - | ||||||||||||
1354 | is passed in the \a event parameter. | - | ||||||||||||
1355 | - | |||||||||||||
1356 | \sa event(), QEvent | - | ||||||||||||
1357 | */ | - | ||||||||||||
1358 | void QObject::customEvent(QEvent * /* event */) | - | ||||||||||||
1359 | { | - | ||||||||||||
1360 | } | - | ||||||||||||
1361 | - | |||||||||||||
1362 | - | |||||||||||||
1363 | - | |||||||||||||
1364 | /*! | - | ||||||||||||
1365 | Filters events if this object has been installed as an event | - | ||||||||||||
1366 | filter for the \a watched object. | - | ||||||||||||
1367 | - | |||||||||||||
1368 | In your reimplementation of this function, if you want to filter | - | ||||||||||||
1369 | the \a event out, i.e. stop it being handled further, return | - | ||||||||||||
1370 | true; otherwise return false. | - | ||||||||||||
1371 | - | |||||||||||||
1372 | Example: | - | ||||||||||||
1373 | \snippet code/src_corelib_kernel_qobject.cpp 6 | - | ||||||||||||
1374 | - | |||||||||||||
1375 | Notice in the example above that unhandled events are passed to | - | ||||||||||||
1376 | the base class's eventFilter() function, since the base class | - | ||||||||||||
1377 | might have reimplemented eventFilter() for its own internal | - | ||||||||||||
1378 | purposes. | - | ||||||||||||
1379 | - | |||||||||||||
1380 | \warning If you delete the receiver object in this function, be | - | ||||||||||||
1381 | sure to return true. Otherwise, Qt will forward the event to the | - | ||||||||||||
1382 | deleted object and the program might crash. | - | ||||||||||||
1383 | - | |||||||||||||
1384 | \sa installEventFilter() | - | ||||||||||||
1385 | */ | - | ||||||||||||
1386 | - | |||||||||||||
1387 | bool QObject::eventFilter(QObject * /* watched */, QEvent * /* event */) | - | ||||||||||||
1388 | { | - | ||||||||||||
1389 | return false; | - | ||||||||||||
1390 | } | - | ||||||||||||
1391 | - | |||||||||||||
1392 | /*! | - | ||||||||||||
1393 | \fn bool QObject::signalsBlocked() const | - | ||||||||||||
1394 | - | |||||||||||||
1395 | Returns \c true if signals are blocked; otherwise returns \c false. | - | ||||||||||||
1396 | - | |||||||||||||
1397 | Signals are not blocked by default. | - | ||||||||||||
1398 | - | |||||||||||||
1399 | \sa blockSignals(), QSignalBlocker | - | ||||||||||||
1400 | */ | - | ||||||||||||
1401 | - | |||||||||||||
1402 | /*! | - | ||||||||||||
1403 | If \a block is true, signals emitted by this object are blocked | - | ||||||||||||
1404 | (i.e., emitting a signal will not invoke anything connected to it). | - | ||||||||||||
1405 | If \a block is false, no such blocking will occur. | - | ||||||||||||
1406 | - | |||||||||||||
1407 | The return value is the previous value of signalsBlocked(). | - | ||||||||||||
1408 | - | |||||||||||||
1409 | Note that the destroyed() signal will be emitted even if the signals | - | ||||||||||||
1410 | for this object have been blocked. | - | ||||||||||||
1411 | - | |||||||||||||
1412 | Signals emitted while being blocked are not buffered. | - | ||||||||||||
1413 | - | |||||||||||||
1414 | \sa signalsBlocked(), QSignalBlocker | - | ||||||||||||
1415 | */ | - | ||||||||||||
1416 | - | |||||||||||||
1417 | bool QObject::blockSignals(bool block) Q_DECL_NOTHROW | - | ||||||||||||
1418 | { | - | ||||||||||||
1419 | Q_D(QObject); | - | ||||||||||||
1420 | bool previous = d->blockSig; | - | ||||||||||||
1421 | d->blockSig = block; | - | ||||||||||||
1422 | return previous; executed 39656 times by 104 tests: return previous; Executed by:
| 39656 | ||||||||||||
1423 | } | - | ||||||||||||
1424 | - | |||||||||||||
1425 | /*! | - | ||||||||||||
1426 | Returns the thread in which the object lives. | - | ||||||||||||
1427 | - | |||||||||||||
1428 | \sa moveToThread() | - | ||||||||||||
1429 | */ | - | ||||||||||||
1430 | QThread *QObject::thread() const | - | ||||||||||||
1431 | { | - | ||||||||||||
1432 | return d_func()->threadData->thread; | - | ||||||||||||
1433 | } | - | ||||||||||||
1434 | - | |||||||||||||
1435 | /*! | - | ||||||||||||
1436 | Changes the thread affinity for this object and its children. The | - | ||||||||||||
1437 | object cannot be moved if it has a parent. Event processing will | - | ||||||||||||
1438 | continue in the \a targetThread. | - | ||||||||||||
1439 | - | |||||||||||||
1440 | To move an object to the main thread, use QApplication::instance() | - | ||||||||||||
1441 | to retrieve a pointer to the current application, and then use | - | ||||||||||||
1442 | QApplication::thread() to retrieve the thread in which the | - | ||||||||||||
1443 | application lives. For example: | - | ||||||||||||
1444 | - | |||||||||||||
1445 | \snippet code/src_corelib_kernel_qobject.cpp 7 | - | ||||||||||||
1446 | - | |||||||||||||
1447 | If \a targetThread is zero, all event processing for this object | - | ||||||||||||
1448 | and its children stops. | - | ||||||||||||
1449 | - | |||||||||||||
1450 | Note that all active timers for the object will be reset. The | - | ||||||||||||
1451 | timers are first stopped in the current thread and restarted (with | - | ||||||||||||
1452 | the same interval) in the \a targetThread. As a result, constantly | - | ||||||||||||
1453 | moving an object between threads can postpone timer events | - | ||||||||||||
1454 | indefinitely. | - | ||||||||||||
1455 | - | |||||||||||||
1456 | A QEvent::ThreadChange event is sent to this object just before | - | ||||||||||||
1457 | the thread affinity is changed. You can handle this event to | - | ||||||||||||
1458 | perform any special processing. Note that any new events that are | - | ||||||||||||
1459 | posted to this object will be handled in the \a targetThread. | - | ||||||||||||
1460 | - | |||||||||||||
1461 | \warning This function is \e not thread-safe; the current thread | - | ||||||||||||
1462 | must be same as the current thread affinity. In other words, this | - | ||||||||||||
1463 | function can only "push" an object from the current thread to | - | ||||||||||||
1464 | another thread, it cannot "pull" an object from any arbitrary | - | ||||||||||||
1465 | thread to the current thread. | - | ||||||||||||
1466 | - | |||||||||||||
1467 | \sa thread() | - | ||||||||||||
1468 | */ | - | ||||||||||||
1469 | void QObject::moveToThread(QThread *targetThread) | - | ||||||||||||
1470 | { | - | ||||||||||||
1471 | Q_D(QObject); | - | ||||||||||||
1472 | - | |||||||||||||
1473 | if (d->threadData->thread == targetThread) { | - | ||||||||||||
1474 | // object is already in this thread | - | ||||||||||||
1475 | return; | - | ||||||||||||
1476 | } | - | ||||||||||||
1477 | - | |||||||||||||
1478 | if (d->parent != 0) { | - | ||||||||||||
1479 | qWarning("QObject::moveToThread: Cannot move objects with a parent"); | - | ||||||||||||
1480 | return; | - | ||||||||||||
1481 | } | - | ||||||||||||
1482 | if (d->isWidget) { | - | ||||||||||||
1483 | qWarning("QObject::moveToThread: Widgets cannot be moved to a new thread"); | - | ||||||||||||
1484 | return; | - | ||||||||||||
1485 | } | - | ||||||||||||
1486 | - | |||||||||||||
1487 | QThreadData *currentData = QThreadData::current(); | - | ||||||||||||
1488 | QThreadData *targetData = targetThread ? QThreadData::get2(targetThread) : Q_NULLPTR; | - | ||||||||||||
1489 | if (d->threadData->thread == 0 && currentData == targetData) { | - | ||||||||||||
1490 | // one exception to the rule: we allow moving objects with no thread affinity to the current thread | - | ||||||||||||
1491 | currentData = d->threadData; | - | ||||||||||||
1492 | } else if (d->threadData != currentData) { | - | ||||||||||||
1493 | qWarning("QObject::moveToThread: Current thread (%p) is not the object's thread (%p).\n" | - | ||||||||||||
1494 | "Cannot move to target thread (%p)\n", | - | ||||||||||||
1495 | currentData->thread.load(), d->threadData->thread.load(), targetData ? targetData->thread.load() : Q_NULLPTR); | - | ||||||||||||
1496 | - | |||||||||||||
1497 | #ifdef Q_OS_MAC | - | ||||||||||||
1498 | qWarning("You might be loading two sets of Qt binaries into the same process. " | - | ||||||||||||
1499 | "Check that all plugins are compiled against the right Qt binaries. Export " | - | ||||||||||||
1500 | "DYLD_PRINT_LIBRARIES=1 and check that only one set of binaries are being loaded."); | - | ||||||||||||
1501 | #endif | - | ||||||||||||
1502 | - | |||||||||||||
1503 | return; | - | ||||||||||||
1504 | } | - | ||||||||||||
1505 | - | |||||||||||||
1506 | // prepare to move | - | ||||||||||||
1507 | d->moveToThread_helper(); | - | ||||||||||||
1508 | - | |||||||||||||
1509 | if (!targetData) | - | ||||||||||||
1510 | targetData = new QThreadData(0); | - | ||||||||||||
1511 | - | |||||||||||||
1512 | QOrderedMutexLocker locker(¤tData->postEventList.mutex, | - | ||||||||||||
1513 | &targetData->postEventList.mutex); | - | ||||||||||||
1514 | - | |||||||||||||
1515 | // keep currentData alive (since we've got it locked) | - | ||||||||||||
1516 | currentData->ref(); | - | ||||||||||||
1517 | - | |||||||||||||
1518 | // move the object | - | ||||||||||||
1519 | d_func()->setThreadData_helper(currentData, targetData); | - | ||||||||||||
1520 | - | |||||||||||||
1521 | locker.unlock(); | - | ||||||||||||
1522 | - | |||||||||||||
1523 | // now currentData can commit suicide if it wants to | - | ||||||||||||
1524 | currentData->deref(); | - | ||||||||||||
1525 | } | - | ||||||||||||
1526 | - | |||||||||||||
1527 | void QObjectPrivate::moveToThread_helper() | - | ||||||||||||
1528 | { | - | ||||||||||||
1529 | Q_Q(QObject); | - | ||||||||||||
1530 | QEvent e(QEvent::ThreadChange); | - | ||||||||||||
1531 | QCoreApplication::sendEvent(q, &e); | - | ||||||||||||
1532 | for (int i = 0; i < children.size(); ++i) { | - | ||||||||||||
1533 | QObject *child = children.at(i); | - | ||||||||||||
1534 | child->d_func()->moveToThread_helper(); | - | ||||||||||||
1535 | } | - | ||||||||||||
1536 | } | - | ||||||||||||
1537 | - | |||||||||||||
1538 | void QObjectPrivate::setThreadData_helper(QThreadData *currentData, QThreadData *targetData) | - | ||||||||||||
1539 | { | - | ||||||||||||
1540 | Q_Q(QObject); | - | ||||||||||||
1541 | - | |||||||||||||
1542 | // move posted events | - | ||||||||||||
1543 | int eventsMoved = 0; | - | ||||||||||||
1544 | for (int i = 0; i < currentData->postEventList.size(); ++i) { | - | ||||||||||||
1545 | const QPostEvent &pe = currentData->postEventList.at(i); | - | ||||||||||||
1546 | if (!pe.event) | - | ||||||||||||
1547 | continue; | - | ||||||||||||
1548 | if (pe.receiver == q) { | - | ||||||||||||
1549 | // move this post event to the targetList | - | ||||||||||||
1550 | targetData->postEventList.addEvent(pe); | - | ||||||||||||
1551 | const_cast<QPostEvent &>(pe).event = 0; | - | ||||||||||||
1552 | ++eventsMoved; | - | ||||||||||||
1553 | } | - | ||||||||||||
1554 | } | - | ||||||||||||
1555 | if (eventsMoved > 0 && targetData->eventDispatcher.load()) { | - | ||||||||||||
1556 | targetData->canWait = false; | - | ||||||||||||
1557 | targetData->eventDispatcher.load()->wakeUp(); | - | ||||||||||||
1558 | } | - | ||||||||||||
1559 | - | |||||||||||||
1560 | // the current emitting thread shouldn't restore currentSender after calling moveToThread() | - | ||||||||||||
1561 | if (currentSender) | - | ||||||||||||
1562 | currentSender->ref = 0; | - | ||||||||||||
1563 | currentSender = 0; | - | ||||||||||||
1564 | - | |||||||||||||
1565 | // set new thread data | - | ||||||||||||
1566 | targetData->ref(); | - | ||||||||||||
1567 | threadData->deref(); | - | ||||||||||||
1568 | threadData = targetData; | - | ||||||||||||
1569 | - | |||||||||||||
1570 | for (int i = 0; i < children.size(); ++i) { | - | ||||||||||||
1571 | QObject *child = children.at(i); | - | ||||||||||||
1572 | child->d_func()->setThreadData_helper(currentData, targetData); | - | ||||||||||||
1573 | } | - | ||||||||||||
1574 | } | - | ||||||||||||
1575 | - | |||||||||||||
1576 | void QObjectPrivate::_q_reregisterTimers(void *pointer) | - | ||||||||||||
1577 | { | - | ||||||||||||
1578 | Q_Q(QObject); | - | ||||||||||||
1579 | QList<QAbstractEventDispatcher::TimerInfo> *timerList = reinterpret_cast<QList<QAbstractEventDispatcher::TimerInfo> *>(pointer); | - | ||||||||||||
1580 | QAbstractEventDispatcher *eventDispatcher = threadData->eventDispatcher.load(); | - | ||||||||||||
1581 | for (int i = 0; i < timerList->size(); ++i) { | - | ||||||||||||
1582 | const QAbstractEventDispatcher::TimerInfo &ti = timerList->at(i); | - | ||||||||||||
1583 | eventDispatcher->registerTimer(ti.timerId, ti.interval, ti.timerType, q); | - | ||||||||||||
1584 | } | - | ||||||||||||
1585 | delete timerList; | - | ||||||||||||
1586 | } | - | ||||||||||||
1587 | - | |||||||||||||
1588 | - | |||||||||||||
1589 | // | - | ||||||||||||
1590 | // The timer flag hasTimer is set when startTimer is called. | - | ||||||||||||
1591 | // It is not reset when killing the timer because more than | - | ||||||||||||
1592 | // one timer might be active. | - | ||||||||||||
1593 | // | - | ||||||||||||
1594 | - | |||||||||||||
1595 | /*! | - | ||||||||||||
1596 | Starts a timer and returns a timer identifier, or returns zero if | - | ||||||||||||
1597 | it could not start a timer. | - | ||||||||||||
1598 | - | |||||||||||||
1599 | A timer event will occur every \a interval milliseconds until | - | ||||||||||||
1600 | killTimer() is called. If \a interval is 0, then the timer event | - | ||||||||||||
1601 | occurs once every time there are no more window system events to | - | ||||||||||||
1602 | process. | - | ||||||||||||
1603 | - | |||||||||||||
1604 | The virtual timerEvent() function is called with the QTimerEvent | - | ||||||||||||
1605 | event parameter class when a timer event occurs. Reimplement this | - | ||||||||||||
1606 | function to get timer events. | - | ||||||||||||
1607 | - | |||||||||||||
1608 | If multiple timers are running, the QTimerEvent::timerId() can be | - | ||||||||||||
1609 | used to find out which timer was activated. | - | ||||||||||||
1610 | - | |||||||||||||
1611 | Example: | - | ||||||||||||
1612 | - | |||||||||||||
1613 | \snippet code/src_corelib_kernel_qobject.cpp 8 | - | ||||||||||||
1614 | - | |||||||||||||
1615 | Note that QTimer's accuracy depends on the underlying operating system and | - | ||||||||||||
1616 | hardware. The \a timerType argument allows you to customize the accuracy of | - | ||||||||||||
1617 | the timer. See Qt::TimerType for information on the different timer types. | - | ||||||||||||
1618 | Most platforms support an accuracy of 20 milliseconds; some provide more. | - | ||||||||||||
1619 | If Qt is unable to deliver the requested number of timer events, it will | - | ||||||||||||
1620 | silently discard some. | - | ||||||||||||
1621 | - | |||||||||||||
1622 | The QTimer class provides a high-level programming interface with | - | ||||||||||||
1623 | single-shot timers and timer signals instead of events. There is | - | ||||||||||||
1624 | also a QBasicTimer class that is more lightweight than QTimer and | - | ||||||||||||
1625 | less clumsy than using timer IDs directly. | - | ||||||||||||
1626 | - | |||||||||||||
1627 | \sa timerEvent(), killTimer(), QTimer::singleShot() | - | ||||||||||||
1628 | */ | - | ||||||||||||
1629 | - | |||||||||||||
1630 | int QObject::startTimer(int interval, Qt::TimerType timerType) | - | ||||||||||||
1631 | { | - | ||||||||||||
1632 | Q_D(QObject); | - | ||||||||||||
1633 | - | |||||||||||||
1634 | if (Q_UNLIKELY(interval < 0)) { | - | ||||||||||||
1635 | qWarning("QObject::startTimer: Timers cannot have negative intervals"); | - | ||||||||||||
1636 | return 0; | - | ||||||||||||
1637 | } | - | ||||||||||||
1638 | if (Q_UNLIKELY(!d->threadData->eventDispatcher.load())) { | - | ||||||||||||
1639 | qWarning("QObject::startTimer: Timers can only be used with threads started with QThread"); | - | ||||||||||||
1640 | return 0; | - | ||||||||||||
1641 | } | - | ||||||||||||
1642 | if (Q_UNLIKELY(thread() != QThread::currentThread())) { | - | ||||||||||||
1643 | qWarning("QObject::startTimer: Timers cannot be started from another thread"); | - | ||||||||||||
1644 | return 0; | - | ||||||||||||
1645 | } | - | ||||||||||||
1646 | int timerId = d->threadData->eventDispatcher.load()->registerTimer(interval, timerType, this); | - | ||||||||||||
1647 | if (!d->extraData) | - | ||||||||||||
1648 | d->extraData = new QObjectPrivate::ExtraData; | - | ||||||||||||
1649 | d->extraData->runningTimers.append(timerId); | - | ||||||||||||
1650 | return timerId; | - | ||||||||||||
1651 | } | - | ||||||||||||
1652 | - | |||||||||||||
1653 | /*! | - | ||||||||||||
1654 | Kills the timer with timer identifier, \a id. | - | ||||||||||||
1655 | - | |||||||||||||
1656 | The timer identifier is returned by startTimer() when a timer | - | ||||||||||||
1657 | event is started. | - | ||||||||||||
1658 | - | |||||||||||||
1659 | \sa timerEvent(), startTimer() | - | ||||||||||||
1660 | */ | - | ||||||||||||
1661 | - | |||||||||||||
1662 | void QObject::killTimer(int id) | - | ||||||||||||
1663 | { | - | ||||||||||||
1664 | Q_D(QObject); | - | ||||||||||||
1665 | if (Q_UNLIKELY(thread() != QThread::currentThread())) { | - | ||||||||||||
1666 | qWarning("QObject::killTimer: Timers cannot be stopped from another thread"); | - | ||||||||||||
1667 | return; | - | ||||||||||||
1668 | } | - | ||||||||||||
1669 | if (id) { | - | ||||||||||||
1670 | int at = d->extraData ? d->extraData->runningTimers.indexOf(id) : -1; | - | ||||||||||||
1671 | if (at == -1) { | - | ||||||||||||
1672 | // timer isn't owned by this object | - | ||||||||||||
1673 | qWarning("QObject::killTimer(): Error: timer id %d is not valid for object %p (%s, %s), timer has not been killed", | - | ||||||||||||
1674 | id, | - | ||||||||||||
1675 | this, | - | ||||||||||||
1676 | metaObject()->className(), | - | ||||||||||||
1677 | qPrintable(objectName())); | - | ||||||||||||
1678 | return; | - | ||||||||||||
1679 | } | - | ||||||||||||
1680 | - | |||||||||||||
1681 | if (d->threadData->eventDispatcher.load()) | - | ||||||||||||
1682 | d->threadData->eventDispatcher.load()->unregisterTimer(id); | - | ||||||||||||
1683 | - | |||||||||||||
1684 | d->extraData->runningTimers.remove(at); | - | ||||||||||||
1685 | QAbstractEventDispatcherPrivate::releaseTimerId(id); | - | ||||||||||||
1686 | } | - | ||||||||||||
1687 | } | - | ||||||||||||
1688 | - | |||||||||||||
1689 | - | |||||||||||||
1690 | /*! | - | ||||||||||||
1691 | \fn QObject *QObject::parent() const | - | ||||||||||||
1692 | - | |||||||||||||
1693 | Returns a pointer to the parent object. | - | ||||||||||||
1694 | - | |||||||||||||
1695 | \sa children() | - | ||||||||||||
1696 | */ | - | ||||||||||||
1697 | - | |||||||||||||
1698 | /*! | - | ||||||||||||
1699 | \fn const QObjectList &QObject::children() const | - | ||||||||||||
1700 | - | |||||||||||||
1701 | Returns a list of child objects. | - | ||||||||||||
1702 | The QObjectList class is defined in the \c{<QObject>} header | - | ||||||||||||
1703 | file as the following: | - | ||||||||||||
1704 | - | |||||||||||||
1705 | \quotefromfile kernel/qobject.h | - | ||||||||||||
1706 | \skipto /typedef .*QObjectList/ | - | ||||||||||||
1707 | \printuntil QObjectList | - | ||||||||||||
1708 | - | |||||||||||||
1709 | The first child added is the \l{QList::first()}{first} object in | - | ||||||||||||
1710 | the list and the last child added is the \l{QList::last()}{last} | - | ||||||||||||
1711 | object in the list, i.e. new children are appended at the end. | - | ||||||||||||
1712 | - | |||||||||||||
1713 | Note that the list order changes when QWidget children are | - | ||||||||||||
1714 | \l{QWidget::raise()}{raised} or \l{QWidget::lower()}{lowered}. A | - | ||||||||||||
1715 | widget that is raised becomes the last object in the list, and a | - | ||||||||||||
1716 | widget that is lowered becomes the first object in the list. | - | ||||||||||||
1717 | - | |||||||||||||
1718 | \sa findChild(), findChildren(), parent(), setParent() | - | ||||||||||||
1719 | */ | - | ||||||||||||
1720 | - | |||||||||||||
1721 | - | |||||||||||||
1722 | /*! | - | ||||||||||||
1723 | \fn T *QObject::findChild(const QString &name, Qt::FindChildOptions options) const | - | ||||||||||||
1724 | - | |||||||||||||
1725 | Returns the child of this object that can be cast into type T and | - | ||||||||||||
1726 | that is called \a name, or 0 if there is no such object. | - | ||||||||||||
1727 | Omitting the \a name argument causes all object names to be matched. | - | ||||||||||||
1728 | The search is performed recursively, unless \a options specifies the | - | ||||||||||||
1729 | option FindDirectChildrenOnly. | - | ||||||||||||
1730 | - | |||||||||||||
1731 | If there is more than one child matching the search, the most | - | ||||||||||||
1732 | direct ancestor is returned. If there are several direct | - | ||||||||||||
1733 | ancestors, it is undefined which one will be returned. In that | - | ||||||||||||
1734 | case, findChildren() should be used. | - | ||||||||||||
1735 | - | |||||||||||||
1736 | This example returns a child \c{QPushButton} of \c{parentWidget} | - | ||||||||||||
1737 | named \c{"button1"}, even if the button isn't a direct child of | - | ||||||||||||
1738 | the parent: | - | ||||||||||||
1739 | - | |||||||||||||
1740 | \snippet code/src_corelib_kernel_qobject.cpp 10 | - | ||||||||||||
1741 | - | |||||||||||||
1742 | This example returns a \c{QListWidget} child of \c{parentWidget}: | - | ||||||||||||
1743 | - | |||||||||||||
1744 | \snippet code/src_corelib_kernel_qobject.cpp 11 | - | ||||||||||||
1745 | - | |||||||||||||
1746 | This example returns a child \c{QPushButton} of \c{parentWidget} | - | ||||||||||||
1747 | (its direct parent) named \c{"button1"}: | - | ||||||||||||
1748 | - | |||||||||||||
1749 | \snippet code/src_corelib_kernel_qobject.cpp 41 | - | ||||||||||||
1750 | - | |||||||||||||
1751 | This example returns a \c{QListWidget} child of \c{parentWidget}, | - | ||||||||||||
1752 | its direct parent: | - | ||||||||||||
1753 | - | |||||||||||||
1754 | \snippet code/src_corelib_kernel_qobject.cpp 42 | - | ||||||||||||
1755 | - | |||||||||||||
1756 | \sa findChildren() | - | ||||||||||||
1757 | */ | - | ||||||||||||
1758 | - | |||||||||||||
1759 | /*! | - | ||||||||||||
1760 | \fn QList<T> QObject::findChildren(const QString &name, Qt::FindChildOptions options) const | - | ||||||||||||
1761 | - | |||||||||||||
1762 | Returns all children of this object with the given \a name that can be | - | ||||||||||||
1763 | cast to type T, or an empty list if there are no such objects. | - | ||||||||||||
1764 | Omitting the \a name argument causes all object names to be matched. | - | ||||||||||||
1765 | The search is performed recursively, unless \a options specifies the | - | ||||||||||||
1766 | option FindDirectChildrenOnly. | - | ||||||||||||
1767 | - | |||||||||||||
1768 | The following example shows how to find a list of child \c{QWidget}s of | - | ||||||||||||
1769 | the specified \c{parentWidget} named \c{widgetname}: | - | ||||||||||||
1770 | - | |||||||||||||
1771 | \snippet code/src_corelib_kernel_qobject.cpp 12 | - | ||||||||||||
1772 | - | |||||||||||||
1773 | This example returns all \c{QPushButton}s that are children of \c{parentWidget}: | - | ||||||||||||
1774 | - | |||||||||||||
1775 | \snippet code/src_corelib_kernel_qobject.cpp 13 | - | ||||||||||||
1776 | - | |||||||||||||
1777 | This example returns all \c{QPushButton}s that are immediate children of \c{parentWidget}: | - | ||||||||||||
1778 | - | |||||||||||||
1779 | \snippet code/src_corelib_kernel_qobject.cpp 43 | - | ||||||||||||
1780 | - | |||||||||||||
1781 | \sa findChild() | - | ||||||||||||
1782 | */ | - | ||||||||||||
1783 | - | |||||||||||||
1784 | /*! | - | ||||||||||||
1785 | \fn QList<T> QObject::findChildren(const QRegExp ®Exp, Qt::FindChildOptions options) const | - | ||||||||||||
1786 | \overload findChildren() | - | ||||||||||||
1787 | - | |||||||||||||
1788 | Returns the children of this object that can be cast to type T | - | ||||||||||||
1789 | and that have names matching the regular expression \a regExp, | - | ||||||||||||
1790 | or an empty list if there are no such objects. | - | ||||||||||||
1791 | The search is performed recursively, unless \a options specifies the | - | ||||||||||||
1792 | option FindDirectChildrenOnly. | - | ||||||||||||
1793 | */ | - | ||||||||||||
1794 | - | |||||||||||||
1795 | /*! | - | ||||||||||||
1796 | \fn QList<T> QObject::findChildren(const QRegularExpression &re, Qt::FindChildOptions options) const | - | ||||||||||||
1797 | \overload findChildren() | - | ||||||||||||
1798 | - | |||||||||||||
1799 | \since 5.0 | - | ||||||||||||
1800 | - | |||||||||||||
1801 | Returns the children of this object that can be cast to type T | - | ||||||||||||
1802 | and that have names matching the regular expression \a re, | - | ||||||||||||
1803 | or an empty list if there are no such objects. | - | ||||||||||||
1804 | The search is performed recursively, unless \a options specifies the | - | ||||||||||||
1805 | option FindDirectChildrenOnly. | - | ||||||||||||
1806 | */ | - | ||||||||||||
1807 | - | |||||||||||||
1808 | /*! | - | ||||||||||||
1809 | \fn T qFindChild(const QObject *obj, const QString &name) | - | ||||||||||||
1810 | \relates QObject | - | ||||||||||||
1811 | \overload qFindChildren() | - | ||||||||||||
1812 | \obsolete | - | ||||||||||||
1813 | - | |||||||||||||
1814 | This function is equivalent to | - | ||||||||||||
1815 | \a{obj}->\l{QObject::findChild()}{findChild}<T>(\a name). | - | ||||||||||||
1816 | - | |||||||||||||
1817 | \note This function was provided as a workaround for MSVC 6 | - | ||||||||||||
1818 | which did not support member template functions. It is advised | - | ||||||||||||
1819 | to use the other form in new code. | - | ||||||||||||
1820 | - | |||||||||||||
1821 | \sa QObject::findChild() | - | ||||||||||||
1822 | */ | - | ||||||||||||
1823 | - | |||||||||||||
1824 | /*! | - | ||||||||||||
1825 | \fn QList<T> qFindChildren(const QObject *obj, const QString &name) | - | ||||||||||||
1826 | \relates QObject | - | ||||||||||||
1827 | \overload qFindChildren() | - | ||||||||||||
1828 | \obsolete | - | ||||||||||||
1829 | - | |||||||||||||
1830 | This function is equivalent to | - | ||||||||||||
1831 | \a{obj}->\l{QObject::findChildren()}{findChildren}<T>(\a name). | - | ||||||||||||
1832 | - | |||||||||||||
1833 | \note This function was provided as a workaround for MSVC 6 | - | ||||||||||||
1834 | which did not support member template functions. It is advised | - | ||||||||||||
1835 | to use the other form in new code. | - | ||||||||||||
1836 | - | |||||||||||||
1837 | \sa QObject::findChildren() | - | ||||||||||||
1838 | */ | - | ||||||||||||
1839 | - | |||||||||||||
1840 | /*! | - | ||||||||||||
1841 | \fn QList<T> qFindChildren(const QObject *obj, const QRegExp ®Exp) | - | ||||||||||||
1842 | \relates QObject | - | ||||||||||||
1843 | \overload qFindChildren() | - | ||||||||||||
1844 | - | |||||||||||||
1845 | This function is equivalent to | - | ||||||||||||
1846 | \a{obj}->\l{QObject::findChildren()}{findChildren}<T>(\a regExp). | - | ||||||||||||
1847 | - | |||||||||||||
1848 | \note This function was provided as a workaround for MSVC 6 | - | ||||||||||||
1849 | which did not support member template functions. It is advised | - | ||||||||||||
1850 | to use the other form in new code. | - | ||||||||||||
1851 | - | |||||||||||||
1852 | \sa QObject::findChildren() | - | ||||||||||||
1853 | */ | - | ||||||||||||
1854 | - | |||||||||||||
1855 | /*! | - | ||||||||||||
1856 | \internal | - | ||||||||||||
1857 | */ | - | ||||||||||||
1858 | void qt_qFindChildren_helper(const QObject *parent, const QString &name, | - | ||||||||||||
1859 | const QMetaObject &mo, QList<void*> *list, Qt::FindChildOptions options) | - | ||||||||||||
1860 | { | - | ||||||||||||
1861 | if (!parent || !list) | - | ||||||||||||
1862 | return; | - | ||||||||||||
1863 | const QObjectList &children = parent->children(); | - | ||||||||||||
1864 | QObject *obj; | - | ||||||||||||
1865 | for (int i = 0; i < children.size(); ++i) { | - | ||||||||||||
1866 | obj = children.at(i); | - | ||||||||||||
1867 | if (mo.cast(obj)) { | - | ||||||||||||
1868 | if (name.isNull() || obj->objectName() == name) | - | ||||||||||||
1869 | list->append(obj); | - | ||||||||||||
1870 | } | - | ||||||||||||
1871 | if (options & Qt::FindChildrenRecursively) | - | ||||||||||||
1872 | qt_qFindChildren_helper(obj, name, mo, list, options); | - | ||||||||||||
1873 | } | - | ||||||||||||
1874 | } | - | ||||||||||||
1875 | - | |||||||||||||
1876 | #ifndef QT_NO_REGEXP | - | ||||||||||||
1877 | /*! | - | ||||||||||||
1878 | \internal | - | ||||||||||||
1879 | */ | - | ||||||||||||
1880 | void qt_qFindChildren_helper(const QObject *parent, const QRegExp &re, | - | ||||||||||||
1881 | const QMetaObject &mo, QList<void*> *list, Qt::FindChildOptions options) | - | ||||||||||||
1882 | { | - | ||||||||||||
1883 | if (!parent || !list) | - | ||||||||||||
1884 | return; | - | ||||||||||||
1885 | const QObjectList &children = parent->children(); | - | ||||||||||||
1886 | QRegExp reCopy = re; | - | ||||||||||||
1887 | QObject *obj; | - | ||||||||||||
1888 | for (int i = 0; i < children.size(); ++i) { | - | ||||||||||||
1889 | obj = children.at(i); | - | ||||||||||||
1890 | if (mo.cast(obj) && reCopy.indexIn(obj->objectName()) != -1) | - | ||||||||||||
1891 | list->append(obj); | - | ||||||||||||
1892 | - | |||||||||||||
1893 | if (options & Qt::FindChildrenRecursively) | - | ||||||||||||
1894 | qt_qFindChildren_helper(obj, re, mo, list, options); | - | ||||||||||||
1895 | } | - | ||||||||||||
1896 | } | - | ||||||||||||
1897 | #endif // QT_NO_REGEXP | - | ||||||||||||
1898 | - | |||||||||||||
1899 | #ifndef QT_NO_REGULAREXPRESSION | - | ||||||||||||
1900 | /*! | - | ||||||||||||
1901 | \internal | - | ||||||||||||
1902 | */ | - | ||||||||||||
1903 | void qt_qFindChildren_helper(const QObject *parent, const QRegularExpression &re, | - | ||||||||||||
1904 | const QMetaObject &mo, QList<void*> *list, Qt::FindChildOptions options) | - | ||||||||||||
1905 | { | - | ||||||||||||
1906 | if (!parent || !list) | - | ||||||||||||
1907 | return; | - | ||||||||||||
1908 | const QObjectList &children = parent->children(); | - | ||||||||||||
1909 | QObject *obj; | - | ||||||||||||
1910 | for (int i = 0; i < children.size(); ++i) { | - | ||||||||||||
1911 | obj = children.at(i); | - | ||||||||||||
1912 | if (mo.cast(obj)) { | - | ||||||||||||
1913 | QRegularExpressionMatch m = re.match(obj->objectName()); | - | ||||||||||||
1914 | if (m.hasMatch()) | - | ||||||||||||
1915 | list->append(obj); | - | ||||||||||||
1916 | } | - | ||||||||||||
1917 | if (options & Qt::FindChildrenRecursively) | - | ||||||||||||
1918 | qt_qFindChildren_helper(obj, re, mo, list, options); | - | ||||||||||||
1919 | } | - | ||||||||||||
1920 | } | - | ||||||||||||
1921 | #endif // QT_NO_REGULAREXPRESSION | - | ||||||||||||
1922 | - | |||||||||||||
1923 | /*! | - | ||||||||||||
1924 | \internal | - | ||||||||||||
1925 | */ | - | ||||||||||||
1926 | QObject *qt_qFindChild_helper(const QObject *parent, const QString &name, const QMetaObject &mo, Qt::FindChildOptions options) | - | ||||||||||||
1927 | { | - | ||||||||||||
1928 | if (!parent) | - | ||||||||||||
1929 | return 0; | - | ||||||||||||
1930 | const QObjectList &children = parent->children(); | - | ||||||||||||
1931 | QObject *obj; | - | ||||||||||||
1932 | int i; | - | ||||||||||||
1933 | for (i = 0; i < children.size(); ++i) { | - | ||||||||||||
1934 | obj = children.at(i); | - | ||||||||||||
1935 | if (mo.cast(obj) && (name.isNull() || obj->objectName() == name)) | - | ||||||||||||
1936 | return obj; | - | ||||||||||||
1937 | } | - | ||||||||||||
1938 | if (options & Qt::FindChildrenRecursively) { | - | ||||||||||||
1939 | for (i = 0; i < children.size(); ++i) { | - | ||||||||||||
1940 | obj = qt_qFindChild_helper(children.at(i), name, mo, options); | - | ||||||||||||
1941 | if (obj) | - | ||||||||||||
1942 | return obj; | - | ||||||||||||
1943 | } | - | ||||||||||||
1944 | } | - | ||||||||||||
1945 | return 0; | - | ||||||||||||
1946 | } | - | ||||||||||||
1947 | - | |||||||||||||
1948 | /*! | - | ||||||||||||
1949 | Makes the object a child of \a parent. | - | ||||||||||||
1950 | - | |||||||||||||
1951 | \sa parent(), children() | - | ||||||||||||
1952 | */ | - | ||||||||||||
1953 | void QObject::setParent(QObject *parent) | - | ||||||||||||
1954 | { | - | ||||||||||||
1955 | Q_D(QObject); | - | ||||||||||||
1956 | Q_ASSERT(!d->isWidget); | - | ||||||||||||
1957 | d->setParent_helper(parent); | - | ||||||||||||
1958 | } | - | ||||||||||||
1959 | - | |||||||||||||
1960 | void QObjectPrivate::deleteChildren() | - | ||||||||||||
1961 | { | - | ||||||||||||
1962 | Q_ASSERT_X(!isDeletingChildren, "QObjectPrivate::deleteChildren()", "isDeletingChildren already set, did this function recurse?"); | - | ||||||||||||
1963 | isDeletingChildren = true; | - | ||||||||||||
1964 | // delete children objects | - | ||||||||||||
1965 | // don't use qDeleteAll as the destructor of the child might | - | ||||||||||||
1966 | // delete siblings | - | ||||||||||||
1967 | for (int i = 0; i < children.count(); ++i) { | - | ||||||||||||
1968 | currentChildBeingDeleted = children.at(i); | - | ||||||||||||
1969 | children[i] = 0; | - | ||||||||||||
1970 | delete currentChildBeingDeleted; | - | ||||||||||||
1971 | } | - | ||||||||||||
1972 | children.clear(); | - | ||||||||||||
1973 | currentChildBeingDeleted = 0; | - | ||||||||||||
1974 | isDeletingChildren = false; | - | ||||||||||||
1975 | } | - | ||||||||||||
1976 | - | |||||||||||||
1977 | void QObjectPrivate::setParent_helper(QObject *o) | - | ||||||||||||
1978 | { | - | ||||||||||||
1979 | Q_Q(QObject); | - | ||||||||||||
1980 | if (o == parent) | - | ||||||||||||
1981 | return; | - | ||||||||||||
1982 | if (parent) { | - | ||||||||||||
1983 | QObjectPrivate *parentD = parent->d_func(); | - | ||||||||||||
1984 | if (parentD->isDeletingChildren && wasDeleted | - | ||||||||||||
1985 | && parentD->currentChildBeingDeleted == q) { | - | ||||||||||||
1986 | // don't do anything since QObjectPrivate::deleteChildren() already | - | ||||||||||||
1987 | // cleared our entry in parentD->children. | - | ||||||||||||
1988 | } else { | - | ||||||||||||
1989 | const int index = parentD->children.indexOf(q); | - | ||||||||||||
1990 | if (parentD->isDeletingChildren) { | - | ||||||||||||
1991 | parentD->children[index] = 0; | - | ||||||||||||
1992 | } else { | - | ||||||||||||
1993 | parentD->children.removeAt(index); | - | ||||||||||||
1994 | if (sendChildEvents && parentD->receiveChildEvents) { | - | ||||||||||||
1995 | QChildEvent e(QEvent::ChildRemoved, q); | - | ||||||||||||
1996 | QCoreApplication::sendEvent(parent, &e); | - | ||||||||||||
1997 | } | - | ||||||||||||
1998 | } | - | ||||||||||||
1999 | } | - | ||||||||||||
2000 | } | - | ||||||||||||
2001 | parent = o; | - | ||||||||||||
2002 | if (parent) { | - | ||||||||||||
2003 | // object hierarchies are constrained to a single thread | - | ||||||||||||
2004 | if (threadData != parent->d_func()->threadData) { | - | ||||||||||||
2005 | qWarning("QObject::setParent: Cannot set parent, new parent is in a different thread"); | - | ||||||||||||
2006 | parent = 0; | - | ||||||||||||
2007 | return; | - | ||||||||||||
2008 | } | - | ||||||||||||
2009 | parent->d_func()->children.append(q); | - | ||||||||||||
2010 | if(sendChildEvents && parent->d_func()->receiveChildEvents) { | - | ||||||||||||
2011 | if (!isWidget) { | - | ||||||||||||
2012 | QChildEvent e(QEvent::ChildAdded, q); | - | ||||||||||||
2013 | QCoreApplication::sendEvent(parent, &e); | - | ||||||||||||
2014 | } | - | ||||||||||||
2015 | } | - | ||||||||||||
2016 | } | - | ||||||||||||
2017 | if (!wasDeleted && !isDeletingChildren && declarativeData && QAbstractDeclarativeData::parentChanged) | - | ||||||||||||
2018 | QAbstractDeclarativeData::parentChanged(declarativeData, q, o); | - | ||||||||||||
2019 | } | - | ||||||||||||
2020 | - | |||||||||||||
2021 | /*! | - | ||||||||||||
2022 | \fn void QObject::installEventFilter(QObject *filterObj) | - | ||||||||||||
2023 | - | |||||||||||||
2024 | Installs an event filter \a filterObj on this object. For example: | - | ||||||||||||
2025 | \snippet code/src_corelib_kernel_qobject.cpp 14 | - | ||||||||||||
2026 | - | |||||||||||||
2027 | An event filter is an object that receives all events that are | - | ||||||||||||
2028 | sent to this object. The filter can either stop the event or | - | ||||||||||||
2029 | forward it to this object. The event filter \a filterObj receives | - | ||||||||||||
2030 | events via its eventFilter() function. The eventFilter() function | - | ||||||||||||
2031 | must return true if the event should be filtered, (i.e. stopped); | - | ||||||||||||
2032 | otherwise it must return false. | - | ||||||||||||
2033 | - | |||||||||||||
2034 | If multiple event filters are installed on a single object, the | - | ||||||||||||
2035 | filter that was installed last is activated first. | - | ||||||||||||
2036 | - | |||||||||||||
2037 | Here's a \c KeyPressEater class that eats the key presses of its | - | ||||||||||||
2038 | monitored objects: | - | ||||||||||||
2039 | - | |||||||||||||
2040 | \snippet code/src_corelib_kernel_qobject.cpp 15 | - | ||||||||||||
2041 | - | |||||||||||||
2042 | And here's how to install it on two widgets: | - | ||||||||||||
2043 | - | |||||||||||||
2044 | \snippet code/src_corelib_kernel_qobject.cpp 16 | - | ||||||||||||
2045 | - | |||||||||||||
2046 | The QShortcut class, for example, uses this technique to intercept | - | ||||||||||||
2047 | shortcut key presses. | - | ||||||||||||
2048 | - | |||||||||||||
2049 | \warning If you delete the receiver object in your eventFilter() | - | ||||||||||||
2050 | function, be sure to return true. If you return false, Qt sends | - | ||||||||||||
2051 | the event to the deleted object and the program will crash. | - | ||||||||||||
2052 | - | |||||||||||||
2053 | Note that the filtering object must be in the same thread as this | - | ||||||||||||
2054 | object. If \a filterObj is in a different thread, this function does | - | ||||||||||||
2055 | nothing. If either \a filterObj or this object are moved to a different | - | ||||||||||||
2056 | thread after calling this function, the event filter will not be | - | ||||||||||||
2057 | called until both objects have the same thread affinity again (it | - | ||||||||||||
2058 | is \e not removed). | - | ||||||||||||
2059 | - | |||||||||||||
2060 | \sa removeEventFilter(), eventFilter(), event() | - | ||||||||||||
2061 | */ | - | ||||||||||||
2062 | - | |||||||||||||
2063 | void QObject::installEventFilter(QObject *obj) | - | ||||||||||||
2064 | { | - | ||||||||||||
2065 | Q_D(QObject); | - | ||||||||||||
2066 | if (!obj) | - | ||||||||||||
2067 | return; | - | ||||||||||||
2068 | if (d->threadData != obj->d_func()->threadData) { | - | ||||||||||||
2069 | qWarning("QObject::installEventFilter(): Cannot filter events for objects in a different thread."); | - | ||||||||||||
2070 | return; | - | ||||||||||||
2071 | } | - | ||||||||||||
2072 | - | |||||||||||||
2073 | if (!d->extraData) | - | ||||||||||||
2074 | d->extraData = new QObjectPrivate::ExtraData; | - | ||||||||||||
2075 | - | |||||||||||||
2076 | // clean up unused items in the list | - | ||||||||||||
2077 | d->extraData->eventFilters.removeAll((QObject*)0); | - | ||||||||||||
2078 | d->extraData->eventFilters.removeAll(obj); | - | ||||||||||||
2079 | d->extraData->eventFilters.prepend(obj); | - | ||||||||||||
2080 | } | - | ||||||||||||
2081 | - | |||||||||||||
2082 | /*! | - | ||||||||||||
2083 | Removes an event filter object \a obj from this object. The | - | ||||||||||||
2084 | request is ignored if such an event filter has not been installed. | - | ||||||||||||
2085 | - | |||||||||||||
2086 | All event filters for this object are automatically removed when | - | ||||||||||||
2087 | this object is destroyed. | - | ||||||||||||
2088 | - | |||||||||||||
2089 | It is always safe to remove an event filter, even during event | - | ||||||||||||
2090 | filter activation (i.e. from the eventFilter() function). | - | ||||||||||||
2091 | - | |||||||||||||
2092 | \sa installEventFilter(), eventFilter(), event() | - | ||||||||||||
2093 | */ | - | ||||||||||||
2094 | - | |||||||||||||
2095 | void QObject::removeEventFilter(QObject *obj) | - | ||||||||||||
2096 | { | - | ||||||||||||
2097 | Q_D(QObject); | - | ||||||||||||
2098 | if (d->extraData) { | - | ||||||||||||
2099 | for (int i = 0; i < d->extraData->eventFilters.count(); ++i) { | - | ||||||||||||
2100 | if (d->extraData->eventFilters.at(i) == obj) | - | ||||||||||||
2101 | d->extraData->eventFilters[i] = 0; | - | ||||||||||||
2102 | } | - | ||||||||||||
2103 | } | - | ||||||||||||
2104 | } | - | ||||||||||||
2105 | - | |||||||||||||
2106 | - | |||||||||||||
2107 | /*! | - | ||||||||||||
2108 | \fn QObject::destroyed(QObject *obj) | - | ||||||||||||
2109 | - | |||||||||||||
2110 | This signal is emitted immediately before the object \a obj is | - | ||||||||||||
2111 | destroyed, and can not be blocked. | - | ||||||||||||
2112 | - | |||||||||||||
2113 | All the objects's children are destroyed immediately after this | - | ||||||||||||
2114 | signal is emitted. | - | ||||||||||||
2115 | - | |||||||||||||
2116 | \sa deleteLater(), QPointer | - | ||||||||||||
2117 | */ | - | ||||||||||||
2118 | - | |||||||||||||
2119 | /*! | - | ||||||||||||
2120 | Schedules this object for deletion. | - | ||||||||||||
2121 | - | |||||||||||||
2122 | The object will be deleted when control returns to the event | - | ||||||||||||
2123 | loop. If the event loop is not running when this function is | - | ||||||||||||
2124 | called (e.g. deleteLater() is called on an object before | - | ||||||||||||
2125 | QCoreApplication::exec()), the object will be deleted once the | - | ||||||||||||
2126 | event loop is started. If deleteLater() is called after the main event loop | - | ||||||||||||
2127 | has stopped, the object will not be deleted. | - | ||||||||||||
2128 | Since Qt 4.8, if deleteLater() is called on an object that lives in a | - | ||||||||||||
2129 | thread with no running event loop, the object will be destroyed when the | - | ||||||||||||
2130 | thread finishes. | - | ||||||||||||
2131 | - | |||||||||||||
2132 | Note that entering and leaving a new event loop (e.g., by opening a modal | - | ||||||||||||
2133 | dialog) will \e not perform the deferred deletion; for the object to be | - | ||||||||||||
2134 | deleted, the control must return to the event loop from which | - | ||||||||||||
2135 | deleteLater() was called. | - | ||||||||||||
2136 | - | |||||||||||||
2137 | \b{Note:} It is safe to call this function more than once; when the | - | ||||||||||||
2138 | first deferred deletion event is delivered, any pending events for the | - | ||||||||||||
2139 | object are removed from the event queue. | - | ||||||||||||
2140 | - | |||||||||||||
2141 | \sa destroyed(), QPointer | - | ||||||||||||
2142 | */ | - | ||||||||||||
2143 | void QObject::deleteLater() | - | ||||||||||||
2144 | { | - | ||||||||||||
2145 | QCoreApplication::postEvent(this, new QDeferredDeleteEvent()); | - | ||||||||||||
2146 | } | - | ||||||||||||
2147 | - | |||||||||||||
2148 | /*! | - | ||||||||||||
2149 | \fn QString QObject::tr(const char *sourceText, const char *disambiguation, int n) | - | ||||||||||||
2150 | \reentrant | - | ||||||||||||
2151 | - | |||||||||||||
2152 | Returns a translated version of \a sourceText, optionally based on a | - | ||||||||||||
2153 | \a disambiguation string and value of \a n for strings containing plurals; | - | ||||||||||||
2154 | otherwise returns QString::fromUtf8(\a sourceText) if no appropriate | - | ||||||||||||
2155 | translated string is available. | - | ||||||||||||
2156 | - | |||||||||||||
2157 | Example: | - | ||||||||||||
2158 | \snippet ../widgets/mainwindows/sdi/mainwindow.cpp implicit tr context | - | ||||||||||||
2159 | \dots | - | ||||||||||||
2160 | - | |||||||||||||
2161 | If the same \a sourceText is used in different roles within the | - | ||||||||||||
2162 | same context, an additional identifying string may be passed in | - | ||||||||||||
2163 | \a disambiguation (0 by default). In Qt 4.4 and earlier, this was | - | ||||||||||||
2164 | the preferred way to pass comments to translators. | - | ||||||||||||
2165 | - | |||||||||||||
2166 | Example: | - | ||||||||||||
2167 | - | |||||||||||||
2168 | \snippet code/src_corelib_kernel_qobject.cpp 17 | - | ||||||||||||
2169 | \dots | - | ||||||||||||
2170 | - | |||||||||||||
2171 | See \l{Writing Source Code for Translation} for a detailed description of | - | ||||||||||||
2172 | Qt's translation mechanisms in general, and the | - | ||||||||||||
2173 | \l{Writing Source Code for Translation#Disambiguation}{Disambiguation} | - | ||||||||||||
2174 | section for information on disambiguation. | - | ||||||||||||
2175 | - | |||||||||||||
2176 | \warning This method is reentrant only if all translators are | - | ||||||||||||
2177 | installed \e before calling this method. Installing or removing | - | ||||||||||||
2178 | translators while performing translations is not supported. Doing | - | ||||||||||||
2179 | so will probably result in crashes or other undesirable behavior. | - | ||||||||||||
2180 | - | |||||||||||||
2181 | \sa QCoreApplication::translate(), {Internationalization with Qt} | - | ||||||||||||
2182 | */ | - | ||||||||||||
2183 | - | |||||||||||||
2184 | /*! | - | ||||||||||||
2185 | \fn QString QObject::trUtf8(const char *sourceText, const char *disambiguation, int n) | - | ||||||||||||
2186 | \reentrant | - | ||||||||||||
2187 | \obsolete | - | ||||||||||||
2188 | - | |||||||||||||
2189 | Returns a translated version of \a sourceText, or | - | ||||||||||||
2190 | QString::fromUtf8(\a sourceText) if there is no appropriate | - | ||||||||||||
2191 | version. It is otherwise identical to tr(\a sourceText, \a | - | ||||||||||||
2192 | disambiguation, \a n). | - | ||||||||||||
2193 | - | |||||||||||||
2194 | \warning This method is reentrant only if all translators are | - | ||||||||||||
2195 | installed \e before calling this method. Installing or removing | - | ||||||||||||
2196 | translators while performing translations is not supported. Doing | - | ||||||||||||
2197 | so will probably result in crashes or other undesirable behavior. | - | ||||||||||||
2198 | - | |||||||||||||
2199 | \warning For portability reasons, we recommend that you use | - | ||||||||||||
2200 | escape sequences for specifying non-ASCII characters in string | - | ||||||||||||
2201 | literals to trUtf8(). For example: | - | ||||||||||||
2202 | - | |||||||||||||
2203 | \snippet code/src_corelib_kernel_qobject.cpp 20 | - | ||||||||||||
2204 | - | |||||||||||||
2205 | \sa tr(), QCoreApplication::translate(), {Internationalization with Qt} | - | ||||||||||||
2206 | */ | - | ||||||||||||
2207 | - | |||||||||||||
2208 | - | |||||||||||||
2209 | - | |||||||||||||
2210 | - | |||||||||||||
2211 | /***************************************************************************** | - | ||||||||||||
2212 | Signals and slots | - | ||||||||||||
2213 | *****************************************************************************/ | - | ||||||||||||
2214 | - | |||||||||||||
2215 | - | |||||||||||||
2216 | const char *qFlagLocation(const char *method) | - | ||||||||||||
2217 | { | - | ||||||||||||
2218 | QThreadData *currentThreadData = QThreadData::current(false); | - | ||||||||||||
2219 | if (currentThreadData != 0) | - | ||||||||||||
2220 | currentThreadData->flaggedSignatures.store(method); | - | ||||||||||||
2221 | return method; | - | ||||||||||||
2222 | } | - | ||||||||||||
2223 | - | |||||||||||||
2224 | static int extract_code(const char *member) | - | ||||||||||||
2225 | { | - | ||||||||||||
2226 | // extract code, ensure QMETHOD_CODE <= code <= QSIGNAL_CODE | - | ||||||||||||
2227 | return (((int)(*member) - '0') & 0x3); | - | ||||||||||||
2228 | } | - | ||||||||||||
2229 | - | |||||||||||||
2230 | static const char * extract_location(const char *member) | - | ||||||||||||
2231 | { | - | ||||||||||||
2232 | if (QThreadData::current()->flaggedSignatures.contains(member)) { | - | ||||||||||||
2233 | // signature includes location information after the first null-terminator | - | ||||||||||||
2234 | const char *location = member + qstrlen(member) + 1; | - | ||||||||||||
2235 | if (*location != '\0') | - | ||||||||||||
2236 | return location; | - | ||||||||||||
2237 | } | - | ||||||||||||
2238 | return 0; | - | ||||||||||||
2239 | } | - | ||||||||||||
2240 | - | |||||||||||||
2241 | static bool check_signal_macro(const QObject *sender, const char *signal, | - | ||||||||||||
2242 | const char *func, const char *op) | - | ||||||||||||
2243 | { | - | ||||||||||||
2244 | int sigcode = extract_code(signal); | - | ||||||||||||
2245 | if (sigcode != QSIGNAL_CODE) { | - | ||||||||||||
2246 | if (sigcode == QSLOT_CODE) | - | ||||||||||||
2247 | qWarning("QObject::%s: Attempt to %s non-signal %s::%s", | - | ||||||||||||
2248 | func, op, sender->metaObject()->className(), signal+1); | - | ||||||||||||
2249 | else | - | ||||||||||||
2250 | qWarning("QObject::%s: Use the SIGNAL macro to %s %s::%s", | - | ||||||||||||
2251 | func, op, sender->metaObject()->className(), signal); | - | ||||||||||||
2252 | return false; | - | ||||||||||||
2253 | } | - | ||||||||||||
2254 | return true; | - | ||||||||||||
2255 | } | - | ||||||||||||
2256 | - | |||||||||||||
2257 | static bool check_method_code(int code, const QObject *object, | - | ||||||||||||
2258 | const char *method, const char *func) | - | ||||||||||||
2259 | { | - | ||||||||||||
2260 | if (code != QSLOT_CODE && code != QSIGNAL_CODE) { | - | ||||||||||||
2261 | qWarning("QObject::%s: Use the SLOT or SIGNAL macro to " | - | ||||||||||||
2262 | "%s %s::%s", func, func, object->metaObject()->className(), method); | - | ||||||||||||
2263 | return false; | - | ||||||||||||
2264 | } | - | ||||||||||||
2265 | return true; | - | ||||||||||||
2266 | } | - | ||||||||||||
2267 | - | |||||||||||||
2268 | static void err_method_notfound(const QObject *object, | - | ||||||||||||
2269 | const char *method, const char *func) | - | ||||||||||||
2270 | { | - | ||||||||||||
2271 | const char *type = "method"; | - | ||||||||||||
2272 | switch (extract_code(method)) { | - | ||||||||||||
2273 | case QSLOT_CODE: type = "slot"; break; | - | ||||||||||||
2274 | case QSIGNAL_CODE: type = "signal"; break; | - | ||||||||||||
2275 | } | - | ||||||||||||
2276 | const char *loc = extract_location(method); | - | ||||||||||||
2277 | if (strchr(method,')') == 0) // common typing mistake | - | ||||||||||||
2278 | qWarning("QObject::%s: Parentheses expected, %s %s::%s%s%s", | - | ||||||||||||
2279 | func, type, object->metaObject()->className(), method+1, | - | ||||||||||||
2280 | loc ? " in ": "", loc ? loc : ""); | - | ||||||||||||
2281 | else | - | ||||||||||||
2282 | qWarning("QObject::%s: No such %s %s::%s%s%s", | - | ||||||||||||
2283 | func, type, object->metaObject()->className(), method+1, | - | ||||||||||||
2284 | loc ? " in ": "", loc ? loc : ""); | - | ||||||||||||
2285 | - | |||||||||||||
2286 | } | - | ||||||||||||
2287 | - | |||||||||||||
2288 | - | |||||||||||||
2289 | static void err_info_about_objects(const char * func, | - | ||||||||||||
2290 | const QObject * sender, | - | ||||||||||||
2291 | const QObject * receiver) | - | ||||||||||||
2292 | { | - | ||||||||||||
2293 | QString a = sender ? sender->objectName() : QString(); | - | ||||||||||||
2294 | QString b = receiver ? receiver->objectName() : QString(); | - | ||||||||||||
2295 | if (!a.isEmpty()) | - | ||||||||||||
2296 | qWarning("QObject::%s: (sender name: '%s')", func, a.toLocal8Bit().data()); | - | ||||||||||||
2297 | if (!b.isEmpty()) | - | ||||||||||||
2298 | qWarning("QObject::%s: (receiver name: '%s')", func, b.toLocal8Bit().data()); | - | ||||||||||||
2299 | } | - | ||||||||||||
2300 | - | |||||||||||||
2301 | /*! | - | ||||||||||||
2302 | Returns a pointer to the object that sent the signal, if called in | - | ||||||||||||
2303 | a slot activated by a signal; otherwise it returns 0. The pointer | - | ||||||||||||
2304 | is valid only during the execution of the slot that calls this | - | ||||||||||||
2305 | function from this object's thread context. | - | ||||||||||||
2306 | - | |||||||||||||
2307 | The pointer returned by this function becomes invalid if the | - | ||||||||||||
2308 | sender is destroyed, or if the slot is disconnected from the | - | ||||||||||||
2309 | sender's signal. | - | ||||||||||||
2310 | - | |||||||||||||
2311 | \warning This function violates the object-oriented principle of | - | ||||||||||||
2312 | modularity. However, getting access to the sender might be useful | - | ||||||||||||
2313 | when many signals are connected to a single slot. | - | ||||||||||||
2314 | - | |||||||||||||
2315 | \warning As mentioned above, the return value of this function is | - | ||||||||||||
2316 | not valid when the slot is called via a Qt::DirectConnection from | - | ||||||||||||
2317 | a thread different from this object's thread. Do not use this | - | ||||||||||||
2318 | function in this type of scenario. | - | ||||||||||||
2319 | - | |||||||||||||
2320 | \sa senderSignalIndex(), QSignalMapper | - | ||||||||||||
2321 | */ | - | ||||||||||||
2322 | - | |||||||||||||
2323 | QObject *QObject::sender() const | - | ||||||||||||
2324 | { | - | ||||||||||||
2325 | Q_D(const QObject); | - | ||||||||||||
2326 | - | |||||||||||||
2327 | QMutexLocker locker(signalSlotLock(this)); | - | ||||||||||||
2328 | if (!d->currentSender) | - | ||||||||||||
2329 | return 0; | - | ||||||||||||
2330 | - | |||||||||||||
2331 | for (QObjectPrivate::Connection *c = d->senders; c; c = c->next) { | - | ||||||||||||
2332 | if (c->sender == d->currentSender->sender) | - | ||||||||||||
2333 | return d->currentSender->sender; | - | ||||||||||||
2334 | } | - | ||||||||||||
2335 | - | |||||||||||||
2336 | return 0; | - | ||||||||||||
2337 | } | - | ||||||||||||
2338 | - | |||||||||||||
2339 | /*! | - | ||||||||||||
2340 | \since 4.8 | - | ||||||||||||
2341 | - | |||||||||||||
2342 | Returns the meta-method index of the signal that called the currently | - | ||||||||||||
2343 | executing slot, which is a member of the class returned by sender(). | - | ||||||||||||
2344 | If called outside of a slot activated by a signal, -1 is returned. | - | ||||||||||||
2345 | - | |||||||||||||
2346 | For signals with default parameters, this function will always return | - | ||||||||||||
2347 | the index with all parameters, regardless of which was used with | - | ||||||||||||
2348 | connect(). For example, the signal \c {destroyed(QObject *obj = 0)} | - | ||||||||||||
2349 | will have two different indexes (with and without the parameter), but | - | ||||||||||||
2350 | this function will always return the index with a parameter. This does | - | ||||||||||||
2351 | not apply when overloading signals with different parameters. | - | ||||||||||||
2352 | - | |||||||||||||
2353 | \warning This function violates the object-oriented principle of | - | ||||||||||||
2354 | modularity. However, getting access to the signal index might be useful | - | ||||||||||||
2355 | when many signals are connected to a single slot. | - | ||||||||||||
2356 | - | |||||||||||||
2357 | \warning The return value of this function is not valid when the slot | - | ||||||||||||
2358 | is called via a Qt::DirectConnection from a thread different from this | - | ||||||||||||
2359 | object's thread. Do not use this function in this type of scenario. | - | ||||||||||||
2360 | - | |||||||||||||
2361 | \sa sender(), QMetaObject::indexOfSignal(), QMetaObject::method() | - | ||||||||||||
2362 | */ | - | ||||||||||||
2363 | - | |||||||||||||
2364 | int QObject::senderSignalIndex() const | - | ||||||||||||
2365 | { | - | ||||||||||||
2366 | Q_D(const QObject); | - | ||||||||||||
2367 | - | |||||||||||||
2368 | QMutexLocker locker(signalSlotLock(this)); | - | ||||||||||||
2369 | if (!d->currentSender) | - | ||||||||||||
2370 | return -1; | - | ||||||||||||
2371 | - | |||||||||||||
2372 | for (QObjectPrivate::Connection *c = d->senders; c; c = c->next) { | - | ||||||||||||
2373 | if (c->sender == d->currentSender->sender) { | - | ||||||||||||
2374 | // Convert from signal range to method range | - | ||||||||||||
2375 | return QMetaObjectPrivate::signal(c->sender->metaObject(), d->currentSender->signal).methodIndex(); | - | ||||||||||||
2376 | } | - | ||||||||||||
2377 | } | - | ||||||||||||
2378 | - | |||||||||||||
2379 | return -1; | - | ||||||||||||
2380 | } | - | ||||||||||||
2381 | - | |||||||||||||
2382 | /*! | - | ||||||||||||
2383 | Returns the number of receivers connected to the \a signal. | - | ||||||||||||
2384 | - | |||||||||||||
2385 | Since both slots and signals can be used as receivers for signals, | - | ||||||||||||
2386 | and the same connections can be made many times, the number of | - | ||||||||||||
2387 | receivers is the same as the number of connections made from this | - | ||||||||||||
2388 | signal. | - | ||||||||||||
2389 | - | |||||||||||||
2390 | When calling this function, you can use the \c SIGNAL() macro to | - | ||||||||||||
2391 | pass a specific signal: | - | ||||||||||||
2392 | - | |||||||||||||
2393 | \snippet code/src_corelib_kernel_qobject.cpp 21 | - | ||||||||||||
2394 | - | |||||||||||||
2395 | \warning This function violates the object-oriented principle of | - | ||||||||||||
2396 | modularity. However, it might be useful when you need to perform | - | ||||||||||||
2397 | expensive initialization only if something is connected to a | - | ||||||||||||
2398 | signal. | - | ||||||||||||
2399 | - | |||||||||||||
2400 | \sa isSignalConnected() | - | ||||||||||||
2401 | */ | - | ||||||||||||
2402 | - | |||||||||||||
2403 | int QObject::receivers(const char *signal) const | - | ||||||||||||
2404 | { | - | ||||||||||||
2405 | Q_D(const QObject); | - | ||||||||||||
2406 | int receivers = 0; | - | ||||||||||||
2407 | if (signal) { | - | ||||||||||||
2408 | QByteArray signal_name = QMetaObject::normalizedSignature(signal); | - | ||||||||||||
2409 | signal = signal_name; | - | ||||||||||||
2410 | #ifndef QT_NO_DEBUG | - | ||||||||||||
2411 | if (!check_signal_macro(this, signal, "receivers", "bind")) | - | ||||||||||||
2412 | return 0; | - | ||||||||||||
2413 | #endif | - | ||||||||||||
2414 | signal++; // skip code | - | ||||||||||||
2415 | int signal_index = d->signalIndex(signal); | - | ||||||||||||
2416 | if (signal_index < 0) { | - | ||||||||||||
2417 | #ifndef QT_NO_DEBUG | - | ||||||||||||
2418 | err_method_notfound(this, signal-1, "receivers"); | - | ||||||||||||
2419 | #endif | - | ||||||||||||
2420 | return 0; | - | ||||||||||||
2421 | } | - | ||||||||||||
2422 | - | |||||||||||||
2423 | if (!d->isSignalConnected(signal_index)) | - | ||||||||||||
2424 | return receivers; | - | ||||||||||||
2425 | - | |||||||||||||
2426 | if (d->declarativeData && QAbstractDeclarativeData::receivers) { | - | ||||||||||||
2427 | receivers += QAbstractDeclarativeData::receivers(d->declarativeData, this, | - | ||||||||||||
2428 | signal_index); | - | ||||||||||||
2429 | } | - | ||||||||||||
2430 | - | |||||||||||||
2431 | QMutexLocker locker(signalSlotLock(this)); | - | ||||||||||||
2432 | if (d->connectionLists) { | - | ||||||||||||
2433 | if (signal_index < d->connectionLists->count()) { | - | ||||||||||||
2434 | const QObjectPrivate::Connection *c = | - | ||||||||||||
2435 | d->connectionLists->at(signal_index).first; | - | ||||||||||||
2436 | while (c) { | - | ||||||||||||
2437 | receivers += c->receiver ? 1 : 0; | - | ||||||||||||
2438 | c = c->nextConnectionList; | - | ||||||||||||
2439 | } | - | ||||||||||||
2440 | } | - | ||||||||||||
2441 | } | - | ||||||||||||
2442 | } | - | ||||||||||||
2443 | return receivers; | - | ||||||||||||
2444 | } | - | ||||||||||||
2445 | - | |||||||||||||
2446 | /*! | - | ||||||||||||
2447 | \since 5.0 | - | ||||||||||||
2448 | Returns \c true if the \a signal is connected to at least one receiver, | - | ||||||||||||
2449 | otherwise returns \c false. | - | ||||||||||||
2450 | - | |||||||||||||
2451 | \a signal must be a signal member of this object, otherwise the behaviour | - | ||||||||||||
2452 | is undefined. | - | ||||||||||||
2453 | - | |||||||||||||
2454 | \snippet code/src_corelib_kernel_qobject.cpp 49 | - | ||||||||||||
2455 | - | |||||||||||||
2456 | As the code snippet above illustrates, you can use this function | - | ||||||||||||
2457 | to avoid emitting a signal that nobody listens to. | - | ||||||||||||
2458 | - | |||||||||||||
2459 | \warning This function violates the object-oriented principle of | - | ||||||||||||
2460 | modularity. However, it might be useful when you need to perform | - | ||||||||||||
2461 | expensive initialization only if something is connected to a | - | ||||||||||||
2462 | signal. | - | ||||||||||||
2463 | */ | - | ||||||||||||
2464 | bool QObject::isSignalConnected(const QMetaMethod &signal) const | - | ||||||||||||
2465 | { | - | ||||||||||||
2466 | Q_D(const QObject); | - | ||||||||||||
2467 | if (!signal.mobj) | - | ||||||||||||
2468 | return false; | - | ||||||||||||
2469 | - | |||||||||||||
2470 | Q_ASSERT_X(signal.mobj->cast(this) && signal.methodType() == QMetaMethod::Signal, | - | ||||||||||||
2471 | "QObject::isSignalConnected" , "the parameter must be a signal member of the object"); | - | ||||||||||||
2472 | uint signalIndex = (signal.handle - QMetaObjectPrivate::get(signal.mobj)->methodData)/5; | - | ||||||||||||
2473 | - | |||||||||||||
2474 | if (signal.mobj->d.data[signal.handle + 4] & MethodCloned) | - | ||||||||||||
2475 | signalIndex = QMetaObjectPrivate::originalClone(signal.mobj, signalIndex); | - | ||||||||||||
2476 | - | |||||||||||||
2477 | signalIndex += QMetaObjectPrivate::signalOffset(signal.mobj); | - | ||||||||||||
2478 | - | |||||||||||||
2479 | if (signalIndex < sizeof(d->connectedSignals) * 8) | - | ||||||||||||
2480 | return d->isSignalConnected(signalIndex); | - | ||||||||||||
2481 | - | |||||||||||||
2482 | QMutexLocker locker(signalSlotLock(this)); | - | ||||||||||||
2483 | if (d->connectionLists) { | - | ||||||||||||
2484 | if (signalIndex < uint(d->connectionLists->count())) { | - | ||||||||||||
2485 | const QObjectPrivate::Connection *c = | - | ||||||||||||
2486 | d->connectionLists->at(signalIndex).first; | - | ||||||||||||
2487 | while (c) { | - | ||||||||||||
2488 | if (c->receiver) | - | ||||||||||||
2489 | return true; | - | ||||||||||||
2490 | c = c->nextConnectionList; | - | ||||||||||||
2491 | } | - | ||||||||||||
2492 | } | - | ||||||||||||
2493 | } | - | ||||||||||||
2494 | return false; | - | ||||||||||||
2495 | } | - | ||||||||||||
2496 | - | |||||||||||||
2497 | /*! | - | ||||||||||||
2498 | \internal | - | ||||||||||||
2499 | - | |||||||||||||
2500 | This helper function calculates signal and method index for the given | - | ||||||||||||
2501 | member in the specified class. | - | ||||||||||||
2502 | - | |||||||||||||
2503 | \list | - | ||||||||||||
2504 | \li If member.mobj is 0 then both signalIndex and methodIndex are set to -1. | - | ||||||||||||
2505 | - | |||||||||||||
2506 | \li If specified member is not a member of obj instance class (or one of | - | ||||||||||||
2507 | its parent classes) then both signalIndex and methodIndex are set to -1. | - | ||||||||||||
2508 | \endlist | - | ||||||||||||
2509 | - | |||||||||||||
2510 | This function is used by QObject::connect and QObject::disconnect which | - | ||||||||||||
2511 | are working with QMetaMethod. | - | ||||||||||||
2512 | - | |||||||||||||
2513 | \a signalIndex is set to the signal index of member. If the member | - | ||||||||||||
2514 | specified is not signal this variable is set to -1. | - | ||||||||||||
2515 | - | |||||||||||||
2516 | \a methodIndex is set to the method index of the member. If the | - | ||||||||||||
2517 | member is not a method of the object specified by the \a obj argument this | - | ||||||||||||
2518 | variable is set to -1. | - | ||||||||||||
2519 | */ | - | ||||||||||||
2520 | void QMetaObjectPrivate::memberIndexes(const QObject *obj, | - | ||||||||||||
2521 | const QMetaMethod &member, | - | ||||||||||||
2522 | int *signalIndex, int *methodIndex) | - | ||||||||||||
2523 | { | - | ||||||||||||
2524 | *signalIndex = -1; | - | ||||||||||||
2525 | *methodIndex = -1; | - | ||||||||||||
2526 | if (!obj || !member.mobj) | - | ||||||||||||
2527 | return; | - | ||||||||||||
2528 | const QMetaObject *m = obj->metaObject(); | - | ||||||||||||
2529 | // Check that member is member of obj class | - | ||||||||||||
2530 | while (m != 0 && m != member.mobj) | - | ||||||||||||
2531 | m = m->d.superdata; | - | ||||||||||||
2532 | if (!m) | - | ||||||||||||
2533 | return; | - | ||||||||||||
2534 | *signalIndex = *methodIndex = (member.handle - get(member.mobj)->methodData)/5; | - | ||||||||||||
2535 | - | |||||||||||||
2536 | int signalOffset; | - | ||||||||||||
2537 | int methodOffset; | - | ||||||||||||
2538 | computeOffsets(m, &signalOffset, &methodOffset); | - | ||||||||||||
2539 | - | |||||||||||||
2540 | *methodIndex += methodOffset; | - | ||||||||||||
2541 | if (member.methodType() == QMetaMethod::Signal) { | - | ||||||||||||
2542 | *signalIndex = originalClone(m, *signalIndex); | - | ||||||||||||
2543 | *signalIndex += signalOffset; | - | ||||||||||||
2544 | } else { | - | ||||||||||||
2545 | *signalIndex = -1; | - | ||||||||||||
2546 | } | - | ||||||||||||
2547 | } | - | ||||||||||||
2548 | - | |||||||||||||
2549 | #ifndef QT_NO_DEBUG | - | ||||||||||||
2550 | static inline void check_and_warn_compat(const QMetaObject *sender, const QMetaMethod &signal, | - | ||||||||||||
2551 | const QMetaObject *receiver, const QMetaMethod &method) | - | ||||||||||||
2552 | { | - | ||||||||||||
2553 | if (signal.attributes() & QMetaMethod::Compatibility) { | - | ||||||||||||
2554 | if (!(method.attributes() & QMetaMethod::Compatibility)) | - | ||||||||||||
2555 | qWarning("QObject::connect: Connecting from COMPAT signal (%s::%s)", | - | ||||||||||||
2556 | sender->className(), signal.methodSignature().constData()); | - | ||||||||||||
2557 | } else if ((method.attributes() & QMetaMethod::Compatibility) && | - | ||||||||||||
2558 | method.methodType() == QMetaMethod::Signal) { | - | ||||||||||||
2559 | qWarning("QObject::connect: Connecting from %s::%s to COMPAT slot (%s::%s)", | - | ||||||||||||
2560 | sender->className(), signal.methodSignature().constData(), | - | ||||||||||||
2561 | receiver->className(), method.methodSignature().constData()); | - | ||||||||||||
2562 | } | - | ||||||||||||
2563 | } | - | ||||||||||||
2564 | #endif | - | ||||||||||||
2565 | - | |||||||||||||
2566 | /*! | - | ||||||||||||
2567 | \threadsafe | - | ||||||||||||
2568 | - | |||||||||||||
2569 | Creates a connection of the given \a type from the \a signal in | - | ||||||||||||
2570 | the \a sender object to the \a method in the \a receiver object. | - | ||||||||||||
2571 | Returns a handle to the connection that can be used to disconnect | - | ||||||||||||
2572 | it later. | - | ||||||||||||
2573 | - | |||||||||||||
2574 | You must use the \c SIGNAL() and \c SLOT() macros when specifying | - | ||||||||||||
2575 | the \a signal and the \a method, for example: | - | ||||||||||||
2576 | - | |||||||||||||
2577 | \snippet code/src_corelib_kernel_qobject.cpp 22 | - | ||||||||||||
2578 | - | |||||||||||||
2579 | This example ensures that the label always displays the current | - | ||||||||||||
2580 | scroll bar value. Note that the signal and slots parameters must not | - | ||||||||||||
2581 | contain any variable names, only the type. E.g. the following would | - | ||||||||||||
2582 | not work and return false: | - | ||||||||||||
2583 | - | |||||||||||||
2584 | \snippet code/src_corelib_kernel_qobject.cpp 23 | - | ||||||||||||
2585 | - | |||||||||||||
2586 | A signal can also be connected to another signal: | - | ||||||||||||
2587 | - | |||||||||||||
2588 | \snippet code/src_corelib_kernel_qobject.cpp 24 | - | ||||||||||||
2589 | - | |||||||||||||
2590 | In this example, the \c MyWidget constructor relays a signal from | - | ||||||||||||
2591 | a private member variable, and makes it available under a name | - | ||||||||||||
2592 | that relates to \c MyWidget. | - | ||||||||||||
2593 | - | |||||||||||||
2594 | A signal can be connected to many slots and signals. Many signals | - | ||||||||||||
2595 | can be connected to one slot. | - | ||||||||||||
2596 | - | |||||||||||||
2597 | If a signal is connected to several slots, the slots are activated | - | ||||||||||||
2598 | in the same order in which the connections were made, when the | - | ||||||||||||
2599 | signal is emitted. | - | ||||||||||||
2600 | - | |||||||||||||
2601 | The function returns a QMetaObject::Connection that represents | - | ||||||||||||
2602 | a handle to a connection if it successfully | - | ||||||||||||
2603 | connects the signal to the slot. The connection handle will be invalid | - | ||||||||||||
2604 | if it cannot create the connection, for example, if QObject is unable | - | ||||||||||||
2605 | to verify the existence of either \a signal or \a method, or if their | - | ||||||||||||
2606 | signatures aren't compatible. | - | ||||||||||||
2607 | You can check if the handle is valid by casting it to a bool. | - | ||||||||||||
2608 | - | |||||||||||||
2609 | By default, a signal is emitted for every connection you make; | - | ||||||||||||
2610 | two signals are emitted for duplicate connections. You can break | - | ||||||||||||
2611 | all of these connections with a single disconnect() call. | - | ||||||||||||
2612 | If you pass the Qt::UniqueConnection \a type, the connection will only | - | ||||||||||||
2613 | be made if it is not a duplicate. If there is already a duplicate | - | ||||||||||||
2614 | (exact same signal to the exact same slot on the same objects), | - | ||||||||||||
2615 | the connection will fail and connect will return an invalid QMetaObject::Connection. | - | ||||||||||||
2616 | - | |||||||||||||
2617 | The optional \a type parameter describes the type of connection | - | ||||||||||||
2618 | to establish. In particular, it determines whether a particular | - | ||||||||||||
2619 | signal is delivered to a slot immediately or queued for delivery | - | ||||||||||||
2620 | at a later time. If the signal is queued, the parameters must be | - | ||||||||||||
2621 | of types that are known to Qt's meta-object system, because Qt | - | ||||||||||||
2622 | needs to copy the arguments to store them in an event behind the | - | ||||||||||||
2623 | scenes. If you try to use a queued connection and get the error | - | ||||||||||||
2624 | message | - | ||||||||||||
2625 | - | |||||||||||||
2626 | \snippet code/src_corelib_kernel_qobject.cpp 25 | - | ||||||||||||
2627 | - | |||||||||||||
2628 | call qRegisterMetaType() to register the data type before you | - | ||||||||||||
2629 | establish the connection. | - | ||||||||||||
2630 | - | |||||||||||||
2631 | \sa disconnect(), sender(), qRegisterMetaType(), Q_DECLARE_METATYPE() | - | ||||||||||||
2632 | */ | - | ||||||||||||
2633 | QMetaObject::Connection QObject::connect(const QObject *sender, const char *signal, | - | ||||||||||||
2634 | const QObject *receiver, const char *method, | - | ||||||||||||
2635 | Qt::ConnectionType type) | - | ||||||||||||
2636 | { | - | ||||||||||||
2637 | if (sender == 0 || receiver == 0 || signal == 0 || method == 0) { | - | ||||||||||||
2638 | qWarning("QObject::connect: Cannot connect %s::%s to %s::%s", | - | ||||||||||||
2639 | sender ? sender->metaObject()->className() : "(null)", | - | ||||||||||||
2640 | (signal && *signal) ? signal+1 : "(null)", | - | ||||||||||||
2641 | receiver ? receiver->metaObject()->className() : "(null)", | - | ||||||||||||
2642 | (method && *method) ? method+1 : "(null)"); | - | ||||||||||||
2643 | return QMetaObject::Connection(0); | - | ||||||||||||
2644 | } | - | ||||||||||||
2645 | QByteArray tmp_signal_name; | - | ||||||||||||
2646 | - | |||||||||||||
2647 | if (!check_signal_macro(sender, signal, "connect", "bind")) | - | ||||||||||||
2648 | return QMetaObject::Connection(0); | - | ||||||||||||
2649 | const QMetaObject *smeta = sender->metaObject(); | - | ||||||||||||
2650 | const char *signal_arg = signal; | - | ||||||||||||
2651 | ++signal; //skip code | - | ||||||||||||
2652 | QArgumentTypeArray signalTypes; | - | ||||||||||||
2653 | Q_ASSERT(QMetaObjectPrivate::get(smeta)->revision >= 7); | - | ||||||||||||
2654 | QByteArray signalName = QMetaObjectPrivate::decodeMethodSignature(signal, signalTypes); | - | ||||||||||||
2655 | int signal_index = QMetaObjectPrivate::indexOfSignalRelative( | - | ||||||||||||
2656 | &smeta, signalName, signalTypes.size(), signalTypes.constData()); | - | ||||||||||||
2657 | if (signal_index < 0) { | - | ||||||||||||
2658 | // check for normalized signatures | - | ||||||||||||
2659 | tmp_signal_name = QMetaObject::normalizedSignature(signal - 1); | - | ||||||||||||
2660 | signal = tmp_signal_name.constData() + 1; | - | ||||||||||||
2661 | - | |||||||||||||
2662 | signalTypes.clear(); | - | ||||||||||||
2663 | signalName = QMetaObjectPrivate::decodeMethodSignature(signal, signalTypes); | - | ||||||||||||
2664 | smeta = sender->metaObject(); | - | ||||||||||||
2665 | signal_index = QMetaObjectPrivate::indexOfSignalRelative( | - | ||||||||||||
2666 | &smeta, signalName, signalTypes.size(), signalTypes.constData()); | - | ||||||||||||
2667 | } | - | ||||||||||||
2668 | if (signal_index < 0) { | - | ||||||||||||
2669 | err_method_notfound(sender, signal_arg, "connect"); | - | ||||||||||||
2670 | err_info_about_objects("connect", sender, receiver); | - | ||||||||||||
2671 | return QMetaObject::Connection(0); | - | ||||||||||||
2672 | } | - | ||||||||||||
2673 | signal_index = QMetaObjectPrivate::originalClone(smeta, signal_index); | - | ||||||||||||
2674 | signal_index += QMetaObjectPrivate::signalOffset(smeta); | - | ||||||||||||
2675 | - | |||||||||||||
2676 | QByteArray tmp_method_name; | - | ||||||||||||
2677 | int membcode = extract_code(method); | - | ||||||||||||
2678 | - | |||||||||||||
2679 | if (!check_method_code(membcode, receiver, method, "connect")) | - | ||||||||||||
2680 | return QMetaObject::Connection(0); | - | ||||||||||||
2681 | const char *method_arg = method; | - | ||||||||||||
2682 | ++method; // skip code | - | ||||||||||||
2683 | - | |||||||||||||
2684 | QArgumentTypeArray methodTypes; | - | ||||||||||||
2685 | QByteArray methodName = QMetaObjectPrivate::decodeMethodSignature(method, methodTypes); | - | ||||||||||||
2686 | const QMetaObject *rmeta = receiver->metaObject(); | - | ||||||||||||
2687 | int method_index_relative = -1; | - | ||||||||||||
2688 | Q_ASSERT(QMetaObjectPrivate::get(rmeta)->revision >= 7); | - | ||||||||||||
2689 | switch (membcode) { | - | ||||||||||||
2690 | case QSLOT_CODE: | - | ||||||||||||
2691 | method_index_relative = QMetaObjectPrivate::indexOfSlotRelative( | - | ||||||||||||
2692 | &rmeta, methodName, methodTypes.size(), methodTypes.constData()); | - | ||||||||||||
2693 | break; | - | ||||||||||||
2694 | case QSIGNAL_CODE: | - | ||||||||||||
2695 | method_index_relative = QMetaObjectPrivate::indexOfSignalRelative( | - | ||||||||||||
2696 | &rmeta, methodName, methodTypes.size(), methodTypes.constData()); | - | ||||||||||||
2697 | break; | - | ||||||||||||
2698 | } | - | ||||||||||||
2699 | if (method_index_relative < 0) { | - | ||||||||||||
2700 | // check for normalized methods | - | ||||||||||||
2701 | tmp_method_name = QMetaObject::normalizedSignature(method); | - | ||||||||||||
2702 | method = tmp_method_name.constData(); | - | ||||||||||||
2703 | - | |||||||||||||
2704 | methodTypes.clear(); | - | ||||||||||||
2705 | methodName = QMetaObjectPrivate::decodeMethodSignature(method, methodTypes); | - | ||||||||||||
2706 | // rmeta may have been modified above | - | ||||||||||||
2707 | rmeta = receiver->metaObject(); | - | ||||||||||||
2708 | switch (membcode) { | - | ||||||||||||
2709 | case QSLOT_CODE: | - | ||||||||||||
2710 | method_index_relative = QMetaObjectPrivate::indexOfSlotRelative( | - | ||||||||||||
2711 | &rmeta, methodName, methodTypes.size(), methodTypes.constData()); | - | ||||||||||||
2712 | break; | - | ||||||||||||
2713 | case QSIGNAL_CODE: | - | ||||||||||||
2714 | method_index_relative = QMetaObjectPrivate::indexOfSignalRelative( | - | ||||||||||||
2715 | &rmeta, methodName, methodTypes.size(), methodTypes.constData()); | - | ||||||||||||
2716 | break; | - | ||||||||||||
2717 | } | - | ||||||||||||
2718 | } | - | ||||||||||||
2719 | - | |||||||||||||
2720 | if (method_index_relative < 0) { | - | ||||||||||||
2721 | err_method_notfound(receiver, method_arg, "connect"); | - | ||||||||||||
2722 | err_info_about_objects("connect", sender, receiver); | - | ||||||||||||
2723 | return QMetaObject::Connection(0); | - | ||||||||||||
2724 | } | - | ||||||||||||
2725 | - | |||||||||||||
2726 | if (!QMetaObjectPrivate::checkConnectArgs(signalTypes.size(), signalTypes.constData(), | - | ||||||||||||
2727 | methodTypes.size(), methodTypes.constData())) { | - | ||||||||||||
2728 | qWarning("QObject::connect: Incompatible sender/receiver arguments" | - | ||||||||||||
2729 | "\n %s::%s --> %s::%s", | - | ||||||||||||
2730 | sender->metaObject()->className(), signal, | - | ||||||||||||
2731 | receiver->metaObject()->className(), method); | - | ||||||||||||
2732 | return QMetaObject::Connection(0); | - | ||||||||||||
2733 | } | - | ||||||||||||
2734 | - | |||||||||||||
2735 | int *types = 0; | - | ||||||||||||
2736 | if ((type == Qt::QueuedConnection) | - | ||||||||||||
2737 | && !(types = queuedConnectionTypes(signalTypes.constData(), signalTypes.size()))) { | - | ||||||||||||
2738 | return QMetaObject::Connection(0); | - | ||||||||||||
2739 | } | - | ||||||||||||
2740 | - | |||||||||||||
2741 | #ifndef QT_NO_DEBUG | - | ||||||||||||
2742 | QMetaMethod smethod = QMetaObjectPrivate::signal(smeta, signal_index); | - | ||||||||||||
2743 | QMetaMethod rmethod = rmeta->method(method_index_relative + rmeta->methodOffset()); | - | ||||||||||||
2744 | check_and_warn_compat(smeta, smethod, rmeta, rmethod); | - | ||||||||||||
2745 | #endif | - | ||||||||||||
2746 | QMetaObject::Connection handle = QMetaObject::Connection(QMetaObjectPrivate::connect( | - | ||||||||||||
2747 | sender, signal_index, smeta, receiver, method_index_relative, rmeta ,type, types)); | - | ||||||||||||
2748 | return handle; | - | ||||||||||||
2749 | } | - | ||||||||||||
2750 | - | |||||||||||||
2751 | /*! | - | ||||||||||||
2752 | \since 4.8 | - | ||||||||||||
2753 | - | |||||||||||||
2754 | Creates a connection of the given \a type from the \a signal in | - | ||||||||||||
2755 | the \a sender object to the \a method in the \a receiver object. | - | ||||||||||||
2756 | Returns a handle to the connection that can be used to disconnect | - | ||||||||||||
2757 | it later. | - | ||||||||||||
2758 | - | |||||||||||||
2759 | The Connection handle will be invalid if it cannot create the | - | ||||||||||||
2760 | connection, for example, the parameters were invalid. | - | ||||||||||||
2761 | You can check if the QMetaObject::Connection is valid by casting it to a bool. | - | ||||||||||||
2762 | - | |||||||||||||
2763 | This function works in the same way as | - | ||||||||||||
2764 | \c {connect(const QObject *sender, const char *signal, | - | ||||||||||||
2765 | const QObject *receiver, const char *method, | - | ||||||||||||
2766 | Qt::ConnectionType type)} | - | ||||||||||||
2767 | but it uses QMetaMethod to specify signal and method. | - | ||||||||||||
2768 | - | |||||||||||||
2769 | \sa connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type) | - | ||||||||||||
2770 | */ | - | ||||||||||||
2771 | QMetaObject::Connection QObject::connect(const QObject *sender, const QMetaMethod &signal, | - | ||||||||||||
2772 | const QObject *receiver, const QMetaMethod &method, | - | ||||||||||||
2773 | Qt::ConnectionType type) | - | ||||||||||||
2774 | { | - | ||||||||||||
2775 | if (sender == 0 | - | ||||||||||||
2776 | || receiver == 0 | - | ||||||||||||
2777 | || signal.methodType() != QMetaMethod::Signal | - | ||||||||||||
2778 | || method.methodType() == QMetaMethod::Constructor) { | - | ||||||||||||
2779 | qWarning("QObject::connect: Cannot connect %s::%s to %s::%s", | - | ||||||||||||
2780 | sender ? sender->metaObject()->className() : "(null)", | - | ||||||||||||
2781 | signal.methodSignature().constData(), | - | ||||||||||||
2782 | receiver ? receiver->metaObject()->className() : "(null)", | - | ||||||||||||
2783 | method.methodSignature().constData() ); | - | ||||||||||||
2784 | return QMetaObject::Connection(0); | - | ||||||||||||
2785 | } | - | ||||||||||||
2786 | - | |||||||||||||
2787 | int signal_index; | - | ||||||||||||
2788 | int method_index; | - | ||||||||||||
2789 | { | - | ||||||||||||
2790 | int dummy; | - | ||||||||||||
2791 | QMetaObjectPrivate::memberIndexes(sender, signal, &signal_index, &dummy); | - | ||||||||||||
2792 | QMetaObjectPrivate::memberIndexes(receiver, method, &dummy, &method_index); | - | ||||||||||||
2793 | } | - | ||||||||||||
2794 | - | |||||||||||||
2795 | const QMetaObject *smeta = sender->metaObject(); | - | ||||||||||||
2796 | const QMetaObject *rmeta = receiver->metaObject(); | - | ||||||||||||
2797 | if (signal_index == -1) { | - | ||||||||||||
2798 | qWarning("QObject::connect: Can't find signal %s on instance of class %s", | - | ||||||||||||
2799 | signal.methodSignature().constData(), smeta->className()); | - | ||||||||||||
2800 | return QMetaObject::Connection(0); | - | ||||||||||||
2801 | } | - | ||||||||||||
2802 | if (method_index == -1) { | - | ||||||||||||
2803 | qWarning("QObject::connect: Can't find method %s on instance of class %s", | - | ||||||||||||
2804 | method.methodSignature().constData(), rmeta->className()); | - | ||||||||||||
2805 | return QMetaObject::Connection(0); | - | ||||||||||||
2806 | } | - | ||||||||||||
2807 | - | |||||||||||||
2808 | if (!QMetaObject::checkConnectArgs(signal.methodSignature().constData(), method.methodSignature().constData())) { | - | ||||||||||||
2809 | qWarning("QObject::connect: Incompatible sender/receiver arguments" | - | ||||||||||||
2810 | "\n %s::%s --> %s::%s", | - | ||||||||||||
2811 | smeta->className(), signal.methodSignature().constData(), | - | ||||||||||||
2812 | rmeta->className(), method.methodSignature().constData()); | - | ||||||||||||
2813 | return QMetaObject::Connection(0); | - | ||||||||||||
2814 | } | - | ||||||||||||
2815 | - | |||||||||||||
2816 | int *types = 0; | - | ||||||||||||
2817 | if ((type == Qt::QueuedConnection) | - | ||||||||||||
2818 | && !(types = queuedConnectionTypes(signal.parameterTypes()))) | - | ||||||||||||
2819 | return QMetaObject::Connection(0); | - | ||||||||||||
2820 | - | |||||||||||||
2821 | #ifndef QT_NO_DEBUG | - | ||||||||||||
2822 | check_and_warn_compat(smeta, signal, rmeta, method); | - | ||||||||||||
2823 | #endif | - | ||||||||||||
2824 | QMetaObject::Connection handle = QMetaObject::Connection(QMetaObjectPrivate::connect( | - | ||||||||||||
2825 | sender, signal_index, signal.enclosingMetaObject(), receiver, method_index, 0, type, types)); | - | ||||||||||||
2826 | return handle; | - | ||||||||||||
2827 | } | - | ||||||||||||
2828 | - | |||||||||||||
2829 | /*! | - | ||||||||||||
2830 | \fn bool QObject::connect(const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const | - | ||||||||||||
2831 | \overload connect() | - | ||||||||||||
2832 | \threadsafe | - | ||||||||||||
2833 | - | |||||||||||||
2834 | Connects \a signal from the \a sender object to this object's \a | - | ||||||||||||
2835 | method. | - | ||||||||||||
2836 | - | |||||||||||||
2837 | Equivalent to connect(\a sender, \a signal, \c this, \a method, \a type). | - | ||||||||||||
2838 | - | |||||||||||||
2839 | Every connection you make emits a signal, so duplicate connections emit | - | ||||||||||||
2840 | two signals. You can break a connection using disconnect(). | - | ||||||||||||
2841 | - | |||||||||||||
2842 | \sa disconnect() | - | ||||||||||||
2843 | */ | - | ||||||||||||
2844 | - | |||||||||||||
2845 | /*! | - | ||||||||||||
2846 | \threadsafe | - | ||||||||||||
2847 | - | |||||||||||||
2848 | Disconnects \a signal in object \a sender from \a method in object | - | ||||||||||||
2849 | \a receiver. Returns \c true if the connection is successfully broken; | - | ||||||||||||
2850 | otherwise returns \c false. | - | ||||||||||||
2851 | - | |||||||||||||
2852 | A signal-slot connection is removed when either of the objects | - | ||||||||||||
2853 | involved are destroyed. | - | ||||||||||||
2854 | - | |||||||||||||
2855 | disconnect() is typically used in three ways, as the following | - | ||||||||||||
2856 | examples demonstrate. | - | ||||||||||||
2857 | \list 1 | - | ||||||||||||
2858 | \li Disconnect everything connected to an object's signals: | - | ||||||||||||
2859 | - | |||||||||||||
2860 | \snippet code/src_corelib_kernel_qobject.cpp 26 | - | ||||||||||||
2861 | - | |||||||||||||
2862 | equivalent to the non-static overloaded function | - | ||||||||||||
2863 | - | |||||||||||||
2864 | \snippet code/src_corelib_kernel_qobject.cpp 27 | - | ||||||||||||
2865 | - | |||||||||||||
2866 | \li Disconnect everything connected to a specific signal: | - | ||||||||||||
2867 | - | |||||||||||||
2868 | \snippet code/src_corelib_kernel_qobject.cpp 28 | - | ||||||||||||
2869 | - | |||||||||||||
2870 | equivalent to the non-static overloaded function | - | ||||||||||||
2871 | - | |||||||||||||
2872 | \snippet code/src_corelib_kernel_qobject.cpp 29 | - | ||||||||||||
2873 | - | |||||||||||||
2874 | \li Disconnect a specific receiver: | - | ||||||||||||
2875 | - | |||||||||||||
2876 | \snippet code/src_corelib_kernel_qobject.cpp 30 | - | ||||||||||||
2877 | - | |||||||||||||
2878 | equivalent to the non-static overloaded function | - | ||||||||||||
2879 | - | |||||||||||||
2880 | \snippet code/src_corelib_kernel_qobject.cpp 31 | - | ||||||||||||
2881 | - | |||||||||||||
2882 | \endlist | - | ||||||||||||
2883 | - | |||||||||||||
2884 | 0 may be used as a wildcard, meaning "any signal", "any receiving | - | ||||||||||||
2885 | object", or "any slot in the receiving object", respectively. | - | ||||||||||||
2886 | - | |||||||||||||
2887 | The \a sender may never be 0. (You cannot disconnect signals from | - | ||||||||||||
2888 | more than one object in a single call.) | - | ||||||||||||
2889 | - | |||||||||||||
2890 | If \a signal is 0, it disconnects \a receiver and \a method from | - | ||||||||||||
2891 | any signal. If not, only the specified signal is disconnected. | - | ||||||||||||
2892 | - | |||||||||||||
2893 | If \a receiver is 0, it disconnects anything connected to \a | - | ||||||||||||
2894 | signal. If not, slots in objects other than \a receiver are not | - | ||||||||||||
2895 | disconnected. | - | ||||||||||||
2896 | - | |||||||||||||
2897 | If \a method is 0, it disconnects anything that is connected to \a | - | ||||||||||||
2898 | receiver. If not, only slots named \a method will be disconnected, | - | ||||||||||||
2899 | and all other slots are left alone. The \a method must be 0 if \a | - | ||||||||||||
2900 | receiver is left out, so you cannot disconnect a | - | ||||||||||||
2901 | specifically-named slot on all objects. | - | ||||||||||||
2902 | - | |||||||||||||
2903 | \sa connect() | - | ||||||||||||
2904 | */ | - | ||||||||||||
2905 | bool QObject::disconnect(const QObject *sender, const char *signal, | - | ||||||||||||
2906 | const QObject *receiver, const char *method) | - | ||||||||||||
2907 | { | - | ||||||||||||
2908 | if (sender == 0 || (receiver == 0 && method != 0)) { | - | ||||||||||||
2909 | qWarning("QObject::disconnect: Unexpected null parameter"); | - | ||||||||||||
2910 | return false; | - | ||||||||||||
2911 | } | - | ||||||||||||
2912 | - | |||||||||||||
2913 | const char *signal_arg = signal; | - | ||||||||||||
2914 | QByteArray signal_name; | - | ||||||||||||
2915 | bool signal_found = false; | - | ||||||||||||
2916 | if (signal) { | - | ||||||||||||
2917 | QT_TRY { | - | ||||||||||||
2918 | signal_name = QMetaObject::normalizedSignature(signal); | - | ||||||||||||
2919 | signal = signal_name.constData(); | - | ||||||||||||
2920 | } QT_CATCH (const std::bad_alloc &) { | - | ||||||||||||
2921 | // if the signal is already normalized, we can continue. | - | ||||||||||||
2922 | if (sender->metaObject()->indexOfSignal(signal + 1) == -1) | - | ||||||||||||
2923 | QT_RETHROW; | - | ||||||||||||
2924 | } | - | ||||||||||||
2925 | - | |||||||||||||
2926 | if (!check_signal_macro(sender, signal, "disconnect", "unbind")) | - | ||||||||||||
2927 | return false; | - | ||||||||||||
2928 | signal++; // skip code | - | ||||||||||||
2929 | } | - | ||||||||||||
2930 | - | |||||||||||||
2931 | QByteArray method_name; | - | ||||||||||||
2932 | const char *method_arg = method; | - | ||||||||||||
2933 | int membcode = -1; | - | ||||||||||||
2934 | bool method_found = false; | - | ||||||||||||
2935 | if (method) { | - | ||||||||||||
2936 | QT_TRY { | - | ||||||||||||
2937 | method_name = QMetaObject::normalizedSignature(method); | - | ||||||||||||
2938 | method = method_name.constData(); | - | ||||||||||||
2939 | } QT_CATCH(const std::bad_alloc &) { | - | ||||||||||||
2940 | // if the method is already normalized, we can continue. | - | ||||||||||||
2941 | if (receiver->metaObject()->indexOfMethod(method + 1) == -1) | - | ||||||||||||
2942 | QT_RETHROW; | - | ||||||||||||
2943 | } | - | ||||||||||||
2944 | - | |||||||||||||
2945 | membcode = extract_code(method); | - | ||||||||||||
2946 | if (!check_method_code(membcode, receiver, method, "disconnect")) | - | ||||||||||||
2947 | return false; | - | ||||||||||||
2948 | method++; // skip code | - | ||||||||||||
2949 | } | - | ||||||||||||
2950 | - | |||||||||||||
2951 | /* We now iterate through all the sender's and receiver's meta | - | ||||||||||||
2952 | * objects in order to also disconnect possibly shadowed signals | - | ||||||||||||
2953 | * and slots with the same signature. | - | ||||||||||||
2954 | */ | - | ||||||||||||
2955 | bool res = false; | - | ||||||||||||
2956 | const QMetaObject *smeta = sender->metaObject(); | - | ||||||||||||
2957 | QByteArray signalName; | - | ||||||||||||
2958 | QArgumentTypeArray signalTypes; | - | ||||||||||||
2959 | Q_ASSERT(QMetaObjectPrivate::get(smeta)->revision >= 7); | - | ||||||||||||
2960 | if (signal) | - | ||||||||||||
2961 | signalName = QMetaObjectPrivate::decodeMethodSignature(signal, signalTypes); | - | ||||||||||||
2962 | QByteArray methodName; | - | ||||||||||||
2963 | QArgumentTypeArray methodTypes; | - | ||||||||||||
2964 | Q_ASSERT(!receiver || QMetaObjectPrivate::get(receiver->metaObject())->revision >= 7); | - | ||||||||||||
2965 | if (method) | - | ||||||||||||
2966 | methodName = QMetaObjectPrivate::decodeMethodSignature(method, methodTypes); | - | ||||||||||||
2967 | do { | - | ||||||||||||
2968 | int signal_index = -1; | - | ||||||||||||
2969 | if (signal) { | - | ||||||||||||
2970 | signal_index = QMetaObjectPrivate::indexOfSignalRelative( | - | ||||||||||||
2971 | &smeta, signalName, signalTypes.size(), signalTypes.constData()); | - | ||||||||||||
2972 | if (signal_index < 0) | - | ||||||||||||
2973 | break; | - | ||||||||||||
2974 | signal_index = QMetaObjectPrivate::originalClone(smeta, signal_index); | - | ||||||||||||
2975 | signal_index += QMetaObjectPrivate::signalOffset(smeta); | - | ||||||||||||
2976 | signal_found = true; | - | ||||||||||||
2977 | } | - | ||||||||||||
2978 | - | |||||||||||||
2979 | if (!method) { | - | ||||||||||||
2980 | res |= QMetaObjectPrivate::disconnect(sender, signal_index, smeta, receiver, -1, 0); | - | ||||||||||||
2981 | } else { | - | ||||||||||||
2982 | const QMetaObject *rmeta = receiver->metaObject(); | - | ||||||||||||
2983 | do { | - | ||||||||||||
2984 | int method_index = QMetaObjectPrivate::indexOfMethod( | - | ||||||||||||
2985 | rmeta, methodName, methodTypes.size(), methodTypes.constData()); | - | ||||||||||||
2986 | if (method_index >= 0) | - | ||||||||||||
2987 | while (method_index < rmeta->methodOffset()) | - | ||||||||||||
2988 | rmeta = rmeta->superClass(); | - | ||||||||||||
2989 | if (method_index < 0) | - | ||||||||||||
2990 | break; | - | ||||||||||||
2991 | res |= QMetaObjectPrivate::disconnect(sender, signal_index, smeta, receiver, method_index, 0); | - | ||||||||||||
2992 | method_found = true; | - | ||||||||||||
2993 | } while ((rmeta = rmeta->superClass())); | - | ||||||||||||
2994 | } | - | ||||||||||||
2995 | } while (signal && (smeta = smeta->superClass())); | - | ||||||||||||
2996 | - | |||||||||||||
2997 | if (signal && !signal_found) { | - | ||||||||||||
2998 | err_method_notfound(sender, signal_arg, "disconnect"); | - | ||||||||||||
2999 | err_info_about_objects("disconnect", sender, receiver); | - | ||||||||||||
3000 | } else if (method && !method_found) { | - | ||||||||||||
3001 | err_method_notfound(receiver, method_arg, "disconnect"); | - | ||||||||||||
3002 | err_info_about_objects("disconnect", sender, receiver); | - | ||||||||||||
3003 | } | - | ||||||||||||
3004 | if (res) { | - | ||||||||||||
3005 | if (!signal) | - | ||||||||||||
3006 | const_cast<QObject*>(sender)->disconnectNotify(QMetaMethod()); | - | ||||||||||||
3007 | } | - | ||||||||||||
3008 | return res; | - | ||||||||||||
3009 | } | - | ||||||||||||
3010 | - | |||||||||||||
3011 | /*! | - | ||||||||||||
3012 | \since 4.8 | - | ||||||||||||
3013 | - | |||||||||||||
3014 | Disconnects \a signal in object \a sender from \a method in object | - | ||||||||||||
3015 | \a receiver. Returns \c true if the connection is successfully broken; | - | ||||||||||||
3016 | otherwise returns \c false. | - | ||||||||||||
3017 | - | |||||||||||||
3018 | This function provides the same possibilities like | - | ||||||||||||
3019 | \c {disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method) } | - | ||||||||||||
3020 | but uses QMetaMethod to represent the signal and the method to be disconnected. | - | ||||||||||||
3021 | - | |||||||||||||
3022 | Additionally this function returnsfalse and no signals and slots disconnected | - | ||||||||||||
3023 | if: | - | ||||||||||||
3024 | \list 1 | - | ||||||||||||
3025 | - | |||||||||||||
3026 | \li \a signal is not a member of sender class or one of its parent classes. | - | ||||||||||||
3027 | - | |||||||||||||
3028 | \li \a method is not a member of receiver class or one of its parent classes. | - | ||||||||||||
3029 | - | |||||||||||||
3030 | \li \a signal instance represents not a signal. | - | ||||||||||||
3031 | - | |||||||||||||
3032 | \endlist | - | ||||||||||||
3033 | - | |||||||||||||
3034 | QMetaMethod() may be used as wildcard in the meaning "any signal" or "any slot in receiving object". | - | ||||||||||||
3035 | In the same way 0 can be used for \a receiver in the meaning "any receiving object". In this case | - | ||||||||||||
3036 | method should also be QMetaMethod(). \a sender parameter should be never 0. | - | ||||||||||||
3037 | - | |||||||||||||
3038 | \sa disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method) | - | ||||||||||||
3039 | */ | - | ||||||||||||
3040 | bool QObject::disconnect(const QObject *sender, const QMetaMethod &signal, | - | ||||||||||||
3041 | const QObject *receiver, const QMetaMethod &method) | - | ||||||||||||
3042 | { | - | ||||||||||||
3043 | if (sender == 0 || (receiver == 0 && method.mobj != 0)) { | - | ||||||||||||
3044 | qWarning("QObject::disconnect: Unexpected null parameter"); | - | ||||||||||||
3045 | return false; | - | ||||||||||||
3046 | } | - | ||||||||||||
3047 | if (signal.mobj) { | - | ||||||||||||
3048 | if(signal.methodType() != QMetaMethod::Signal) { | - | ||||||||||||
3049 | qWarning("QObject::%s: Attempt to %s non-signal %s::%s", | - | ||||||||||||
3050 | "disconnect","unbind", | - | ||||||||||||
3051 | sender->metaObject()->className(), signal.methodSignature().constData()); | - | ||||||||||||
3052 | return false; | - | ||||||||||||
3053 | } | - | ||||||||||||
3054 | } | - | ||||||||||||
3055 | if (method.mobj) { | - | ||||||||||||
3056 | if(method.methodType() == QMetaMethod::Constructor) { | - | ||||||||||||
3057 | qWarning("QObject::disconect: cannot use constructor as argument %s::%s", | - | ||||||||||||
3058 | receiver->metaObject()->className(), method.methodSignature().constData()); | - | ||||||||||||
3059 | return false; | - | ||||||||||||
3060 | } | - | ||||||||||||
3061 | } | - | ||||||||||||
3062 | - | |||||||||||||
3063 | // Reconstructing SIGNAL() macro result for signal.methodSignature() string | - | ||||||||||||
3064 | QByteArray signalSignature; | - | ||||||||||||
3065 | if (signal.mobj) { | - | ||||||||||||
3066 | signalSignature.reserve(signal.methodSignature().size()+1); | - | ||||||||||||
3067 | signalSignature.append((char)(QSIGNAL_CODE + '0')); | - | ||||||||||||
3068 | signalSignature.append(signal.methodSignature()); | - | ||||||||||||
3069 | } | - | ||||||||||||
3070 | - | |||||||||||||
3071 | int signal_index; | - | ||||||||||||
3072 | int method_index; | - | ||||||||||||
3073 | { | - | ||||||||||||
3074 | int dummy; | - | ||||||||||||
3075 | QMetaObjectPrivate::memberIndexes(sender, signal, &signal_index, &dummy); | - | ||||||||||||
3076 | QMetaObjectPrivate::memberIndexes(receiver, method, &dummy, &method_index); | - | ||||||||||||
3077 | } | - | ||||||||||||
3078 | // If we are here sender is not null. If signal is not null while signal_index | - | ||||||||||||
3079 | // is -1 then this signal is not a member of sender. | - | ||||||||||||
3080 | if (signal.mobj && signal_index == -1) { | - | ||||||||||||
3081 | qWarning("QObject::disconect: signal %s not found on class %s", | - | ||||||||||||
3082 | signal.methodSignature().constData(), sender->metaObject()->className()); | - | ||||||||||||
3083 | return false; | - | ||||||||||||
3084 | } | - | ||||||||||||
3085 | // If this condition is true then method is not a member of receeiver. | - | ||||||||||||
3086 | if (receiver && method.mobj && method_index == -1) { | - | ||||||||||||
3087 | qWarning("QObject::disconect: method %s not found on class %s", | - | ||||||||||||
3088 | method.methodSignature().constData(), receiver->metaObject()->className()); | - | ||||||||||||
3089 | return false; | - | ||||||||||||
3090 | } | - | ||||||||||||
3091 | - | |||||||||||||
3092 | if (!QMetaObjectPrivate::disconnect(sender, signal_index, signal.mobj, receiver, method_index, 0)) | - | ||||||||||||
3093 | return false; | - | ||||||||||||
3094 | - | |||||||||||||
3095 | if (!signal.isValid()) { | - | ||||||||||||
3096 | // The signal is a wildcard, meaning all signals were disconnected. | - | ||||||||||||
3097 | // QMetaObjectPrivate::disconnect() doesn't call disconnectNotify() | - | ||||||||||||
3098 | // per connection in this case. Call it once now, with an invalid | - | ||||||||||||
3099 | // QMetaMethod as argument, as documented. | - | ||||||||||||
3100 | const_cast<QObject*>(sender)->disconnectNotify(signal); | - | ||||||||||||
3101 | } | - | ||||||||||||
3102 | return true; | - | ||||||||||||
3103 | } | - | ||||||||||||
3104 | - | |||||||||||||
3105 | /*! | - | ||||||||||||
3106 | \threadsafe | - | ||||||||||||
3107 | - | |||||||||||||
3108 | \fn bool QObject::disconnect(const char *signal, const QObject *receiver, const char *method) const | - | ||||||||||||
3109 | \overload disconnect() | - | ||||||||||||
3110 | - | |||||||||||||
3111 | Disconnects \a signal from \a method of \a receiver. | - | ||||||||||||
3112 | - | |||||||||||||
3113 | A signal-slot connection is removed when either of the objects | - | ||||||||||||
3114 | involved are destroyed. | - | ||||||||||||
3115 | */ | - | ||||||||||||
3116 | - | |||||||||||||
3117 | /*! | - | ||||||||||||
3118 | \fn bool QObject::disconnect(const QObject *receiver, const char *method) const | - | ||||||||||||
3119 | \overload disconnect() | - | ||||||||||||
3120 | - | |||||||||||||
3121 | Disconnects all signals in this object from \a receiver's \a | - | ||||||||||||
3122 | method. | - | ||||||||||||
3123 | - | |||||||||||||
3124 | A signal-slot connection is removed when either of the objects | - | ||||||||||||
3125 | involved are destroyed. | - | ||||||||||||
3126 | */ | - | ||||||||||||
3127 | - | |||||||||||||
3128 | - | |||||||||||||
3129 | /*! | - | ||||||||||||
3130 | \since 5.0 | - | ||||||||||||
3131 | - | |||||||||||||
3132 | This virtual function is called when something has been connected | - | ||||||||||||
3133 | to \a signal in this object. | - | ||||||||||||
3134 | - | |||||||||||||
3135 | If you want to compare \a signal with a specific signal, you can | - | ||||||||||||
3136 | use QMetaMethod::fromSignal() as follows: | - | ||||||||||||
3137 | - | |||||||||||||
3138 | \snippet code/src_corelib_kernel_qobject.cpp 32 | - | ||||||||||||
3139 | - | |||||||||||||
3140 | \warning This function violates the object-oriented principle of | - | ||||||||||||
3141 | modularity. However, it might be useful when you need to perform | - | ||||||||||||
3142 | expensive initialization only if something is connected to a | - | ||||||||||||
3143 | signal. | - | ||||||||||||
3144 | - | |||||||||||||
3145 | \warning This function is called from the thread which performs the | - | ||||||||||||
3146 | connection, which may be a different thread from the thread in | - | ||||||||||||
3147 | which this object lives. | - | ||||||||||||
3148 | - | |||||||||||||
3149 | \sa connect(), disconnectNotify() | - | ||||||||||||
3150 | */ | - | ||||||||||||
3151 | - | |||||||||||||
3152 | void QObject::connectNotify(const QMetaMethod &signal) | - | ||||||||||||
3153 | { | - | ||||||||||||
3154 | Q_UNUSED(signal); | - | ||||||||||||
3155 | } | - | ||||||||||||
3156 | - | |||||||||||||
3157 | /*! | - | ||||||||||||
3158 | \since 5.0 | - | ||||||||||||
3159 | - | |||||||||||||
3160 | This virtual function is called when something has been | - | ||||||||||||
3161 | disconnected from \a signal in this object. | - | ||||||||||||
3162 | - | |||||||||||||
3163 | See connectNotify() for an example of how to compare | - | ||||||||||||
3164 | \a signal with a specific signal. | - | ||||||||||||
3165 | - | |||||||||||||
3166 | If all signals were disconnected from this object (e.g., the | - | ||||||||||||
3167 | signal argument to disconnect() was 0), disconnectNotify() | - | ||||||||||||
3168 | is only called once, and the \a signal will be an invalid | - | ||||||||||||
3169 | QMetaMethod (QMetaMethod::isValid() returns \c false). | - | ||||||||||||
3170 | - | |||||||||||||
3171 | \warning This function violates the object-oriented principle of | - | ||||||||||||
3172 | modularity. However, it might be useful for optimizing access to | - | ||||||||||||
3173 | expensive resources. | - | ||||||||||||
3174 | - | |||||||||||||
3175 | \warning This function is called from the thread which performs the | - | ||||||||||||
3176 | disconnection, which may be a different thread from the thread in | - | ||||||||||||
3177 | which this object lives. This function may also be called with a QObject | - | ||||||||||||
3178 | internal mutex locked. It is therefore not allowed to re-enter any | - | ||||||||||||
3179 | of any QObject functions from your reimplementation and if you lock | - | ||||||||||||
3180 | a mutex in your reimplementation, make sure that you don't call QObject | - | ||||||||||||
3181 | functions with that mutex held in other places or it will result in | - | ||||||||||||
3182 | a deadlock. | - | ||||||||||||
3183 | - | |||||||||||||
3184 | \sa disconnect(), connectNotify() | - | ||||||||||||
3185 | */ | - | ||||||||||||
3186 | - | |||||||||||||
3187 | void QObject::disconnectNotify(const QMetaMethod &signal) | - | ||||||||||||
3188 | { | - | ||||||||||||
3189 | Q_UNUSED(signal); | - | ||||||||||||
3190 | } | - | ||||||||||||
3191 | - | |||||||||||||
3192 | /* | - | ||||||||||||
3193 | \internal | - | ||||||||||||
3194 | convert a signal index from the method range to the signal range | - | ||||||||||||
3195 | */ | - | ||||||||||||
3196 | static int methodIndexToSignalIndex(const QMetaObject **base, int signal_index) | - | ||||||||||||
3197 | { | - | ||||||||||||
3198 | if (signal_index < 0) | - | ||||||||||||
3199 | return signal_index; | - | ||||||||||||
3200 | const QMetaObject *metaObject = *base; | - | ||||||||||||
3201 | while (metaObject && metaObject->methodOffset() > signal_index) | - | ||||||||||||
3202 | metaObject = metaObject->superClass(); | - | ||||||||||||
3203 | - | |||||||||||||
3204 | if (metaObject) { | - | ||||||||||||
3205 | int signalOffset, methodOffset; | - | ||||||||||||
3206 | computeOffsets(metaObject, &signalOffset, &methodOffset); | - | ||||||||||||
3207 | if (signal_index < metaObject->methodCount()) | - | ||||||||||||
3208 | signal_index = QMetaObjectPrivate::originalClone(metaObject, signal_index - methodOffset) + signalOffset; | - | ||||||||||||
3209 | else | - | ||||||||||||
3210 | signal_index = signal_index - methodOffset + signalOffset; | - | ||||||||||||
3211 | *base = metaObject; | - | ||||||||||||
3212 | } | - | ||||||||||||
3213 | return signal_index; | - | ||||||||||||
3214 | } | - | ||||||||||||
3215 | - | |||||||||||||
3216 | /*! | - | ||||||||||||
3217 | \internal | - | ||||||||||||
3218 | \a types is a 0-terminated vector of meta types for queued | - | ||||||||||||
3219 | connections. | - | ||||||||||||
3220 | - | |||||||||||||
3221 | if \a signal_index is -1, then we effectively connect *all* signals | - | ||||||||||||
3222 | from the sender to the receiver's slot | - | ||||||||||||
3223 | */ | - | ||||||||||||
3224 | QMetaObject::Connection QMetaObject::connect(const QObject *sender, int signal_index, | - | ||||||||||||
3225 | const QObject *receiver, int method_index, int type, int *types) | - | ||||||||||||
3226 | { | - | ||||||||||||
3227 | const QMetaObject *smeta = sender->metaObject(); | - | ||||||||||||
3228 | signal_index = methodIndexToSignalIndex(&smeta, signal_index); | - | ||||||||||||
3229 | return Connection(QMetaObjectPrivate::connect(sender, signal_index, smeta, | - | ||||||||||||
3230 | receiver, method_index, | - | ||||||||||||
3231 | 0, //FIXME, we could speed this connection up by computing the relative index | - | ||||||||||||
3232 | type, types)); | - | ||||||||||||
3233 | } | - | ||||||||||||
3234 | - | |||||||||||||
3235 | /*! | - | ||||||||||||
3236 | \internal | - | ||||||||||||
3237 | Same as the QMetaObject::connect, but \a signal_index must be the result of QObjectPrivate::signalIndex | - | ||||||||||||
3238 | - | |||||||||||||
3239 | method_index is relative to the rmeta metaobject, if rmeta is null, then it is absolute index | - | ||||||||||||
3240 | - | |||||||||||||
3241 | the QObjectPrivate::Connection* has a refcount of 2, so it must be passed to a QMetaObject::Connection | - | ||||||||||||
3242 | */ | - | ||||||||||||
3243 | QObjectPrivate::Connection *QMetaObjectPrivate::connect(const QObject *sender, | - | ||||||||||||
3244 | int signal_index, const QMetaObject *smeta, | - | ||||||||||||
3245 | const QObject *receiver, int method_index, | - | ||||||||||||
3246 | const QMetaObject *rmeta, int type, int *types) | - | ||||||||||||
3247 | { | - | ||||||||||||
3248 | QObject *s = const_cast<QObject *>(sender); | - | ||||||||||||
3249 | QObject *r = const_cast<QObject *>(receiver); | - | ||||||||||||
3250 | - | |||||||||||||
3251 | int method_offset = rmeta ? rmeta->methodOffset() : 0; | - | ||||||||||||
3252 | Q_ASSERT(!rmeta || QMetaObjectPrivate::get(rmeta)->revision >= 6); | - | ||||||||||||
3253 | QObjectPrivate::StaticMetaCallFunction callFunction = | - | ||||||||||||
3254 | rmeta ? rmeta->d.static_metacall : 0; | - | ||||||||||||
3255 | - | |||||||||||||
3256 | QOrderedMutexLocker locker(signalSlotLock(sender), | - | ||||||||||||
3257 | signalSlotLock(receiver)); | - | ||||||||||||
3258 | - | |||||||||||||
3259 | if (type & Qt::UniqueConnection) { | - | ||||||||||||
3260 | QObjectConnectionListVector *connectionLists = QObjectPrivate::get(s)->connectionLists; | - | ||||||||||||
3261 | if (connectionLists && connectionLists->count() > signal_index) { | - | ||||||||||||
3262 | const QObjectPrivate::Connection *c2 = | - | ||||||||||||
3263 | (*connectionLists)[signal_index].first; | - | ||||||||||||
3264 | - | |||||||||||||
3265 | int method_index_absolute = method_index + method_offset; | - | ||||||||||||
3266 | - | |||||||||||||
3267 | while (c2) { | - | ||||||||||||
3268 | if (!c2->isSlotObject && c2->receiver == receiver && c2->method() == method_index_absolute) | - | ||||||||||||
3269 | return 0; | - | ||||||||||||
3270 | c2 = c2->nextConnectionList; | - | ||||||||||||
3271 | } | - | ||||||||||||
3272 | } | - | ||||||||||||
3273 | type &= Qt::UniqueConnection - 1; | - | ||||||||||||
3274 | } | - | ||||||||||||
3275 | - | |||||||||||||
3276 | QScopedPointer<QObjectPrivate::Connection> c(new QObjectPrivate::Connection); | - | ||||||||||||
3277 | c->sender = s; | - | ||||||||||||
3278 | c->signal_index = signal_index; | - | ||||||||||||
3279 | c->receiver = r; | - | ||||||||||||
3280 | c->method_relative = method_index; | - | ||||||||||||
3281 | c->method_offset = method_offset; | - | ||||||||||||
3282 | c->connectionType = type; | - | ||||||||||||
3283 | c->isSlotObject = false; | - | ||||||||||||
3284 | c->argumentTypes.store(types); | - | ||||||||||||
3285 | c->nextConnectionList = 0; | - | ||||||||||||
3286 | c->callFunction = callFunction; | - | ||||||||||||
3287 | - | |||||||||||||
3288 | QObjectPrivate::get(s)->addConnection(signal_index, c.data()); | - | ||||||||||||
3289 | - | |||||||||||||
3290 | locker.unlock(); | - | ||||||||||||
3291 | QMetaMethod smethod = QMetaObjectPrivate::signal(smeta, signal_index); | - | ||||||||||||
3292 | if (smethod.isValid()) | - | ||||||||||||
3293 | s->connectNotify(smethod); | - | ||||||||||||
3294 | - | |||||||||||||
3295 | return c.take(); | - | ||||||||||||
3296 | } | - | ||||||||||||
3297 | - | |||||||||||||
3298 | /*! | - | ||||||||||||
3299 | \internal | - | ||||||||||||
3300 | */ | - | ||||||||||||
3301 | bool QMetaObject::disconnect(const QObject *sender, int signal_index, | - | ||||||||||||
3302 | const QObject *receiver, int method_index) | - | ||||||||||||
3303 | { | - | ||||||||||||
3304 | const QMetaObject *smeta = sender->metaObject(); | - | ||||||||||||
3305 | signal_index = methodIndexToSignalIndex(&smeta, signal_index); | - | ||||||||||||
3306 | return QMetaObjectPrivate::disconnect(sender, signal_index, smeta, | - | ||||||||||||
3307 | receiver, method_index, 0); | - | ||||||||||||
3308 | } | - | ||||||||||||
3309 | - | |||||||||||||
3310 | /*! | - | ||||||||||||
3311 | \internal | - | ||||||||||||
3312 | - | |||||||||||||
3313 | Disconnect a single signal connection. If QMetaObject::connect() has been called | - | ||||||||||||
3314 | multiple times for the same sender, signal_index, receiver and method_index only | - | ||||||||||||
3315 | one of these connections will be removed. | - | ||||||||||||
3316 | */ | - | ||||||||||||
3317 | bool QMetaObject::disconnectOne(const QObject *sender, int signal_index, | - | ||||||||||||
3318 | const QObject *receiver, int method_index) | - | ||||||||||||
3319 | { | - | ||||||||||||
3320 | const QMetaObject *smeta = sender->metaObject(); | - | ||||||||||||
3321 | signal_index = methodIndexToSignalIndex(&smeta, signal_index); | - | ||||||||||||
3322 | return QMetaObjectPrivate::disconnect(sender, signal_index, smeta, | - | ||||||||||||
3323 | receiver, method_index, 0, | - | ||||||||||||
3324 | QMetaObjectPrivate::DisconnectOne); | - | ||||||||||||
3325 | } | - | ||||||||||||
3326 | - | |||||||||||||
3327 | /*! | - | ||||||||||||
3328 | \internal | - | ||||||||||||
3329 | Helper function to remove the connection from the senders list and setting the receivers to 0 | - | ||||||||||||
3330 | */ | - | ||||||||||||
3331 | bool QMetaObjectPrivate::disconnectHelper(QObjectPrivate::Connection *c, | - | ||||||||||||
3332 | const QObject *receiver, int method_index, void **slot, | - | ||||||||||||
3333 | QMutex *senderMutex, DisconnectType disconnectType) | - | ||||||||||||
3334 | { | - | ||||||||||||
3335 | bool success = false; | - | ||||||||||||
3336 | while (c) { | - | ||||||||||||
3337 | if (c->receiver | - | ||||||||||||
3338 | && (receiver == 0 || (c->receiver == receiver | - | ||||||||||||
3339 | && (method_index < 0 || (!c->isSlotObject && c->method() == method_index)) | - | ||||||||||||
3340 | && (slot == 0 || (c->isSlotObject && c->slotObj->compare(slot)))))) { | - | ||||||||||||
3341 | bool needToUnlock = false; | - | ||||||||||||
3342 | QMutex *receiverMutex = 0; | - | ||||||||||||
3343 | if (c->receiver) { | - | ||||||||||||
3344 | receiverMutex = signalSlotLock(c->receiver); | - | ||||||||||||
3345 | // need to relock this receiver and sender in the correct order | - | ||||||||||||
3346 | needToUnlock = QOrderedMutexLocker::relock(senderMutex, receiverMutex); | - | ||||||||||||
3347 | } | - | ||||||||||||
3348 | if (c->receiver) { | - | ||||||||||||
3349 | *c->prev = c->next; | - | ||||||||||||
3350 | if (c->next) | - | ||||||||||||
3351 | c->next->prev = c->prev; | - | ||||||||||||
3352 | } | - | ||||||||||||
3353 | - | |||||||||||||
3354 | if (needToUnlock) | - | ||||||||||||
3355 | receiverMutex->unlock(); | - | ||||||||||||
3356 | - | |||||||||||||
3357 | c->receiver = 0; | - | ||||||||||||
3358 | - | |||||||||||||
3359 | if (c->isSlotObject) { | - | ||||||||||||
3360 | c->isSlotObject = false; | - | ||||||||||||
3361 | senderMutex->unlock(); | - | ||||||||||||
3362 | c->slotObj->destroyIfLastRef(); | - | ||||||||||||
3363 | senderMutex->lock(); | - | ||||||||||||
3364 | } | - | ||||||||||||
3365 | - | |||||||||||||
3366 | success = true; | - | ||||||||||||
3367 | - | |||||||||||||
3368 | if (disconnectType == DisconnectOne) | - | ||||||||||||
3369 | return success; | - | ||||||||||||
3370 | } | - | ||||||||||||
3371 | c = c->nextConnectionList; | - | ||||||||||||
3372 | } | - | ||||||||||||
3373 | return success; | - | ||||||||||||
3374 | } | - | ||||||||||||
3375 | - | |||||||||||||
3376 | /*! | - | ||||||||||||
3377 | \internal | - | ||||||||||||
3378 | Same as the QMetaObject::disconnect, but \a signal_index must be the result of QObjectPrivate::signalIndex | - | ||||||||||||
3379 | */ | - | ||||||||||||
3380 | bool QMetaObjectPrivate::disconnect(const QObject *sender, | - | ||||||||||||
3381 | int signal_index, const QMetaObject *smeta, | - | ||||||||||||
3382 | const QObject *receiver, int method_index, void **slot, | - | ||||||||||||
3383 | DisconnectType disconnectType) | - | ||||||||||||
3384 | { | - | ||||||||||||
3385 | if (!sender) | - | ||||||||||||
3386 | return false; | - | ||||||||||||
3387 | - | |||||||||||||
3388 | QObject *s = const_cast<QObject *>(sender); | - | ||||||||||||
3389 | - | |||||||||||||
3390 | QMutex *senderMutex = signalSlotLock(sender); | - | ||||||||||||
3391 | QMutexLocker locker(senderMutex); | - | ||||||||||||
3392 | - | |||||||||||||
3393 | QObjectConnectionListVector *connectionLists = QObjectPrivate::get(s)->connectionLists; | - | ||||||||||||
3394 | if (!connectionLists) | - | ||||||||||||
3395 | return false; | - | ||||||||||||
3396 | - | |||||||||||||
3397 | // prevent incoming connections changing the connectionLists while unlocked | - | ||||||||||||
3398 | ++connectionLists->inUse; | - | ||||||||||||
3399 | - | |||||||||||||
3400 | bool success = false; | - | ||||||||||||
3401 | if (signal_index < 0) { | - | ||||||||||||
3402 | // remove from all connection lists | - | ||||||||||||
3403 | for (int sig_index = -1; sig_index < connectionLists->count(); ++sig_index) { | - | ||||||||||||
3404 | QObjectPrivate::Connection *c = | - | ||||||||||||
3405 | (*connectionLists)[sig_index].first; | - | ||||||||||||
3406 | if (disconnectHelper(c, receiver, method_index, slot, senderMutex, disconnectType)) { | - | ||||||||||||
3407 | success = true; | - | ||||||||||||
3408 | connectionLists->dirty = true; | - | ||||||||||||
3409 | } | - | ||||||||||||
3410 | } | - | ||||||||||||
3411 | } else if (signal_index < connectionLists->count()) { | - | ||||||||||||
3412 | QObjectPrivate::Connection *c = | - | ||||||||||||
3413 | (*connectionLists)[signal_index].first; | - | ||||||||||||
3414 | if (disconnectHelper(c, receiver, method_index, slot, senderMutex, disconnectType)) { | - | ||||||||||||
3415 | success = true; | - | ||||||||||||
3416 | connectionLists->dirty = true; | - | ||||||||||||
3417 | } | - | ||||||||||||
3418 | } | - | ||||||||||||
3419 | - | |||||||||||||
3420 | --connectionLists->inUse; | - | ||||||||||||
3421 | Q_ASSERT(connectionLists->inUse >= 0); | - | ||||||||||||
3422 | if (connectionLists->orphaned && !connectionLists->inUse) | - | ||||||||||||
3423 | delete connectionLists; | - | ||||||||||||
3424 | - | |||||||||||||
3425 | locker.unlock(); | - | ||||||||||||
3426 | if (success) { | - | ||||||||||||
3427 | QMetaMethod smethod = QMetaObjectPrivate::signal(smeta, signal_index); | - | ||||||||||||
3428 | if (smethod.isValid()) | - | ||||||||||||
3429 | s->disconnectNotify(smethod); | - | ||||||||||||
3430 | } | - | ||||||||||||
3431 | - | |||||||||||||
3432 | return success; | - | ||||||||||||
3433 | } | - | ||||||||||||
3434 | - | |||||||||||||
3435 | /*! | - | ||||||||||||
3436 | \fn void QMetaObject::connectSlotsByName(QObject *object) | - | ||||||||||||
3437 | - | |||||||||||||
3438 | Searches recursively for all child objects of the given \a object, and connects | - | ||||||||||||
3439 | matching signals from them to slots of \a object that follow the following form: | - | ||||||||||||
3440 | - | |||||||||||||
3441 | \snippet code/src_corelib_kernel_qobject.cpp 33 | - | ||||||||||||
3442 | - | |||||||||||||
3443 | Let's assume our object has a child object of type \c{QPushButton} with | - | ||||||||||||
3444 | the \l{QObject::objectName}{object name} \c{button1}. The slot to catch the | - | ||||||||||||
3445 | button's \c{clicked()} signal would be: | - | ||||||||||||
3446 | - | |||||||||||||
3447 | \snippet code/src_corelib_kernel_qobject.cpp 34 | - | ||||||||||||
3448 | - | |||||||||||||
3449 | If \a object itself has a properly set object name, its own signals are also | - | ||||||||||||
3450 | connected to its respective slots. | - | ||||||||||||
3451 | - | |||||||||||||
3452 | \sa QObject::setObjectName() | - | ||||||||||||
3453 | */ | - | ||||||||||||
3454 | void QMetaObject::connectSlotsByName(QObject *o) | - | ||||||||||||
3455 | { | - | ||||||||||||
3456 | if (!o) | - | ||||||||||||
3457 | return; | - | ||||||||||||
3458 | const QMetaObject *mo = o->metaObject(); | - | ||||||||||||
3459 | Q_ASSERT(mo); | - | ||||||||||||
3460 | const QObjectList list = // list of all objects to look for matching signals including... | - | ||||||||||||
3461 | o->findChildren<QObject *>(QString()) // all children of 'o'... | - | ||||||||||||
3462 | << o; // and the object 'o' itself | - | ||||||||||||
3463 | - | |||||||||||||
3464 | // for each method/slot of o ... | - | ||||||||||||
3465 | for (int i = 0; i < mo->methodCount(); ++i) { | - | ||||||||||||
3466 | const QByteArray slotSignature = mo->method(i).methodSignature(); | - | ||||||||||||
3467 | const char *slot = slotSignature.constData(); | - | ||||||||||||
3468 | Q_ASSERT(slot); | - | ||||||||||||
3469 | - | |||||||||||||
3470 | // ...that starts with "on_", ... | - | ||||||||||||
3471 | if (slot[0] != 'o' || slot[1] != 'n' || slot[2] != '_') | - | ||||||||||||
3472 | continue; | - | ||||||||||||
3473 | - | |||||||||||||
3474 | // ...we check each object in our list, ... | - | ||||||||||||
3475 | bool foundIt = false; | - | ||||||||||||
3476 | for(int j = 0; j < list.count(); ++j) { | - | ||||||||||||
3477 | const QObject *co = list.at(j); | - | ||||||||||||
3478 | const QByteArray coName = co->objectName().toLatin1(); | - | ||||||||||||
3479 | - | |||||||||||||
3480 | // ...discarding those whose objectName is not fitting the pattern "on_<objectName>_...", ... | - | ||||||||||||
3481 | if (coName.isEmpty() || qstrncmp(slot + 3, coName.constData(), coName.size()) || slot[coName.size()+3] != '_') | - | ||||||||||||
3482 | continue; | - | ||||||||||||
3483 | - | |||||||||||||
3484 | const char *signal = slot + coName.size() + 4; // the 'signal' part of the slot name | - | ||||||||||||
3485 | - | |||||||||||||
3486 | // ...for the presence of a matching signal "on_<objectName>_<signal>". | - | ||||||||||||
3487 | const QMetaObject *smeta; | - | ||||||||||||
3488 | int sigIndex = co->d_func()->signalIndex(signal, &smeta); | - | ||||||||||||
3489 | if (sigIndex < 0) { | - | ||||||||||||
3490 | // if no exactly fitting signal (name + complete parameter type list) could be found | - | ||||||||||||
3491 | // look for just any signal with the correct name and at least the slot's parameter list. | - | ||||||||||||
3492 | // Note: if more than one of thoses signals exist, the one that gets connected is | - | ||||||||||||
3493 | // chosen 'at random' (order of declaration in source file) | - | ||||||||||||
3494 | QList<QByteArray> compatibleSignals; | - | ||||||||||||
3495 | const QMetaObject *smo = co->metaObject(); | - | ||||||||||||
3496 | int sigLen = qstrlen(signal) - 1; // ignore the trailing ')' | - | ||||||||||||
3497 | for (int k = QMetaObjectPrivate::absoluteSignalCount(smo)-1; k >= 0; --k) { | - | ||||||||||||
3498 | const QMetaMethod method = QMetaObjectPrivate::signal(smo, k); | - | ||||||||||||
3499 | if (!qstrncmp(method.methodSignature().constData(), signal, sigLen)) { | - | ||||||||||||
3500 | smeta = method.enclosingMetaObject(); | - | ||||||||||||
3501 | sigIndex = k; | - | ||||||||||||
3502 | compatibleSignals.prepend(method.methodSignature()); | - | ||||||||||||
3503 | } | - | ||||||||||||
3504 | } | - | ||||||||||||
3505 | if (compatibleSignals.size() > 1) | - | ||||||||||||
3506 | qWarning() << "QMetaObject::connectSlotsByName: Connecting slot" << slot | - | ||||||||||||
3507 | << "with the first of the following compatible signals:" << compatibleSignals; | - | ||||||||||||
3508 | } | - | ||||||||||||
3509 | - | |||||||||||||
3510 | if (sigIndex < 0) | - | ||||||||||||
3511 | continue; | - | ||||||||||||
3512 | - | |||||||||||||
3513 | // we connect it... | - | ||||||||||||
3514 | if (Connection(QMetaObjectPrivate::connect(co, sigIndex, smeta, o, i))) { | - | ||||||||||||
3515 | foundIt = true; | - | ||||||||||||
3516 | // ...and stop looking for further objects with the same name. | - | ||||||||||||
3517 | // Note: the Designer will make sure each object name is unique in the above | - | ||||||||||||
3518 | // 'list' but other code may create two child objects with the same name. In | - | ||||||||||||
3519 | // this case one is chosen 'at random'. | - | ||||||||||||
3520 | break; | - | ||||||||||||
3521 | } | - | ||||||||||||
3522 | } | - | ||||||||||||
3523 | if (foundIt) { | - | ||||||||||||
3524 | // we found our slot, now skip all overloads | - | ||||||||||||
3525 | while (mo->method(i + 1).attributes() & QMetaMethod::Cloned) | - | ||||||||||||
3526 | ++i; | - | ||||||||||||
3527 | } else if (!(mo->method(i).attributes() & QMetaMethod::Cloned)) { | - | ||||||||||||
3528 | // check if the slot has the following signature: "on_..._...(..." | - | ||||||||||||
3529 | int iParen = slotSignature.indexOf('('); | - | ||||||||||||
3530 | int iLastUnderscore = slotSignature.lastIndexOf('_', iParen-1); | - | ||||||||||||
3531 | if (iLastUnderscore > 3) | - | ||||||||||||
3532 | qWarning("QMetaObject::connectSlotsByName: No matching signal for %s", slot); | - | ||||||||||||
3533 | } | - | ||||||||||||
3534 | } | - | ||||||||||||
3535 | } | - | ||||||||||||
3536 | - | |||||||||||||
3537 | /*! | - | ||||||||||||
3538 | \internal | - | ||||||||||||
3539 | - | |||||||||||||
3540 | \a signal must be in the signal index range (see QObjectPrivate::signalIndex()). | - | ||||||||||||
3541 | */ | - | ||||||||||||
3542 | static void queued_activate(QObject *sender, int signal, QObjectPrivate::Connection *c, void **argv, | - | ||||||||||||
3543 | QMutexLocker &locker) | - | ||||||||||||
3544 | { | - | ||||||||||||
3545 | const int *argumentTypes = c->argumentTypes.load(); | - | ||||||||||||
3546 | if (!argumentTypes) { | - | ||||||||||||
3547 | QMetaMethod m = QMetaObjectPrivate::signal(sender->metaObject(), signal); | - | ||||||||||||
3548 | argumentTypes = queuedConnectionTypes(m.parameterTypes()); | - | ||||||||||||
3549 | if (!argumentTypes) // cannot queue arguments | - | ||||||||||||
3550 | argumentTypes = &DIRECT_CONNECTION_ONLY; | - | ||||||||||||
3551 | if (!c->argumentTypes.testAndSetOrdered(0, argumentTypes)) { | - | ||||||||||||
3552 | if (argumentTypes != &DIRECT_CONNECTION_ONLY) | - | ||||||||||||
3553 | delete [] argumentTypes; | - | ||||||||||||
3554 | argumentTypes = c->argumentTypes.load(); | - | ||||||||||||
3555 | } | - | ||||||||||||
3556 | } | - | ||||||||||||
3557 | if (argumentTypes == &DIRECT_CONNECTION_ONLY) // cannot activate | - | ||||||||||||
3558 | return; | - | ||||||||||||
3559 | int nargs = 1; // include return type | - | ||||||||||||
3560 | while (argumentTypes[nargs-1]) | - | ||||||||||||
3561 | ++nargs; | - | ||||||||||||
3562 | int *types = (int *) malloc(nargs*sizeof(int)); | - | ||||||||||||
3563 | Q_CHECK_PTR(types); | - | ||||||||||||
3564 | void **args = (void **) malloc(nargs*sizeof(void *)); | - | ||||||||||||
3565 | Q_CHECK_PTR(args); | - | ||||||||||||
3566 | types[0] = 0; // return type | - | ||||||||||||
3567 | args[0] = 0; // return value | - | ||||||||||||
3568 | - | |||||||||||||
3569 | if (nargs > 1) { | - | ||||||||||||
3570 | for (int n = 1; n < nargs; ++n) | - | ||||||||||||
3571 | types[n] = argumentTypes[n-1]; | - | ||||||||||||
3572 | - | |||||||||||||
3573 | locker.unlock(); | - | ||||||||||||
3574 | for (int n = 1; n < nargs; ++n) | - | ||||||||||||
3575 | args[n] = QMetaType::create(types[n], argv[n]); | - | ||||||||||||
3576 | locker.relock(); | - | ||||||||||||
3577 | - | |||||||||||||
3578 | if (!c->receiver) { | - | ||||||||||||
3579 | locker.unlock(); | - | ||||||||||||
3580 | // we have been disconnected while the mutex was unlocked | - | ||||||||||||
3581 | for (int n = 1; n < nargs; ++n) | - | ||||||||||||
3582 | QMetaType::destroy(types[n], args[n]); | - | ||||||||||||
3583 | free(types); | - | ||||||||||||
3584 | free(args); | - | ||||||||||||
3585 | locker.relock(); | - | ||||||||||||
3586 | return; | - | ||||||||||||
3587 | } | - | ||||||||||||
3588 | } | - | ||||||||||||
3589 | - | |||||||||||||
3590 | QMetaCallEvent *ev = c->isSlotObject ? | - | ||||||||||||
3591 | new QMetaCallEvent(c->slotObj, sender, signal, nargs, types, args) : | - | ||||||||||||
3592 | new QMetaCallEvent(c->method_offset, c->method_relative, c->callFunction, sender, signal, nargs, types, args); | - | ||||||||||||
3593 | QCoreApplication::postEvent(c->receiver, ev); | - | ||||||||||||
3594 | } | - | ||||||||||||
3595 | - | |||||||||||||
3596 | /*! | - | ||||||||||||
3597 | \internal | - | ||||||||||||
3598 | */ | - | ||||||||||||
3599 | void QMetaObject::activate(QObject *sender, const QMetaObject *m, int local_signal_index, | - | ||||||||||||
3600 | void **argv) | - | ||||||||||||
3601 | { | - | ||||||||||||
3602 | activate(sender, QMetaObjectPrivate::signalOffset(m), local_signal_index, argv); | - | ||||||||||||
3603 | } | - | ||||||||||||
3604 | - | |||||||||||||
3605 | /*! | - | ||||||||||||
3606 | \internal | - | ||||||||||||
3607 | */ | - | ||||||||||||
3608 | void QMetaObject::activate(QObject *sender, int signalOffset, int local_signal_index, void **argv) | - | ||||||||||||
3609 | { | - | ||||||||||||
3610 | int signal_index = signalOffset + local_signal_index; | - | ||||||||||||
if (!sender->d_func()->isSignalConnected(signal_index) | ||||||||||||||
&& !qt_signal_spy_callback_set.signal_begin_callback | ||||||||||||||
&& !qt_signal_spy_callback_set.signal_end_callback) { | ||||||||||||||
return; | ||||||||||||||
3611 | - | |||||||||||||
3612 | }if (sender->d_func()->blockSig)
| 48493-22007731 | ||||||||||||
3613 | return; executed 48493 times by 34 tests: return; Executed by:
| 48493 | ||||||||||||
3614 | - | |||||||||||||
3615 | if (sender->d_func()->declarativeDataisDeclarativeSignalConnected(signal_index)
| 0-22007733 | ||||||||||||
3616 | && QAbstractDeclarativeData::signalEmitted) {
| 0 | ||||||||||||
3617 | QAbstractDeclarativeData::signalEmitted(sender->d_func()->declarativeData, sender, | - | ||||||||||||
3618 | signal_index, argv); | - | ||||||||||||
3619 | } never executed: end of block | 0 | ||||||||||||
3620 | - | |||||||||||||
3621 | if (!sender->d_func()->isSignalConnected(signal_index,
| 7734830-14272902 | ||||||||||||
3622 | && !qt_signal_spy_callback_set.signal_begin_callback
| 0-14272902 | ||||||||||||
3623 | && !qt_signal_spy_callback_set.signal_end_callback) {
| 0-14272903 | ||||||||||||
3624 | // The possible declarative connection is done, and nothing else is connected, so: | - | ||||||||||||
3625 | executed 14272903 times by 1013 tests: return;return; Executed by:
executed 14272903 times by 1013 tests: return; Executed by:
| 14272903 | ||||||||||||
3626 | } | - | ||||||||||||
3627 | - | |||||||||||||
3628 | void *empty_argv[] = { 0 }; | - | ||||||||||||
3629 | if (qt_signal_spy_callback_set.signal_begin_callback != 0) {
| 0-7734830 | ||||||||||||
3630 | qt_signal_spy_callback_set.signal_begin_callback(sender, signal_index, | - | ||||||||||||
3631 | argv ? argv : empty_argv); | - | ||||||||||||
3632 | } never executed: end of block | 0 | ||||||||||||
3633 | - | |||||||||||||
3634 | Qt::HANDLE currentThreadId = QThread::currentThreadId();{ | - | ||||||||||||
3635 | QMutexLocker locker(signalSlotLock(sender)); | - | ||||||||||||
3636 | struct ConnectionListsRef { | - | ||||||||||||
3637 | QObjectConnectionListVector *connectionLists; | - | ||||||||||||
3638 | ConnectionListsRef(QObjectConnectionListVector *connectionLists) : connectionLists(connectionLists) | - | ||||||||||||
3639 | { | - | ||||||||||||
3640 | if (connectionLists) | - | ||||||||||||
3641 | ++connectionLists->inUse; | - | ||||||||||||
3642 | } | - | ||||||||||||
3643 | ~ConnectionListsRef() | - | ||||||||||||
3644 | { | - | ||||||||||||
3645 | if (!connectionLists) | - | ||||||||||||
3646 | return; | - | ||||||||||||
3647 | - | |||||||||||||
3648 | --connectionLists->inUse; | - | ||||||||||||
3649 | Q_ASSERT(connectionLists->inUse >= 0); | - | ||||||||||||
3650 | if (connectionLists->orphaned) { | - | ||||||||||||
3651 | if (!connectionLists->inUse) | - | ||||||||||||
3652 | delete connectionLists; | - | ||||||||||||
3653 | } | - | ||||||||||||
3654 | } | - | ||||||||||||
3655 | - | |||||||||||||
3656 | QObjectConnectionListVector *operator->() const { return connectionLists; } | - | ||||||||||||
3657 | }; | - | ||||||||||||
3658 | ConnectionListsRef connectionLists = sender->d_func()->connectionLists; | - | ||||||||||||
3659 | if (!connectionLists.connectionLists) {
| 0-7734830 | ||||||||||||
3660 | locker.unlock(); | - | ||||||||||||
3661 | if (qt_signal_spy_callback_set.signal_end_callback != 0)
| 0 | ||||||||||||
3662 | qt_signal_spy_callback_set.signal_end_callback(sender, signal_index); never executed: qt_signal_spy_callback_set.signal_end_callback(sender, signal_index); | 0 | ||||||||||||
3663 | return; never executed: return; | 0 | ||||||||||||
3664 | } | - | ||||||||||||
3665 | - | |||||||||||||
3666 | const QObjectPrivate::ConnectionList *list; | - | ||||||||||||
3667 | if (signal_index < connectionLists->count())
| 406-7734424 | ||||||||||||
3668 | list = &connectionLists->at(signal_index); executed 7734424 times by 507 tests: list = &connectionLists->at(signal_index); Executed by:
| 7734424 | ||||||||||||
3669 | else | - | ||||||||||||
3670 | list = &connectionLists->allsignals; executed 406 times by 9 tests: list = &connectionLists->allsignals; Executed by:
| 406 | ||||||||||||
3671 | - | |||||||||||||
3672 | Qt::HANDLE currentThreadId = QThread::currentThreadId(); | - | ||||||||||||
3673 | - | |||||||||||||
3674 | do { | - | ||||||||||||
3675 | QObjectPrivate::Connection *c = list->first; | - | ||||||||||||
3676 | if (!c) continue; executed 7734275 times by 507 tests: continue; Executed by:
| 7734275-7734721 | ||||||||||||
3677 | // We need to check against last here to ensure that signals added | - | ||||||||||||
3678 | // during the signal emission are not emitted in this emission. | - | ||||||||||||
3679 | QObjectPrivate::Connection *last = list->last; | - | ||||||||||||
3680 | - | |||||||||||||
3681 | do { | - | ||||||||||||
3682 | if (!c->receiver)
| 22510-8500750 | ||||||||||||
3683 | continue; executed 22510 times by 274 tests: continue; Executed by:
| 22510 | ||||||||||||
3684 | - | |||||||||||||
3685 | QObject * const receiver = c->receiver; | - | ||||||||||||
3686 | const bool receiverInSameThread = currentThreadId == receiver->d_func()->threadData->threadId; | - | ||||||||||||
3687 | - | |||||||||||||
3688 | // determine if this connection should be sent immediately or | - | ||||||||||||
3689 | // put into the event queue | - | ||||||||||||
3690 | if ((c->connectionType == Qt::AutoConnection && !receiverInSameThread)
| 13222-8270219 | ||||||||||||
3691 | || (c->connectionType == Qt::QueuedConnection)) {
| 146324-8341204 | ||||||||||||
3692 | queued_activate(sender, signal_index, c, argv ? argv : empty_argv, locker); | - | ||||||||||||
3693 | continue; executed 159546 times by 431 tests: continue; Executed by:
| 159546 | ||||||||||||
3694 | #ifndef QT_NO_THREAD | - | ||||||||||||
3695 | } else if (c->connectionType == Qt::BlockingQueuedConnection) {
| 4442-8336762 | ||||||||||||
3696 | if (receiverInSameThread) {
| 0-4442 | ||||||||||||
3697 | qWarning("Qt: Dead lock detected while activating a BlockingQueuedConnection: " | - | ||||||||||||
3698 | "Sender is %s(%p), receiver is %s(%p)", | - | ||||||||||||
3699 | sender->metaObject()->className(), sender, | - | ||||||||||||
3700 | receiver->metaObject()->className(), receiver); | - | ||||||||||||
3701 | } never executed: end of block | 0 | ||||||||||||
3702 | QSemaphore semaphore; | - | ||||||||||||
3703 | QMetaCallEvent *ev = c->isSlotObject ?
| 641-3801 | ||||||||||||
3704 | new QMetaCallEvent(c->slotObj, sender, signal_index, 0, 0, argv ? argv : empty_argv, &semaphore) : | - | ||||||||||||
3705 | new QMetaCallEvent(c->method_offset, c->method_relative, c->callFunction, sender, signal_index, 0, 0, argv ? argv : empty_argv, &semaphore); | - | ||||||||||||
3706 | QCoreApplication::postEvent(receiver, ev); | - | ||||||||||||
3707 | locker.unlock(); | - | ||||||||||||
3708 | semaphore.acquire(); | - | ||||||||||||
3709 | locker.relock(); | - | ||||||||||||
3710 | continue; executed 4442 times by 315 tests: continue; Executed by:
| 4442 | ||||||||||||
3711 | #endif | - | ||||||||||||
3712 | } | - | ||||||||||||
3713 | - | |||||||||||||
3714 | QConnectionSenderSwitcher sw; | - | ||||||||||||
3715 | - | |||||||||||||
3716 | if (receiverInSameThread) {
| 507-8336255 | ||||||||||||
3717 | sw.switchSender(receiver, sender, signal_index); | - | ||||||||||||
3718 | } executed 8336255 times by 381 tests: end of block Executed by:
| 8336255 | ||||||||||||
3719 | const QObjectPrivate::StaticMetaCallFunction callFunction = c->callFunction;
| 28931-8307831 | ||||||||||||
const int method_relative = c->method_relative;
| ||||||||||||||
3720 | c->slotObj->ref(); | - | ||||||||||||
3721 | QScopedPointer<QtPrivate::QSlotObjectBase, QSlotObjectBaseDeleter> obj(c->slotObj); | - | ||||||||||||
3722 | locker.unlock(); | - | ||||||||||||
3723 | obj->call(receiver, argv ? argv : empty_argv); | - | ||||||||||||
3724 | - | |||||||||||||
3725 | // Make sure the slot object gets destroyed before the mutex is locked again, as the | - | ||||||||||||
3726 | // destructor of the slot object might also lock a mutex from the signalSlotLock() mutex pool, | - | ||||||||||||
3727 | // and that would deadlock if the pool happens to return the same mutex. | - | ||||||||||||
3728 | obj.reset(); | - | ||||||||||||
3729 | - | |||||||||||||
3730 | locker.relock(); | - | ||||||||||||
3731 | } else if (c->callFunction && c->method_offset <= receiver->metaObject()->methodOffset()) { executed 28929 times by 301 tests: end of block Executed by:
| 3325-8293985 | ||||||||||||
3732 | //we compare the vtable to make sure we are not in the destructor of the object. | - | ||||||||||||
3733 | locker.unlock();const int methodIndex = c->method(); | - | ||||||||||||
3734 | const int method_relative = c->method_relative; | - | ||||||||||||
3735 | const auto callFunction = c->callFunction; | - | ||||||||||||
3736 | locker.unlock(); | - | ||||||||||||
3737 | if (qt_signal_spy_callback_set.slot_begin_callback != 0)
| 0-8290660 | ||||||||||||
3738 | qt_signal_spy_callback_set.slot_begin_callback(receiver, methodIndex, argv ? argv : empty_argv); never executed: qt_signal_spy_callback_set.slot_begin_callback(receiver, methodIndex, argv ? argv : empty_argv); | 0 | ||||||||||||
3739 | - | |||||||||||||
3740 | callFunction(receiver, QMetaObject::InvokeMetaMethod, method_relative, argv ? argv : empty_argv); | - | ||||||||||||
3741 | - | |||||||||||||
3742 | if (qt_signal_spy_callback_set.slot_end_callback != 0)
| 0-8290657 | ||||||||||||
3743 | qt_signal_spy_callback_set.slot_end_callback(receiver, methodIndex); never executed: qt_signal_spy_callback_set.slot_end_callback(receiver, methodIndex); | 0 | ||||||||||||
3744 | locker.relock(); | - | ||||||||||||
3745 | } else { executed 8290657 times by 229 tests: end of block Executed by:
| 8290657 | ||||||||||||
3746 | const int method = c->method_relative + c->method_offset; | - | ||||||||||||
3747 | locker.unlock(); | - | ||||||||||||
3748 | - | |||||||||||||
3749 | if (qt_signal_spy_callback_set.slot_begin_callback != 0) {
| 0-17171 | ||||||||||||
3750 | qt_signal_spy_callback_set.slot_begin_callback(receiver, | - | ||||||||||||
3751 | method, | - | ||||||||||||
3752 | argv ? argv : empty_argv); | - | ||||||||||||
3753 | } never executed: end of block | 0 | ||||||||||||
3754 | - | |||||||||||||
3755 | metacall(receiver, QMetaObject::InvokeMetaMethod, method, argv ? argv : empty_argv); | - | ||||||||||||
3756 | - | |||||||||||||
3757 | if (qt_signal_spy_callback_set.slot_end_callback != 0)
| 0-17171 | ||||||||||||
3758 | qt_signal_spy_callback_set.slot_end_callback(receiver, method); never executed: qt_signal_spy_callback_set.slot_end_callback(receiver, method); | 0 | ||||||||||||
3759 | - | |||||||||||||
3760 | locker.relock(); | - | ||||||||||||
3761 | } executed 17171 times by 148 tests: end of block Executed by:
| 17171 | ||||||||||||
3762 | - | |||||||||||||
3763 | if (connectionLists->orphaned)
| 252-8336505 | ||||||||||||
3764 | break; executed 252 times by 15 tests: break; Executed by:
| 252 | ||||||||||||
3765 | } while (c != last && (c = c->nextConnectionList) != 0); executed 8336505 times by 381 tests: end of block Executed by:
| 0-8336505 | ||||||||||||
3766 | - | |||||||||||||
3767 | if (connectionLists->orphaned)
| 253-7734463 | ||||||||||||
3768 | break; executed 253 times by 15 tests: break; Executed by:
| 253 | ||||||||||||
3769 | } while (list != &connectionLists->allsignals && executed 7734463 times by 507 tests: end of block Executed by:
| 7734166-7734572 | ||||||||||||
3770 | //start over for all signals; | - | ||||||||||||
3771 | ((list = &connectionLists->allsignals), true)); | - | ||||||||||||
3772 | - | |||||||||||||
3773 | } | - | ||||||||||||
3774 | - | |||||||||||||
3775 | if (qt_signal_spy_callback_set.signal_end_callback != 0)
| 0-7734825 | ||||||||||||
3776 | qt_signal_spy_callback_set.signal_end_callback(sender, signal_index); never executed: qt_signal_spy_callback_set.signal_end_callback(sender, signal_index); | 0 | ||||||||||||
3777 | - | |||||||||||||
3778 | } executed 7734825 times by 507 tests: end of block Executed by:
| 7734825 | ||||||||||||
3779 | - | |||||||||||||
3780 | /*! | - | ||||||||||||
3781 | \internal | - | ||||||||||||
3782 | signal_index comes from indexOfMethod() | - | ||||||||||||
3783 | */ | - | ||||||||||||
3784 | void QMetaObject::activate(QObject *sender, int signal_index, void **argv) | - | ||||||||||||
3785 | { | - | ||||||||||||
3786 | const QMetaObject *mo = sender->metaObject(); | - | ||||||||||||
3787 | while (mo->methodOffset() > signal_index) | - | ||||||||||||
3788 | mo = mo->superClass(); | - | ||||||||||||
3789 | activate(sender, mo, signal_index - mo->methodOffset(), argv); | - | ||||||||||||
3790 | } | - | ||||||||||||
3791 | - | |||||||||||||
3792 | /*! | - | ||||||||||||
3793 | \internal | - | ||||||||||||
3794 | Returns the signal index used in the internal connectionLists vector. | - | ||||||||||||
3795 | - | |||||||||||||
3796 | It is different from QMetaObject::indexOfSignal(): indexOfSignal is the same as indexOfMethod | - | ||||||||||||
3797 | while QObjectPrivate::signalIndex is smaller because it doesn't give index to slots. | - | ||||||||||||
3798 | - | |||||||||||||
3799 | If \a meta is not 0, it is set to the meta-object where the signal was found. | - | ||||||||||||
3800 | */ | - | ||||||||||||
3801 | int QObjectPrivate::signalIndex(const char *signalName, | - | ||||||||||||
3802 | const QMetaObject **meta) const | - | ||||||||||||
3803 | { | - | ||||||||||||
3804 | Q_Q(const QObject); | - | ||||||||||||
3805 | const QMetaObject *base = q->metaObject(); | - | ||||||||||||
3806 | Q_ASSERT(QMetaObjectPrivate::get(base)->revision >= 7); | - | ||||||||||||
3807 | QArgumentTypeArray types; | - | ||||||||||||
3808 | QByteArray name = QMetaObjectPrivate::decodeMethodSignature(signalName, types); | - | ||||||||||||
3809 | int relative_index = QMetaObjectPrivate::indexOfSignalRelative( | - | ||||||||||||
3810 | &base, name, types.size(), types.constData()); | - | ||||||||||||
3811 | if (relative_index < 0) | - | ||||||||||||
3812 | return relative_index; | - | ||||||||||||
3813 | relative_index = QMetaObjectPrivate::originalClone(base, relative_index); | - | ||||||||||||
3814 | if (meta) | - | ||||||||||||
3815 | *meta = base; | - | ||||||||||||
3816 | return relative_index + QMetaObjectPrivate::signalOffset(base); | - | ||||||||||||
3817 | } | - | ||||||||||||
3818 | - | |||||||||||||
3819 | /***************************************************************************** | - | ||||||||||||
3820 | Properties | - | ||||||||||||
3821 | *****************************************************************************/ | - | ||||||||||||
3822 | - | |||||||||||||
3823 | #ifndef QT_NO_PROPERTIES | - | ||||||||||||
3824 | - | |||||||||||||
3825 | /*! | - | ||||||||||||
3826 | Sets the value of the object's \a name property to \a value. | - | ||||||||||||
3827 | - | |||||||||||||
3828 | If the property is defined in the class using Q_PROPERTY then | - | ||||||||||||
3829 | true is returned on success and false otherwise. If the property | - | ||||||||||||
3830 | is not defined using Q_PROPERTY, and therefore not listed in the | - | ||||||||||||
3831 | meta-object, it is added as a dynamic property and false is returned. | - | ||||||||||||
3832 | - | |||||||||||||
3833 | Information about all available properties is provided through the | - | ||||||||||||
3834 | metaObject() and dynamicPropertyNames(). | - | ||||||||||||
3835 | - | |||||||||||||
3836 | Dynamic properties can be queried again using property() and can be | - | ||||||||||||
3837 | removed by setting the property value to an invalid QVariant. | - | ||||||||||||
3838 | Changing the value of a dynamic property causes a QDynamicPropertyChangeEvent | - | ||||||||||||
3839 | to be sent to the object. | - | ||||||||||||
3840 | - | |||||||||||||
3841 | \b{Note:} Dynamic properties starting with "_q_" are reserved for internal | - | ||||||||||||
3842 | purposes. | - | ||||||||||||
3843 | - | |||||||||||||
3844 | \sa property(), metaObject(), dynamicPropertyNames(), QMetaProperty::write() | - | ||||||||||||
3845 | */ | - | ||||||||||||
3846 | bool QObject::setProperty(const char *name, const QVariant &value) | - | ||||||||||||
3847 | { | - | ||||||||||||
3848 | Q_D(QObject); | - | ||||||||||||
3849 | const QMetaObject* meta = metaObject(); | - | ||||||||||||
3850 | if (!name || !meta) | - | ||||||||||||
3851 | return false; | - | ||||||||||||
3852 | - | |||||||||||||
3853 | int id = meta->indexOfProperty(name); | - | ||||||||||||
3854 | if (id < 0) { | - | ||||||||||||
3855 | if (!d->extraData) | - | ||||||||||||
3856 | d->extraData = new QObjectPrivate::ExtraData; | - | ||||||||||||
3857 | - | |||||||||||||
3858 | const int idx = d->extraData->propertyNames.indexOf(name); | - | ||||||||||||
3859 | - | |||||||||||||
3860 | if (!value.isValid()) { | - | ||||||||||||
3861 | if (idx == -1) | - | ||||||||||||
3862 | return false; | - | ||||||||||||
3863 | d->extraData->propertyNames.removeAt(idx); | - | ||||||||||||
3864 | d->extraData->propertyValues.removeAt(idx); | - | ||||||||||||
3865 | } else { | - | ||||||||||||
3866 | if (idx == -1) { | - | ||||||||||||
3867 | d->extraData->propertyNames.append(name); | - | ||||||||||||
3868 | d->extraData->propertyValues.append(value); | - | ||||||||||||
3869 | } else { | - | ||||||||||||
3870 | if (value == d->extraData->propertyValues.at(idx)) | - | ||||||||||||
3871 | return false; | - | ||||||||||||
3872 | d->extraData->propertyValues[idx] = value; | - | ||||||||||||
3873 | } | - | ||||||||||||
3874 | } | - | ||||||||||||
3875 | - | |||||||||||||
3876 | QDynamicPropertyChangeEvent ev(name); | - | ||||||||||||
3877 | QCoreApplication::sendEvent(this, &ev); | - | ||||||||||||
3878 | - | |||||||||||||
3879 | return false; | - | ||||||||||||
3880 | } | - | ||||||||||||
3881 | QMetaProperty p = meta->property(id); | - | ||||||||||||
3882 | #ifndef QT_NO_DEBUG | - | ||||||||||||
3883 | if (!p.isWritable()) | - | ||||||||||||
3884 | qWarning("%s::setProperty: Property \"%s\" invalid," | - | ||||||||||||
3885 | " read-only or does not exist", metaObject()->className(), name); | - | ||||||||||||
3886 | #endif | - | ||||||||||||
3887 | return p.write(this, value); | - | ||||||||||||
3888 | } | - | ||||||||||||
3889 | - | |||||||||||||
3890 | /*! | - | ||||||||||||
3891 | Returns the value of the object's \a name property. | - | ||||||||||||
3892 | - | |||||||||||||
3893 | If no such property exists, the returned variant is invalid. | - | ||||||||||||
3894 | - | |||||||||||||
3895 | Information about all available properties is provided through the | - | ||||||||||||
3896 | metaObject() and dynamicPropertyNames(). | - | ||||||||||||
3897 | - | |||||||||||||
3898 | \sa setProperty(), QVariant::isValid(), metaObject(), dynamicPropertyNames() | - | ||||||||||||
3899 | */ | - | ||||||||||||
3900 | QVariant QObject::property(const char *name) const | - | ||||||||||||
3901 | { | - | ||||||||||||
3902 | Q_D(const QObject); | - | ||||||||||||
3903 | const QMetaObject* meta = metaObject(); | - | ||||||||||||
3904 | if (!name || !meta) | - | ||||||||||||
3905 | return QVariant(); | - | ||||||||||||
3906 | - | |||||||||||||
3907 | int id = meta->indexOfProperty(name); | - | ||||||||||||
3908 | if (id < 0) { | - | ||||||||||||
3909 | if (!d->extraData) | - | ||||||||||||
3910 | return QVariant(); | - | ||||||||||||
3911 | const int i = d->extraData->propertyNames.indexOf(name); | - | ||||||||||||
3912 | return d->extraData->propertyValues.value(i); | - | ||||||||||||
3913 | } | - | ||||||||||||
3914 | QMetaProperty p = meta->property(id); | - | ||||||||||||
3915 | #ifndef QT_NO_DEBUG | - | ||||||||||||
3916 | if (!p.isReadable()) | - | ||||||||||||
3917 | qWarning("%s::property: Property \"%s\" invalid or does not exist", | - | ||||||||||||
3918 | metaObject()->className(), name); | - | ||||||||||||
3919 | #endif | - | ||||||||||||
3920 | return p.read(this); | - | ||||||||||||
3921 | } | - | ||||||||||||
3922 | - | |||||||||||||
3923 | /*! | - | ||||||||||||
3924 | \since 4.2 | - | ||||||||||||
3925 | - | |||||||||||||
3926 | Returns the names of all properties that were dynamically added to | - | ||||||||||||
3927 | the object using setProperty(). | - | ||||||||||||
3928 | */ | - | ||||||||||||
3929 | QList<QByteArray> QObject::dynamicPropertyNames() const | - | ||||||||||||
3930 | { | - | ||||||||||||
3931 | Q_D(const QObject); | - | ||||||||||||
3932 | if (d->extraData) | - | ||||||||||||
3933 | return d->extraData->propertyNames; | - | ||||||||||||
3934 | return QList<QByteArray>(); | - | ||||||||||||
3935 | } | - | ||||||||||||
3936 | - | |||||||||||||
3937 | #endif // QT_NO_PROPERTIES | - | ||||||||||||
3938 | - | |||||||||||||
3939 | - | |||||||||||||
3940 | /***************************************************************************** | - | ||||||||||||
3941 | QObject debugging output routines. | - | ||||||||||||
3942 | *****************************************************************************/ | - | ||||||||||||
3943 | - | |||||||||||||
3944 | static void dumpRecursive(int level, QObject *object) | - | ||||||||||||
3945 | { | - | ||||||||||||
3946 | #if defined(QT_DEBUG) | - | ||||||||||||
3947 | if (object) { | - | ||||||||||||
3948 | QByteArray buf; | - | ||||||||||||
3949 | buf.fill(' ', level / 2 * 8); | - | ||||||||||||
3950 | if (level % 2) | - | ||||||||||||
3951 | buf += " "; | - | ||||||||||||
3952 | QString name = object->objectName(); | - | ||||||||||||
3953 | QString flags = QLatin1String(""); | - | ||||||||||||
3954 | #if 0 | - | ||||||||||||
3955 | if (qApp->focusWidget() == object) | - | ||||||||||||
3956 | flags += 'F'; | - | ||||||||||||
3957 | if (object->isWidgetType()) { | - | ||||||||||||
3958 | QWidget * w = (QWidget *)object; | - | ||||||||||||
3959 | if (w->isVisible()) { | - | ||||||||||||
3960 | QString t("<%1,%2,%3,%4>"); | - | ||||||||||||
3961 | flags += t.arg(w->x()).arg(w->y()).arg(w->width()).arg(w->height()); | - | ||||||||||||
3962 | } else { | - | ||||||||||||
3963 | flags += 'I'; | - | ||||||||||||
3964 | } | - | ||||||||||||
3965 | } | - | ||||||||||||
3966 | #endif | - | ||||||||||||
3967 | qDebug("%s%s::%s %s", (const char*)buf, object->metaObject()->className(), name.toLocal8Bit().data(), | - | ||||||||||||
3968 | flags.toLatin1().data()); | - | ||||||||||||
3969 | QObjectList children = object->children(); | - | ||||||||||||
3970 | if (!children.isEmpty()) { | - | ||||||||||||
3971 | for (int i = 0; i < children.size(); ++i) | - | ||||||||||||
3972 | dumpRecursive(level+1, children.at(i)); | - | ||||||||||||
3973 | } | - | ||||||||||||
3974 | } | - | ||||||||||||
3975 | #else | - | ||||||||||||
3976 | Q_UNUSED(level) | - | ||||||||||||
3977 | Q_UNUSED(object) | - | ||||||||||||
3978 | #endif | - | ||||||||||||
3979 | } | - | ||||||||||||
3980 | - | |||||||||||||
3981 | /*! | - | ||||||||||||
3982 | Dumps a tree of children to the debug output. | - | ||||||||||||
3983 | - | |||||||||||||
3984 | This function is useful for debugging, but does nothing if the | - | ||||||||||||
3985 | library has been compiled in release mode (i.e. without debugging | - | ||||||||||||
3986 | information). | - | ||||||||||||
3987 | - | |||||||||||||
3988 | \sa dumpObjectInfo() | - | ||||||||||||
3989 | */ | - | ||||||||||||
3990 | - | |||||||||||||
3991 | void QObject::dumpObjectTree() | - | ||||||||||||
3992 | { | - | ||||||||||||
3993 | dumpRecursive(0, this); | - | ||||||||||||
3994 | } | - | ||||||||||||
3995 | - | |||||||||||||
3996 | /*! | - | ||||||||||||
3997 | Dumps information about signal connections, etc. for this object | - | ||||||||||||
3998 | to the debug output. | - | ||||||||||||
3999 | - | |||||||||||||
4000 | This function is useful for debugging, but does nothing if the | - | ||||||||||||
4001 | library has been compiled in release mode (i.e. without debugging | - | ||||||||||||
4002 | information). | - | ||||||||||||
4003 | - | |||||||||||||
4004 | \sa dumpObjectTree() | - | ||||||||||||
4005 | */ | - | ||||||||||||
4006 | - | |||||||||||||
4007 | void QObject::dumpObjectInfo() | - | ||||||||||||
4008 | { | - | ||||||||||||
4009 | #if defined(QT_DEBUG) | - | ||||||||||||
4010 | qDebug("OBJECT %s::%s", metaObject()->className(), | - | ||||||||||||
4011 | objectName().isEmpty() ? "unnamed" : objectName().toLocal8Bit().data()); | - | ||||||||||||
4012 | - | |||||||||||||
4013 | Q_D(QObject); | - | ||||||||||||
4014 | QMutexLocker locker(signalSlotLock(this)); | - | ||||||||||||
4015 | - | |||||||||||||
4016 | // first, look for connections where this object is the sender | - | ||||||||||||
4017 | qDebug(" SIGNALS OUT"); | - | ||||||||||||
4018 | - | |||||||||||||
4019 | if (d->connectionLists) { | - | ||||||||||||
4020 | for (int signal_index = 0; signal_index < d->connectionLists->count(); ++signal_index) { | - | ||||||||||||
4021 | const QMetaMethod signal = QMetaObjectPrivate::signal(metaObject(), signal_index); | - | ||||||||||||
4022 | qDebug(" signal: %s", signal.methodSignature().constData()); | - | ||||||||||||
4023 | - | |||||||||||||
4024 | // receivers | - | ||||||||||||
4025 | const QObjectPrivate::Connection *c = | - | ||||||||||||
4026 | d->connectionLists->at(signal_index).first; | - | ||||||||||||
4027 | while (c) { | - | ||||||||||||
4028 | if (!c->receiver) { | - | ||||||||||||
4029 | qDebug(" <Disconnected receiver>"); | - | ||||||||||||
4030 | c = c->nextConnectionList; | - | ||||||||||||
4031 | continue; | - | ||||||||||||
4032 | } | - | ||||||||||||
4033 | if (c->isSlotObject) { | - | ||||||||||||
4034 | qDebug(" <functor or function pointer>"); | - | ||||||||||||
4035 | c = c->nextConnectionList; | - | ||||||||||||
4036 | continue; | - | ||||||||||||
4037 | } | - | ||||||||||||
4038 | const QMetaObject *receiverMetaObject = c->receiver->metaObject(); | - | ||||||||||||
4039 | const QMetaMethod method = receiverMetaObject->method(c->method()); | - | ||||||||||||
4040 | qDebug(" --> %s::%s %s", | - | ||||||||||||
4041 | receiverMetaObject->className(), | - | ||||||||||||
4042 | c->receiver->objectName().isEmpty() ? "unnamed" : qPrintable(c->receiver->objectName()), | - | ||||||||||||
4043 | method.methodSignature().constData()); | - | ||||||||||||
4044 | c = c->nextConnectionList; | - | ||||||||||||
4045 | } | - | ||||||||||||
4046 | } | - | ||||||||||||
4047 | } else { | - | ||||||||||||
4048 | qDebug( " <None>" ); | - | ||||||||||||
4049 | } | - | ||||||||||||
4050 | - | |||||||||||||
4051 | // now look for connections where this object is the receiver | - | ||||||||||||
4052 | qDebug(" SIGNALS IN"); | - | ||||||||||||
4053 | - | |||||||||||||
4054 | if (d->senders) { | - | ||||||||||||
4055 | for (QObjectPrivate::Connection *s = d->senders; s; s = s->next) { | - | ||||||||||||
4056 | QByteArray slotName = QByteArrayLiteral("<unknown>"); | - | ||||||||||||
4057 | if (!s->isSlotObject) { | - | ||||||||||||
4058 | const QMetaMethod slot = metaObject()->method(s->method()); | - | ||||||||||||
4059 | slotName = slot.methodSignature(); | - | ||||||||||||
4060 | } | - | ||||||||||||
4061 | qDebug(" <-- %s::%s %s", | - | ||||||||||||
4062 | s->sender->metaObject()->className(), | - | ||||||||||||
4063 | s->sender->objectName().isEmpty() ? "unnamed" : qPrintable(s->sender->objectName()), | - | ||||||||||||
4064 | slotName.constData()); | - | ||||||||||||
4065 | } | - | ||||||||||||
4066 | } else { | - | ||||||||||||
4067 | qDebug(" <None>"); | - | ||||||||||||
4068 | } | - | ||||||||||||
4069 | #endif | - | ||||||||||||
4070 | } | - | ||||||||||||
4071 | - | |||||||||||||
4072 | #ifndef QT_NO_USERDATA | - | ||||||||||||
4073 | /*! | - | ||||||||||||
4074 | \internal | - | ||||||||||||
4075 | */ | - | ||||||||||||
4076 | uint QObject::registerUserData() | - | ||||||||||||
4077 | { | - | ||||||||||||
4078 | static int user_data_registration = 0; | - | ||||||||||||
4079 | return user_data_registration++; | - | ||||||||||||
4080 | } | - | ||||||||||||
4081 | - | |||||||||||||
4082 | /*! | - | ||||||||||||
4083 | \internal | - | ||||||||||||
4084 | */ | - | ||||||||||||
4085 | QObjectUserData::~QObjectUserData() | - | ||||||||||||
4086 | { | - | ||||||||||||
4087 | } | - | ||||||||||||
4088 | - | |||||||||||||
4089 | /*! | - | ||||||||||||
4090 | \internal | - | ||||||||||||
4091 | */ | - | ||||||||||||
4092 | void QObject::setUserData(uint id, QObjectUserData* data) | - | ||||||||||||
4093 | { | - | ||||||||||||
4094 | Q_D(QObject); | - | ||||||||||||
4095 | if (!d->extraData) | - | ||||||||||||
4096 | d->extraData = new QObjectPrivate::ExtraData; | - | ||||||||||||
4097 | - | |||||||||||||
4098 | if (d->extraData->userData.size() <= (int) id) | - | ||||||||||||
4099 | d->extraData->userData.resize((int) id + 1); | - | ||||||||||||
4100 | d->extraData->userData[id] = data; | - | ||||||||||||
4101 | } | - | ||||||||||||
4102 | - | |||||||||||||
4103 | /*! | - | ||||||||||||
4104 | \internal | - | ||||||||||||
4105 | */ | - | ||||||||||||
4106 | QObjectUserData* QObject::userData(uint id) const | - | ||||||||||||
4107 | { | - | ||||||||||||
4108 | Q_D(const QObject); | - | ||||||||||||
4109 | if (!d->extraData) | - | ||||||||||||
4110 | return 0; | - | ||||||||||||
4111 | if ((int)id < d->extraData->userData.size()) | - | ||||||||||||
4112 | return d->extraData->userData.at(id); | - | ||||||||||||
4113 | return 0; | - | ||||||||||||
4114 | } | - | ||||||||||||
4115 | - | |||||||||||||
4116 | #endif // QT_NO_USERDATA | - | ||||||||||||
4117 | - | |||||||||||||
4118 | - | |||||||||||||
4119 | #ifndef QT_NO_DEBUG_STREAM | - | ||||||||||||
4120 | QDebug operator<<(QDebug dbg, const QObject *o) | - | ||||||||||||
4121 | { | - | ||||||||||||
4122 | QDebugStateSaver saver(dbg); | - | ||||||||||||
4123 | if (!o) | - | ||||||||||||
4124 | return dbg << "QObject(0x0)"; | - | ||||||||||||
4125 | dbg.nospace() << o->metaObject()->className() << '(' << (const void *)o; | - | ||||||||||||
4126 | if (!o->objectName().isEmpty()) | - | ||||||||||||
4127 | dbg << ", name = " << o->objectName(); | - | ||||||||||||
4128 | dbg << ')'; | - | ||||||||||||
4129 | return dbg; | - | ||||||||||||
4130 | } | - | ||||||||||||
4131 | #endif | - | ||||||||||||
4132 | - | |||||||||||||
4133 | /*! | - | ||||||||||||
4134 | \macro Q_CLASSINFO(Name, Value) | - | ||||||||||||
4135 | \relates QObject | - | ||||||||||||
4136 | - | |||||||||||||
4137 | This macro associates extra information to the class, which is available | - | ||||||||||||
4138 | using QObject::metaObject(). Qt makes only limited use of this feature, in | - | ||||||||||||
4139 | the \l{Active Qt}, \l{Qt D-Bus} and \l{Qt QML module}{Qt QML}. | - | ||||||||||||
4140 | - | |||||||||||||
4141 | The extra information takes the form of a \a Name string and a \a Value | - | ||||||||||||
4142 | literal string. | - | ||||||||||||
4143 | - | |||||||||||||
4144 | Example: | - | ||||||||||||
4145 | - | |||||||||||||
4146 | \snippet code/src_corelib_kernel_qobject.cpp 35 | - | ||||||||||||
4147 | - | |||||||||||||
4148 | \sa QMetaObject::classInfo() | - | ||||||||||||
4149 | \sa QAxFactory | - | ||||||||||||
4150 | \sa {Using Qt D-Bus Adaptors} | - | ||||||||||||
4151 | \sa {Extending QML} | - | ||||||||||||
4152 | */ | - | ||||||||||||
4153 | - | |||||||||||||
4154 | /*! | - | ||||||||||||
4155 | \macro Q_INTERFACES(...) | - | ||||||||||||
4156 | \relates QObject | - | ||||||||||||
4157 | - | |||||||||||||
4158 | This macro tells Qt which interfaces the class implements. This | - | ||||||||||||
4159 | is used when implementing plugins. | - | ||||||||||||
4160 | - | |||||||||||||
4161 | Example: | - | ||||||||||||
4162 | - | |||||||||||||
4163 | \snippet ../widgets/tools/plugandpaint/plugins/basictools/basictoolsplugin.h 1 | - | ||||||||||||
4164 | \dots | - | ||||||||||||
4165 | \snippet ../widgets/tools/plugandpaint/plugins/basictools/basictoolsplugin.h 3 | - | ||||||||||||
4166 | - | |||||||||||||
4167 | See the \l{tools/plugandpaint/plugins/basictools}{Plug & Paint | - | ||||||||||||
4168 | Basic Tools} example for details. | - | ||||||||||||
4169 | - | |||||||||||||
4170 | \sa Q_DECLARE_INTERFACE(), Q_PLUGIN_METADATA(), {How to Create Qt Plugins} | - | ||||||||||||
4171 | */ | - | ||||||||||||
4172 | - | |||||||||||||
4173 | /*! | - | ||||||||||||
4174 | \macro Q_PROPERTY(...) | - | ||||||||||||
4175 | \relates QObject | - | ||||||||||||
4176 | - | |||||||||||||
4177 | This macro is used for declaring properties in classes that | - | ||||||||||||
4178 | inherit QObject. Properties behave like class data members, but | - | ||||||||||||
4179 | they have additional features accessible through the \l | - | ||||||||||||
4180 | {Meta-Object System}. | - | ||||||||||||
4181 | - | |||||||||||||
4182 | \snippet code/doc_src_properties.cpp 0 | - | ||||||||||||
4183 | - | |||||||||||||
4184 | The property name and type and the \c READ function are required. | - | ||||||||||||
4185 | The type can be any type supported by QVariant, or it can be a | - | ||||||||||||
4186 | user-defined type. The other items are optional, but a \c WRITE | - | ||||||||||||
4187 | function is common. The attributes default to true except \c USER, | - | ||||||||||||
4188 | which defaults to false. | - | ||||||||||||
4189 | - | |||||||||||||
4190 | For example: | - | ||||||||||||
4191 | - | |||||||||||||
4192 | \snippet code/src_corelib_kernel_qobject.cpp 37 | - | ||||||||||||
4193 | - | |||||||||||||
4194 | For more details about how to use this macro, and a more detailed | - | ||||||||||||
4195 | example of its use, see the discussion on \l {Qt's Property System}. | - | ||||||||||||
4196 | - | |||||||||||||
4197 | \sa {Qt's Property System} | - | ||||||||||||
4198 | */ | - | ||||||||||||
4199 | - | |||||||||||||
4200 | /*! | - | ||||||||||||
4201 | \macro Q_ENUMS(...) | - | ||||||||||||
4202 | \relates QObject | - | ||||||||||||
4203 | \obsolete | - | ||||||||||||
4204 | - | |||||||||||||
4205 | This macro registers one or several enum types to the meta-object | - | ||||||||||||
4206 | system. | - | ||||||||||||
4207 | - | |||||||||||||
4208 | For example: | - | ||||||||||||
4209 | - | |||||||||||||
4210 | \snippet code/src_corelib_kernel_qobject.cpp 38 | - | ||||||||||||
4211 | - | |||||||||||||
4212 | If you want to register an enum that is declared in another class, | - | ||||||||||||
4213 | the enum must be fully qualified with the name of the class | - | ||||||||||||
4214 | defining it. In addition, the class \e defining the enum has to | - | ||||||||||||
4215 | inherit QObject as well as declare the enum using Q_ENUMS(). | - | ||||||||||||
4216 | - | |||||||||||||
4217 | In new code, you should prefer the use of the Q_ENUM() macro, which makes the | - | ||||||||||||
4218 | type available also to the meta type system. | - | ||||||||||||
4219 | For instance, QMetaEnum::fromType() will not work with types declared with Q_ENUMS(). | - | ||||||||||||
4220 | - | |||||||||||||
4221 | \sa {Qt's Property System} | - | ||||||||||||
4222 | */ | - | ||||||||||||
4223 | - | |||||||||||||
4224 | /*! | - | ||||||||||||
4225 | \macro Q_FLAGS(...) | - | ||||||||||||
4226 | \relates QObject | - | ||||||||||||
4227 | \obsolete | - | ||||||||||||
4228 | - | |||||||||||||
4229 | This macro registers one or several \l{QFlags}{flags types} with the | - | ||||||||||||
4230 | meta-object system. It is typically used in a class definition to declare | - | ||||||||||||
4231 | that values of a given enum can be used as flags and combined using the | - | ||||||||||||
4232 | bitwise OR operator. | - | ||||||||||||
4233 | - | |||||||||||||
4234 | \note This macro takes care of registering individual flag values | - | ||||||||||||
4235 | with the meta-object system, so it is unnecessary to use Q_ENUMS() | - | ||||||||||||
4236 | in addition to this macro. | - | ||||||||||||
4237 | - | |||||||||||||
4238 | In new code, you should prefer the use of the Q_FLAG() macro, which makes the | - | ||||||||||||
4239 | type available also to the meta type system. | - | ||||||||||||
4240 | - | |||||||||||||
4241 | \sa {Qt's Property System} | - | ||||||||||||
4242 | */ | - | ||||||||||||
4243 | - | |||||||||||||
4244 | /*! | - | ||||||||||||
4245 | \macro Q_ENUM(...) | - | ||||||||||||
4246 | \relates QObject | - | ||||||||||||
4247 | \since 5.5 | - | ||||||||||||
4248 | - | |||||||||||||
4249 | This macro registers an enum type with the meta-object system. | - | ||||||||||||
4250 | It must be placed after the enum declaration in a class that has the Q_OBJECT or the | - | ||||||||||||
4251 | Q_GADGET macro. | - | ||||||||||||
4252 | - | |||||||||||||
4253 | For example: | - | ||||||||||||
4254 | - | |||||||||||||
4255 | \snippet code/src_corelib_kernel_qobject.cpp 38 | - | ||||||||||||
4256 | - | |||||||||||||
4257 | Enumerations that are declared with Q_ENUM have their QMetaEnum registered in the | - | ||||||||||||
4258 | enclosing QMetaObject. You can also use QMetaEnum::fromType() to get the QMetaEnum. | - | ||||||||||||
4259 | - | |||||||||||||
4260 | Registered enumerations are automatically registered also to the Qt meta | - | ||||||||||||
4261 | type system, making them known to QMetaType without the need to use | - | ||||||||||||
4262 | Q_DECLARE_METATYPE(). This will enable useful features; for example, if used | - | ||||||||||||
4263 | in a QVariant, you can convert them to strings. Likewise, passing them to | - | ||||||||||||
4264 | QDebug will print out their names. | - | ||||||||||||
4265 | - | |||||||||||||
4266 | \sa {Qt's Property System} | - | ||||||||||||
4267 | */ | - | ||||||||||||
4268 | - | |||||||||||||
4269 | - | |||||||||||||
4270 | /*! | - | ||||||||||||
4271 | \macro Q_FLAG(...) | - | ||||||||||||
4272 | \relates QObject | - | ||||||||||||
4273 | \since 5.5 | - | ||||||||||||
4274 | - | |||||||||||||
4275 | This macro registers a single \l{QFlags}{flags type} with the | - | ||||||||||||
4276 | meta-object system. It is typically used in a class definition to declare | - | ||||||||||||
4277 | that values of a given enum can be used as flags and combined using the | - | ||||||||||||
4278 | bitwise OR operator. | - | ||||||||||||
4279 | - | |||||||||||||
4280 | The macro must be placed after the enum declaration. | - | ||||||||||||
4281 | - | |||||||||||||
4282 | For example, in QLibrary, the \l{QLibrary::LoadHints}{LoadHints} flag is | - | ||||||||||||
4283 | declared in the following way: | - | ||||||||||||
4284 | - | |||||||||||||
4285 | \snippet code/src_corelib_kernel_qobject.cpp 39 | - | ||||||||||||
4286 | - | |||||||||||||
4287 | The declaration of the flags themselves is performed in the public section | - | ||||||||||||
4288 | of the QLibrary class itself, using the \l Q_DECLARE_FLAGS() macro. | - | ||||||||||||
4289 | - | |||||||||||||
4290 | \note The Q_FLAG macro takes care of registering individual flag values | - | ||||||||||||
4291 | with the meta-object system, so it is unnecessary to use Q_ENUM() | - | ||||||||||||
4292 | in addition to this macro. | - | ||||||||||||
4293 | - | |||||||||||||
4294 | \sa {Qt's Property System} | - | ||||||||||||
4295 | */ | - | ||||||||||||
4296 | - | |||||||||||||
4297 | - | |||||||||||||
4298 | /*! | - | ||||||||||||
4299 | \macro Q_OBJECT | - | ||||||||||||
4300 | \relates QObject | - | ||||||||||||
4301 | - | |||||||||||||
4302 | The Q_OBJECT macro must appear in the private section of a class | - | ||||||||||||
4303 | definition that declares its own signals and slots or that uses | - | ||||||||||||
4304 | other services provided by Qt's meta-object system. | - | ||||||||||||
4305 | - | |||||||||||||
4306 | For example: | - | ||||||||||||
4307 | - | |||||||||||||
4308 | \snippet signalsandslots/signalsandslots.h 1 | - | ||||||||||||
4309 | \codeline | - | ||||||||||||
4310 | \snippet signalsandslots/signalsandslots.h 2 | - | ||||||||||||
4311 | \snippet signalsandslots/signalsandslots.h 3 | - | ||||||||||||
4312 | - | |||||||||||||
4313 | \note This macro requires the class to be a subclass of QObject. Use | - | ||||||||||||
4314 | Q_GADGET instead of Q_OBJECT to enable the meta object system's support | - | ||||||||||||
4315 | for enums in a class that is not a QObject subclass. | - | ||||||||||||
4316 | - | |||||||||||||
4317 | \sa {Meta-Object System}, {Signals and Slots}, {Qt's Property System} | - | ||||||||||||
4318 | */ | - | ||||||||||||
4319 | - | |||||||||||||
4320 | /*! | - | ||||||||||||
4321 | \macro Q_GADGET | - | ||||||||||||
4322 | \relates QObject | - | ||||||||||||
4323 | - | |||||||||||||
4324 | The Q_GADGET macro is a lighter version of the Q_OBJECT macro for classes | - | ||||||||||||
4325 | that do not inherit from QObject but still want to use some of the | - | ||||||||||||
4326 | reflection capabilities offered by QMetaObject. Just like the Q_OBJECT | - | ||||||||||||
4327 | macro, it must appear in the private section of a class definition. | - | ||||||||||||
4328 | - | |||||||||||||
4329 | Q_GADGETs can have Q_ENUM, Q_PROPERTY and Q_INVOKABLE, but they cannot have | - | ||||||||||||
4330 | signals or slots | - | ||||||||||||
4331 | - | |||||||||||||
4332 | Q_GADGET makes a class member, \c{staticMetaObject}, available. | - | ||||||||||||
4333 | \c{staticMetaObject} is of type QMetaObject and provides access to the | - | ||||||||||||
4334 | enums declared with Q_ENUMS. | - | ||||||||||||
4335 | */ | - | ||||||||||||
4336 | - | |||||||||||||
4337 | /*! | - | ||||||||||||
4338 | \macro Q_SIGNALS | - | ||||||||||||
4339 | \relates QObject | - | ||||||||||||
4340 | - | |||||||||||||
4341 | Use this macro to replace the \c signals keyword in class | - | ||||||||||||
4342 | declarations, when you want to use Qt Signals and Slots with a | - | ||||||||||||
4343 | \l{3rd Party Signals and Slots} {3rd party signal/slot mechanism}. | - | ||||||||||||
4344 | - | |||||||||||||
4345 | The macro is normally used when \c no_keywords is specified with | - | ||||||||||||
4346 | the \c CONFIG variable in the \c .pro file, but it can be used | - | ||||||||||||
4347 | even when \c no_keywords is \e not specified. | - | ||||||||||||
4348 | */ | - | ||||||||||||
4349 | - | |||||||||||||
4350 | /*! | - | ||||||||||||
4351 | \macro Q_SIGNAL | - | ||||||||||||
4352 | \relates QObject | - | ||||||||||||
4353 | - | |||||||||||||
4354 | This is an additional macro that allows you to mark a single | - | ||||||||||||
4355 | function as a signal. It can be quite useful, especially when you | - | ||||||||||||
4356 | use a 3rd-party source code parser which doesn't understand a \c | - | ||||||||||||
4357 | signals or \c Q_SIGNALS groups. | - | ||||||||||||
4358 | - | |||||||||||||
4359 | Use this macro to replace the \c signals keyword in class | - | ||||||||||||
4360 | declarations, when you want to use Qt Signals and Slots with a | - | ||||||||||||
4361 | \l{3rd Party Signals and Slots} {3rd party signal/slot mechanism}. | - | ||||||||||||
4362 | - | |||||||||||||
4363 | The macro is normally used when \c no_keywords is specified with | - | ||||||||||||
4364 | the \c CONFIG variable in the \c .pro file, but it can be used | - | ||||||||||||
4365 | even when \c no_keywords is \e not specified. | - | ||||||||||||
4366 | */ | - | ||||||||||||
4367 | - | |||||||||||||
4368 | /*! | - | ||||||||||||
4369 | \macro Q_SLOTS | - | ||||||||||||
4370 | \relates QObject | - | ||||||||||||
4371 | - | |||||||||||||
4372 | Use this macro to replace the \c slots keyword in class | - | ||||||||||||
4373 | declarations, when you want to use Qt Signals and Slots with a | - | ||||||||||||
4374 | \l{3rd Party Signals and Slots} {3rd party signal/slot mechanism}. | - | ||||||||||||
4375 | - | |||||||||||||
4376 | The macro is normally used when \c no_keywords is specified with | - | ||||||||||||
4377 | the \c CONFIG variable in the \c .pro file, but it can be used | - | ||||||||||||
4378 | even when \c no_keywords is \e not specified. | - | ||||||||||||
4379 | */ | - | ||||||||||||
4380 | - | |||||||||||||
4381 | /*! | - | ||||||||||||
4382 | \macro Q_SLOT | - | ||||||||||||
4383 | \relates QObject | - | ||||||||||||
4384 | - | |||||||||||||
4385 | This is an additional macro that allows you to mark a single | - | ||||||||||||
4386 | function as a slot. It can be quite useful, especially when you | - | ||||||||||||
4387 | use a 3rd-party source code parser which doesn't understand a \c | - | ||||||||||||
4388 | slots or \c Q_SLOTS groups. | - | ||||||||||||
4389 | - | |||||||||||||
4390 | Use this macro to replace the \c slots keyword in class | - | ||||||||||||
4391 | declarations, when you want to use Qt Signals and Slots with a | - | ||||||||||||
4392 | \l{3rd Party Signals and Slots} {3rd party signal/slot mechanism}. | - | ||||||||||||
4393 | - | |||||||||||||
4394 | The macro is normally used when \c no_keywords is specified with | - | ||||||||||||
4395 | the \c CONFIG variable in the \c .pro file, but it can be used | - | ||||||||||||
4396 | even when \c no_keywords is \e not specified. | - | ||||||||||||
4397 | */ | - | ||||||||||||
4398 | - | |||||||||||||
4399 | /*! | - | ||||||||||||
4400 | \macro Q_EMIT | - | ||||||||||||
4401 | \relates QObject | - | ||||||||||||
4402 | - | |||||||||||||
4403 | Use this macro to replace the \c emit keyword for emitting | - | ||||||||||||
4404 | signals, when you want to use Qt Signals and Slots with a | - | ||||||||||||
4405 | \l{3rd Party Signals and Slots} {3rd party signal/slot mechanism}. | - | ||||||||||||
4406 | - | |||||||||||||
4407 | The macro is normally used when \c no_keywords is specified with | - | ||||||||||||
4408 | the \c CONFIG variable in the \c .pro file, but it can be used | - | ||||||||||||
4409 | even when \c no_keywords is \e not specified. | - | ||||||||||||
4410 | */ | - | ||||||||||||
4411 | - | |||||||||||||
4412 | /*! | - | ||||||||||||
4413 | \macro Q_INVOKABLE | - | ||||||||||||
4414 | \relates QObject | - | ||||||||||||
4415 | - | |||||||||||||
4416 | Apply this macro to declarations of member functions to allow them to | - | ||||||||||||
4417 | be invoked via the meta-object system. The macro is written before | - | ||||||||||||
4418 | the return type, as shown in the following example: | - | ||||||||||||
4419 | - | |||||||||||||
4420 | \snippet qmetaobject-invokable/window.h Window class with invokable method | - | ||||||||||||
4421 | - | |||||||||||||
4422 | The \c invokableMethod() function is marked up using Q_INVOKABLE, causing | - | ||||||||||||
4423 | it to be registered with the meta-object system and enabling it to be | - | ||||||||||||
4424 | invoked using QMetaObject::invokeMethod(). | - | ||||||||||||
4425 | Since \c normalMethod() function is not registered in this way, it cannot | - | ||||||||||||
4426 | be invoked using QMetaObject::invokeMethod(). | - | ||||||||||||
4427 | */ | - | ||||||||||||
4428 | - | |||||||||||||
4429 | /*! | - | ||||||||||||
4430 | \macro Q_REVISION | - | ||||||||||||
4431 | \relates QObject | - | ||||||||||||
4432 | - | |||||||||||||
4433 | Apply this macro to declarations of member functions to tag them with a | - | ||||||||||||
4434 | revision number in the meta-object system. The macro is written before | - | ||||||||||||
4435 | the return type, as shown in the following example: | - | ||||||||||||
4436 | - | |||||||||||||
4437 | \snippet qmetaobject-revision/window.h Window class with revision | - | ||||||||||||
4438 | - | |||||||||||||
4439 | This is useful when using the meta-object system to dynamically expose | - | ||||||||||||
4440 | objects to another API, as you can match the version expected by multiple | - | ||||||||||||
4441 | versions of the other API. Consider the following simplified example: | - | ||||||||||||
4442 | - | |||||||||||||
4443 | \snippet qmetaobject-revision/main.cpp Window class using revision | - | ||||||||||||
4444 | - | |||||||||||||
4445 | Using the same Window class as the previous example, the newProperty and | - | ||||||||||||
4446 | newMethod would only be exposed in this code when the expected version is | - | ||||||||||||
4447 | 1 or greater. | - | ||||||||||||
4448 | - | |||||||||||||
4449 | Since all methods are considered to be in revision 0 if untagged, a tag | - | ||||||||||||
4450 | of Q_REVISION(0) is invalid and ignored. | - | ||||||||||||
4451 | - | |||||||||||||
4452 | This tag is not used by the meta-object system itself. Currently this is only | - | ||||||||||||
4453 | used by the QtQml module. | - | ||||||||||||
4454 | - | |||||||||||||
4455 | For a more generic string tag, see \l QMetaMethod::tag() | - | ||||||||||||
4456 | - | |||||||||||||
4457 | \sa QMetaMethod::revision() | - | ||||||||||||
4458 | */ | - | ||||||||||||
4459 | - | |||||||||||||
4460 | /*! | - | ||||||||||||
4461 | \macro Q_SET_OBJECT_NAME(Object) | - | ||||||||||||
4462 | \relates QObject | - | ||||||||||||
4463 | \since 5.0 | - | ||||||||||||
4464 | - | |||||||||||||
4465 | This macro assigns \a Object the objectName "Object". | - | ||||||||||||
4466 | - | |||||||||||||
4467 | It doesn't matter whether \a Object is a pointer or not, the | - | ||||||||||||
4468 | macro figures that out by itself. | - | ||||||||||||
4469 | - | |||||||||||||
4470 | \sa QObject::objectName() | - | ||||||||||||
4471 | */ | - | ||||||||||||
4472 | - | |||||||||||||
4473 | /*! | - | ||||||||||||
4474 | \typedef QObjectList | - | ||||||||||||
4475 | \relates QObject | - | ||||||||||||
4476 | - | |||||||||||||
4477 | Synonym for QList<QObject *>. | - | ||||||||||||
4478 | */ | - | ||||||||||||
4479 | - | |||||||||||||
4480 | void qDeleteInEventHandler(QObject *o) | - | ||||||||||||
4481 | { | - | ||||||||||||
4482 | delete o; | - | ||||||||||||
4483 | } | - | ||||||||||||
4484 | - | |||||||||||||
4485 | /*! | - | ||||||||||||
4486 | \fn QMetaObject::Connection QObject::connect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type) | - | ||||||||||||
4487 | \overload connect() | - | ||||||||||||
4488 | \threadsafe | - | ||||||||||||
4489 | - | |||||||||||||
4490 | Creates a connection of the given \a type from the \a signal in | - | ||||||||||||
4491 | the \a sender object to the \a method in the \a receiver object. | - | ||||||||||||
4492 | Returns a handle to the connection that can be used to disconnect | - | ||||||||||||
4493 | it later. | - | ||||||||||||
4494 | - | |||||||||||||
4495 | The signal must be a function declared as a signal in the header. | - | ||||||||||||
4496 | The slot function can be any member function that can be connected | - | ||||||||||||
4497 | to the signal. | - | ||||||||||||
4498 | A slot can be connected to a given signal if the signal has at | - | ||||||||||||
4499 | least as many arguments as the slot, and there is an implicit | - | ||||||||||||
4500 | conversion between the types of the corresponding arguments in the | - | ||||||||||||
4501 | signal and the slot. | - | ||||||||||||
4502 | - | |||||||||||||
4503 | Example: | - | ||||||||||||
4504 | - | |||||||||||||
4505 | \snippet code/src_corelib_kernel_qobject.cpp 44 | - | ||||||||||||
4506 | - | |||||||||||||
4507 | This example ensures that the label always displays the current | - | ||||||||||||
4508 | line edit text. | - | ||||||||||||
4509 | - | |||||||||||||
4510 | A signal can be connected to many slots and signals. Many signals | - | ||||||||||||
4511 | can be connected to one slot. | - | ||||||||||||
4512 | - | |||||||||||||
4513 | If a signal is connected to several slots, the slots are activated | - | ||||||||||||
4514 | in the same order as the order the connection was made, when the | - | ||||||||||||
4515 | signal is emitted | - | ||||||||||||
4516 | - | |||||||||||||
4517 | The function returns an handle to a connection if it successfully | - | ||||||||||||
4518 | connects the signal to the slot. The Connection handle will be invalid | - | ||||||||||||
4519 | if it cannot create the connection, for example, if QObject is unable | - | ||||||||||||
4520 | to verify the existence of \a signal (if it was not declared as a signal) | - | ||||||||||||
4521 | You can check if the QMetaObject::Connection is valid by casting it to a bool. | - | ||||||||||||
4522 | - | |||||||||||||
4523 | By default, a signal is emitted for every connection you make; | - | ||||||||||||
4524 | two signals are emitted for duplicate connections. You can break | - | ||||||||||||
4525 | all of these connections with a single disconnect() call. | - | ||||||||||||
4526 | If you pass the Qt::UniqueConnection \a type, the connection will only | - | ||||||||||||
4527 | be made if it is not a duplicate. If there is already a duplicate | - | ||||||||||||
4528 | (exact same signal to the exact same slot on the same objects), | - | ||||||||||||
4529 | the connection will fail and connect will return an invalid QMetaObject::Connection. | - | ||||||||||||
4530 | - | |||||||||||||
4531 | The optional \a type parameter describes the type of connection | - | ||||||||||||
4532 | to establish. In particular, it determines whether a particular | - | ||||||||||||
4533 | signal is delivered to a slot immediately or queued for delivery | - | ||||||||||||
4534 | at a later time. If the signal is queued, the parameters must be | - | ||||||||||||
4535 | of types that are known to Qt's meta-object system, because Qt | - | ||||||||||||
4536 | needs to copy the arguments to store them in an event behind the | - | ||||||||||||
4537 | scenes. If you try to use a queued connection and get the error | - | ||||||||||||
4538 | message | - | ||||||||||||
4539 | - | |||||||||||||
4540 | \snippet code/src_corelib_kernel_qobject.cpp 25 | - | ||||||||||||
4541 | - | |||||||||||||
4542 | make sure to declare the argument type with Q_DECLARE_METATYPE | - | ||||||||||||
4543 | - | |||||||||||||
4544 | Overloaded functions can be resolved with help of \l qOverload. | - | ||||||||||||
4545 | - | |||||||||||||
4546 | \note The number of arguments in the signal or slot are limited to 6 if | - | ||||||||||||
4547 | the compiler does not support C++11 variadic templates. | - | ||||||||||||
4548 | */ | - | ||||||||||||
4549 | - | |||||||||||||
4550 | - | |||||||||||||
4551 | /*! | - | ||||||||||||
4552 | \fn QMetaObject::Connection QObject::connect(const QObject *sender, PointerToMemberFunction signal, Functor functor) | - | ||||||||||||
4553 | - | |||||||||||||
4554 | \threadsafe | - | ||||||||||||
4555 | \overload connect() | - | ||||||||||||
4556 | - | |||||||||||||
4557 | Creates a connection from \a signal in | - | ||||||||||||
4558 | \a sender object to \a functor, and returns a handle to the connection | - | ||||||||||||
4559 | - | |||||||||||||
4560 | The signal must be a function declared as a signal in the header. | - | ||||||||||||
4561 | The slot function can be any function or functor that can be connected | - | ||||||||||||
4562 | to the signal. | - | ||||||||||||
4563 | A function can be connected to a given signal if the signal as at | - | ||||||||||||
4564 | least as many argument as the slot. A functor can be connected to a signal | - | ||||||||||||
4565 | if they have exactly the same number of arguments. There must exist implicit | - | ||||||||||||
4566 | conversion between the types of the corresponding arguments in the | - | ||||||||||||
4567 | signal and the slot. | - | ||||||||||||
4568 | - | |||||||||||||
4569 | Example: | - | ||||||||||||
4570 | - | |||||||||||||
4571 | \snippet code/src_corelib_kernel_qobject.cpp 45 | - | ||||||||||||
4572 | - | |||||||||||||
4573 | If your compiler support C++11 lambda expressions, you can use them: | - | ||||||||||||
4574 | - | |||||||||||||
4575 | \snippet code/src_corelib_kernel_qobject.cpp 46 | - | ||||||||||||
4576 | - | |||||||||||||
4577 | The connection will automatically disconnect if the sender is destroyed. | - | ||||||||||||
4578 | However, you should take care that any objects used within the functor | - | ||||||||||||
4579 | are still alive when the signal is emitted. | - | ||||||||||||
4580 | - | |||||||||||||
4581 | Overloaded functions can be resolved with help of \l qOverload. | - | ||||||||||||
4582 | - | |||||||||||||
4583 | \note If the compiler does not support C++11 variadic templates, the number | - | ||||||||||||
4584 | of arguments in the signal or slot are limited to 6, and the functor object | - | ||||||||||||
4585 | must not have an overloaded or templated operator(). | - | ||||||||||||
4586 | */ | - | ||||||||||||
4587 | - | |||||||||||||
4588 | /*! | - | ||||||||||||
4589 | \fn QMetaObject::Connection QObject::connect(const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type) | - | ||||||||||||
4590 | - | |||||||||||||
4591 | \threadsafe | - | ||||||||||||
4592 | \overload connect() | - | ||||||||||||
4593 | - | |||||||||||||
4594 | \since 5.2 | - | ||||||||||||
4595 | - | |||||||||||||
4596 | Creates a connection of a given \a type from \a signal in | - | ||||||||||||
4597 | \a sender object to \a functor to be placed in a specific event | - | ||||||||||||
4598 | loop of \a context, and returns a handle to the connection | - | ||||||||||||
4599 | - | |||||||||||||
4600 | The signal must be a function declared as a signal in the header. | - | ||||||||||||
4601 | The slot function can be any function or functor that can be connected | - | ||||||||||||
4602 | to the signal. | - | ||||||||||||
4603 | A function can be connected to a given signal if the signal as at | - | ||||||||||||
4604 | least as many argument as the slot. A functor can be connected to a signal | - | ||||||||||||
4605 | if they have exactly the same number of arguments. There must exist implicit | - | ||||||||||||
4606 | conversion between the types of the corresponding arguments in the | - | ||||||||||||
4607 | signal and the slot. | - | ||||||||||||
4608 | - | |||||||||||||
4609 | Example: | - | ||||||||||||
4610 | - | |||||||||||||
4611 | \snippet code/src_corelib_kernel_qobject.cpp 50 | - | ||||||||||||
4612 | - | |||||||||||||
4613 | If your compiler support C++11 lambda expressions, you can use them: | - | ||||||||||||
4614 | - | |||||||||||||
4615 | \snippet code/src_corelib_kernel_qobject.cpp 51 | - | ||||||||||||
4616 | - | |||||||||||||
4617 | The connection will automatically disconnect if the sender or the context | - | ||||||||||||
4618 | is destroyed. | - | ||||||||||||
4619 | However, you should take care that any objects used within the functor | - | ||||||||||||
4620 | are still alive when the signal is emitted. | - | ||||||||||||
4621 | - | |||||||||||||
4622 | Overloaded functions can be resolved with help of \l qOverload. | - | ||||||||||||
4623 | - | |||||||||||||
4624 | \note If the compiler does not support C++11 variadic templates, the number | - | ||||||||||||
4625 | of arguments in the signal or slot are limited to 6, and the functor object | - | ||||||||||||
4626 | must not have an overloaded or templated operator(). | - | ||||||||||||
4627 | */ | - | ||||||||||||
4628 | - | |||||||||||||
4629 | /*! | - | ||||||||||||
4630 | \internal | - | ||||||||||||
4631 | - | |||||||||||||
4632 | Implementation of the template version of connect | - | ||||||||||||
4633 | - | |||||||||||||
4634 | \a sender is the sender object | - | ||||||||||||
4635 | \a signal is a pointer to a pointer to a member signal of the sender | - | ||||||||||||
4636 | \a receiver is the receiver object, may not be null, will be equal to sender when | - | ||||||||||||
4637 | connecting to a static function or a functor | - | ||||||||||||
4638 | \a slot a pointer only used when using Qt::UniqueConnection | - | ||||||||||||
4639 | \a type the Qt::ConnctionType passed as argument to connect | - | ||||||||||||
4640 | \a types an array of integer with the metatype id of the parameter of the signal | - | ||||||||||||
4641 | to be used with queued connection | - | ||||||||||||
4642 | must stay valid at least for the whole time of the connection, this function | - | ||||||||||||
4643 | do not take ownership. typically static data. | - | ||||||||||||
4644 | If null, then the types will be computed when the signal is emit in a queued | - | ||||||||||||
4645 | connection from the types from the signature. | - | ||||||||||||
4646 | \a senderMetaObject is the metaobject used to lookup the signal, the signal must be in | - | ||||||||||||
4647 | this metaobject | - | ||||||||||||
4648 | */ | - | ||||||||||||
4649 | QMetaObject::Connection QObject::connectImpl(const QObject *sender, void **signal, | - | ||||||||||||
4650 | const QObject *receiver, void **slot, | - | ||||||||||||
4651 | QtPrivate::QSlotObjectBase *slotObj, Qt::ConnectionType type, | - | ||||||||||||
4652 | const int *types, const QMetaObject *senderMetaObject) | - | ||||||||||||
4653 | { | - | ||||||||||||
4654 | if (!signal) { | - | ||||||||||||
4655 | qWarning("QObject::connect: invalid null parameter"); | - | ||||||||||||
4656 | if (slotObj) | - | ||||||||||||
4657 | slotObj->destroyIfLastRef(); | - | ||||||||||||
4658 | return QMetaObject::Connection(); | - | ||||||||||||
4659 | } | - | ||||||||||||
4660 | - | |||||||||||||
4661 | int signal_index = -1; | - | ||||||||||||
4662 | void *args[] = { &signal_index, signal }; | - | ||||||||||||
4663 | for (; senderMetaObject && signal_index < 0; senderMetaObject = senderMetaObject->superClass()) { | - | ||||||||||||
4664 | senderMetaObject->static_metacall(QMetaObject::IndexOfMethod, 0, args); | - | ||||||||||||
4665 | if (signal_index >= 0 && signal_index < QMetaObjectPrivate::get(senderMetaObject)->signalCount) | - | ||||||||||||
4666 | break; | - | ||||||||||||
4667 | } | - | ||||||||||||
4668 | if (!senderMetaObject) { | - | ||||||||||||
4669 | qWarning("QObject::connect: signal not found in %s", sender->metaObject()->className()); | - | ||||||||||||
4670 | slotObj->destroyIfLastRef(); | - | ||||||||||||
4671 | return QMetaObject::Connection(0); | - | ||||||||||||
4672 | } | - | ||||||||||||
4673 | signal_index += QMetaObjectPrivate::signalOffset(senderMetaObject); | - | ||||||||||||
4674 | return QObjectPrivate::connectImpl(sender, signal_index, receiver, slot, slotObj, type, types, senderMetaObject); | - | ||||||||||||
4675 | } | - | ||||||||||||
4676 | - | |||||||||||||
4677 | /*! | - | ||||||||||||
4678 | \internal | - | ||||||||||||
4679 | - | |||||||||||||
4680 | Internal version of connect used by the template version of QObject::connect (called via connectImpl) and | - | ||||||||||||
4681 | also used by the QObjectPrivate::connect version used by QML. The signal_index is expected to be relative | - | ||||||||||||
4682 | to the number of signals. | - | ||||||||||||
4683 | */ | - | ||||||||||||
4684 | QMetaObject::Connection QObjectPrivate::connectImpl(const QObject *sender, int signal_index, | - | ||||||||||||
4685 | const QObject *receiver, void **slot, | - | ||||||||||||
4686 | QtPrivate::QSlotObjectBase *slotObj, Qt::ConnectionType type, | - | ||||||||||||
4687 | const int *types, const QMetaObject *senderMetaObject) | - | ||||||||||||
4688 | { | - | ||||||||||||
4689 | if (!sender || !slotObj || !senderMetaObject) { | - | ||||||||||||
4690 | qWarning("QObject::connect: invalid null parameter"); | - | ||||||||||||
4691 | if (slotObj) | - | ||||||||||||
4692 | slotObj->destroyIfLastRef(); | - | ||||||||||||
4693 | return QMetaObject::Connection(); | - | ||||||||||||
4694 | } | - | ||||||||||||
4695 | - | |||||||||||||
4696 | QObject *s = const_cast<QObject *>(sender); | - | ||||||||||||
4697 | QObject *r = const_cast<QObject *>(receiver); | - | ||||||||||||
4698 | - | |||||||||||||
4699 | QOrderedMutexLocker locker(signalSlotLock(sender), | - | ||||||||||||
4700 | signalSlotLock(receiver)); | - | ||||||||||||
4701 | - | |||||||||||||
4702 | if (type & Qt::UniqueConnection && slot) { | - | ||||||||||||
4703 | QObjectConnectionListVector *connectionLists = QObjectPrivate::get(s)->connectionLists; | - | ||||||||||||
4704 | if (connectionLists && connectionLists->count() > signal_index) { | - | ||||||||||||
4705 | const QObjectPrivate::Connection *c2 = | - | ||||||||||||
4706 | (*connectionLists)[signal_index].first; | - | ||||||||||||
4707 | - | |||||||||||||
4708 | while (c2) { | - | ||||||||||||
4709 | if (c2->receiver == receiver && c2->isSlotObject && c2->slotObj->compare(slot)) { | - | ||||||||||||
4710 | slotObj->destroyIfLastRef(); | - | ||||||||||||
4711 | return QMetaObject::Connection(); | - | ||||||||||||
4712 | } | - | ||||||||||||
4713 | c2 = c2->nextConnectionList; | - | ||||||||||||
4714 | } | - | ||||||||||||
4715 | } | - | ||||||||||||
4716 | type = static_cast<Qt::ConnectionType>(type ^ Qt::UniqueConnection); | - | ||||||||||||
4717 | } | - | ||||||||||||
4718 | - | |||||||||||||
4719 | QScopedPointer<QObjectPrivate::Connection> c(new QObjectPrivate::Connection); | - | ||||||||||||
4720 | c->sender = s; | - | ||||||||||||
4721 | c->signal_index = signal_index; | - | ||||||||||||
4722 | c->receiver = r; | - | ||||||||||||
4723 | c->slotObj = slotObj; | - | ||||||||||||
4724 | c->connectionType = type; | - | ||||||||||||
4725 | c->isSlotObject = true; | - | ||||||||||||
4726 | if (types) { | - | ||||||||||||
4727 | c->argumentTypes.store(types); | - | ||||||||||||
4728 | c->ownArgumentTypes = false; | - | ||||||||||||
4729 | } | - | ||||||||||||
4730 | - | |||||||||||||
4731 | QObjectPrivate::get(s)->addConnection(signal_index, c.data()); | - | ||||||||||||
4732 | QMetaObject::Connection ret(c.take()); | - | ||||||||||||
4733 | locker.unlock(); | - | ||||||||||||
4734 | - | |||||||||||||
4735 | QMetaMethod method = QMetaObjectPrivate::signal(senderMetaObject, signal_index); | - | ||||||||||||
4736 | Q_ASSERT(method.isValid()); | - | ||||||||||||
4737 | s->connectNotify(method); | - | ||||||||||||
4738 | - | |||||||||||||
4739 | return ret; | - | ||||||||||||
4740 | } | - | ||||||||||||
4741 | - | |||||||||||||
4742 | /*! | - | ||||||||||||
4743 | Disconnect a connection. | - | ||||||||||||
4744 | - | |||||||||||||
4745 | If the \a connection is invalid or has already been disconnected, do nothing | - | ||||||||||||
4746 | and return false. | - | ||||||||||||
4747 | - | |||||||||||||
4748 | \sa connect() | - | ||||||||||||
4749 | */ | - | ||||||||||||
4750 | bool QObject::disconnect(const QMetaObject::Connection &connection) | - | ||||||||||||
4751 | { | - | ||||||||||||
4752 | QObjectPrivate::Connection *c = static_cast<QObjectPrivate::Connection *>(connection.d_ptr); | - | ||||||||||||
4753 | - | |||||||||||||
4754 | if (!c || !c->receiver)
| 1-11 | ||||||||||||
4755 | return false; executed 4 times by 1 test: return false; Executed by:
| 4 | ||||||||||||
4756 | - | |||||||||||||
4757 | QMutex *senderMutex = signalSlotLock(c->sender); | - | ||||||||||||
4758 | QMutex *receiverMutex = signalSlotLock(c->receiver); | - | ||||||||||||
4759 | - | |||||||||||||
4760 | { | - | ||||||||||||
4761 | QOrderedMutexLocker locker(senderMutex, receiverMutex); | - | ||||||||||||
4762 | - | |||||||||||||
4763 | QObjectConnectionListVector *connectionLists = QObjectPrivate::get(c->sender)->connectionLists; | - | ||||||||||||
4764 | Q_ASSERT(connectionLists); | - | ||||||||||||
4765 | connectionLists->dirty = true; | - | ||||||||||||
4766 | - | |||||||||||||
4767 | *c->prev = c->next; | - | ||||||||||||
4768 | if (c->next)
| 1-9 | ||||||||||||
4769 | c->next->prev = c->prev; executed 1 time by 1 test: c->next->prev = c->prev; Executed by:
| 1 | ||||||||||||
4770 | c->receiver = 0; | - | ||||||||||||
4771 | } | - | ||||||||||||
4772 | - | |||||||||||||
4773 | // destroy the QSlotObject, if possible | - | ||||||||||||
4774 | if (c->isSlotObject) {
| 0-10 | ||||||||||||
4775 | c->slotObj->destroyIfLastRef(); | - | ||||||||||||
4776 | c->isSlotObject = false; | - | ||||||||||||
4777 | } executed 10 times by 1 test: end of block Executed by:
| 10 | ||||||||||||
4778 | - | |||||||||||||
4779 | c->sender->disconnectNotify(QMetaObjectPrivate::signal(c->sender->metaObject(), | - | ||||||||||||
c->signal_index));const_cast<QMetaObject::Connection &>(connection).d_ptr = 0; | ||||||||||||||
4780 | c->deref(); // has been removed from the QMetaObject::Connection object | - | ||||||||||||
4781 | - | |||||||||||||
4782 | // disconnectNotify() not called (the signal index is unknown). | - | ||||||||||||
4783 | - | |||||||||||||
4784 | return true; executed 10 times by 1 test: return true; Executed by:
| 10 | ||||||||||||
4785 | } | - | ||||||||||||
4786 | - | |||||||||||||
4787 | /*! \fn bool QObject::disconnect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method) | - | ||||||||||||
4788 | \overload diconnect() | - | ||||||||||||
4789 | \threadsafe | - | ||||||||||||
4790 | - | |||||||||||||
4791 | Disconnects \a signal in object \a sender from \a method in object | - | ||||||||||||
4792 | \a receiver. Returns \c true if the connection is successfully broken; | - | ||||||||||||
4793 | otherwise returns \c false. | - | ||||||||||||
4794 | - | |||||||||||||
4795 | A signal-slot connection is removed when either of the objects | - | ||||||||||||
4796 | involved are destroyed. | - | ||||||||||||
4797 | - | |||||||||||||
4798 | disconnect() is typically used in three ways, as the following | - | ||||||||||||
4799 | examples demonstrate. | - | ||||||||||||
4800 | \list 1 | - | ||||||||||||
4801 | \li Disconnect everything connected to an object's signals: | - | ||||||||||||
4802 | - | |||||||||||||
4803 | \snippet code/src_corelib_kernel_qobject.cpp 26 | - | ||||||||||||
4804 | - | |||||||||||||
4805 | \li Disconnect everything connected to a specific signal: | - | ||||||||||||
4806 | - | |||||||||||||
4807 | \snippet code/src_corelib_kernel_qobject.cpp 47 | - | ||||||||||||
4808 | - | |||||||||||||
4809 | \li Disconnect a specific receiver: | - | ||||||||||||
4810 | - | |||||||||||||
4811 | \snippet code/src_corelib_kernel_qobject.cpp 30 | - | ||||||||||||
4812 | - | |||||||||||||
4813 | \li Disconnect a connection from one specific signal to a specific slot: | - | ||||||||||||
4814 | - | |||||||||||||
4815 | \snippet code/src_corelib_kernel_qobject.cpp 48 | - | ||||||||||||
4816 | - | |||||||||||||
4817 | - | |||||||||||||
4818 | \endlist | - | ||||||||||||
4819 | - | |||||||||||||
4820 | 0 may be used as a wildcard, meaning "any signal", "any receiving | - | ||||||||||||
4821 | object", or "any slot in the receiving object", respectively. | - | ||||||||||||
4822 | - | |||||||||||||
4823 | The \a sender may never be 0. (You cannot disconnect signals from | - | ||||||||||||
4824 | more than one object in a single call.) | - | ||||||||||||
4825 | - | |||||||||||||
4826 | If \a signal is 0, it disconnects \a receiver and \a method from | - | ||||||||||||
4827 | any signal. If not, only the specified signal is disconnected. | - | ||||||||||||
4828 | - | |||||||||||||
4829 | If \a receiver is 0, it disconnects anything connected to \a | - | ||||||||||||
4830 | signal. If not, slots in objects other than \a receiver are not | - | ||||||||||||
4831 | disconnected. | - | ||||||||||||
4832 | - | |||||||||||||
4833 | If \a method is 0, it disconnects anything that is connected to \a | - | ||||||||||||
4834 | receiver. If not, only slots named \a method will be disconnected, | - | ||||||||||||
4835 | and all other slots are left alone. The \a method must be 0 if \a | - | ||||||||||||
4836 | receiver is left out, so you cannot disconnect a | - | ||||||||||||
4837 | specifically-named slot on all objects. | - | ||||||||||||
4838 | - | |||||||||||||
4839 | \note It is not possible to use this overload to diconnect signals | - | ||||||||||||
4840 | connected to functors or lambda expressions. That is because it is not | - | ||||||||||||
4841 | possible to compare them. Instead, use the overload that takes a | - | ||||||||||||
4842 | QMetaObject::Connection | - | ||||||||||||
4843 | - | |||||||||||||
4844 | \sa connect() | - | ||||||||||||
4845 | */ | - | ||||||||||||
4846 | - | |||||||||||||
4847 | bool QObject::disconnectImpl(const QObject *sender, void **signal, const QObject *receiver, void **slot, const QMetaObject *senderMetaObject) | - | ||||||||||||
4848 | { | - | ||||||||||||
4849 | if (sender == 0 || (receiver == 0 && slot != 0)) { | - | ||||||||||||
4850 | qWarning("QObject::disconnect: Unexpected null parameter"); | - | ||||||||||||
4851 | return false; | - | ||||||||||||
4852 | } | - | ||||||||||||
4853 | - | |||||||||||||
4854 | int signal_index = -1; | - | ||||||||||||
4855 | if (signal) { | - | ||||||||||||
4856 | void *args[] = { &signal_index, signal }; | - | ||||||||||||
4857 | for (; senderMetaObject && signal_index < 0; senderMetaObject = senderMetaObject->superClass()) { | - | ||||||||||||
4858 | senderMetaObject->static_metacall(QMetaObject::IndexOfMethod, 0, args); | - | ||||||||||||
4859 | if (signal_index >= 0 && signal_index < QMetaObjectPrivate::get(senderMetaObject)->signalCount) | - | ||||||||||||
4860 | break; | - | ||||||||||||
4861 | } | - | ||||||||||||
4862 | if (!senderMetaObject) { | - | ||||||||||||
4863 | qWarning("QObject::disconnect: signal not found in %s", sender->metaObject()->className()); | - | ||||||||||||
4864 | return QMetaObject::Connection(0); | - | ||||||||||||
4865 | } | - | ||||||||||||
4866 | signal_index += QMetaObjectPrivate::signalOffset(senderMetaObject); | - | ||||||||||||
4867 | } | - | ||||||||||||
4868 | - | |||||||||||||
4869 | return QMetaObjectPrivate::disconnect(sender, signal_index, senderMetaObject, receiver, -1, slot); | - | ||||||||||||
4870 | } | - | ||||||||||||
4871 | - | |||||||||||||
4872 | /*! | - | ||||||||||||
4873 | \internal | - | ||||||||||||
4874 | Used by QML to connect a signal by index to a slot implemented in JavaScript (wrapped in a custom QSlotOBjectBase subclass). | - | ||||||||||||
4875 | - | |||||||||||||
4876 | The signal_index is an index relative to the number of methods. | - | ||||||||||||
4877 | */ | - | ||||||||||||
4878 | QMetaObject::Connection QObjectPrivate::connect(const QObject *sender, int signal_index, QtPrivate::QSlotObjectBase *slotObj, Qt::ConnectionType type) | - | ||||||||||||
4879 | { | - | ||||||||||||
4880 | if (!sender) { | - | ||||||||||||
4881 | qWarning("QObject::connect: invalid null parameter"); | - | ||||||||||||
4882 | if (slotObj) | - | ||||||||||||
4883 | slotObj->destroyIfLastRef(); | - | ||||||||||||
4884 | return QMetaObject::Connection(); | - | ||||||||||||
4885 | } | - | ||||||||||||
4886 | const QMetaObject *senderMetaObject = sender->metaObject(); | - | ||||||||||||
4887 | signal_index = methodIndexToSignalIndex(&senderMetaObject, signal_index); | - | ||||||||||||
4888 | - | |||||||||||||
4889 | return QObjectPrivate::connectImpl(sender, signal_index, sender, /*slot*/0, slotObj, type, /*types*/0, senderMetaObject); | - | ||||||||||||
4890 | } | - | ||||||||||||
4891 | - | |||||||||||||
4892 | /*! | - | ||||||||||||
4893 | \internal | - | ||||||||||||
4894 | Used by QML to disconnect a signal by index that's connected to a slot implemented in JavaScript (wrapped in a custom QSlotObjectBase subclass) | - | ||||||||||||
4895 | In the QML case the slot is not a pointer to a pointer to the function to disconnect, but instead it is a pointer to an array of internal values | - | ||||||||||||
4896 | required for the disconnect. | - | ||||||||||||
4897 | */ | - | ||||||||||||
4898 | bool QObjectPrivate::disconnect(const QObject *sender, int signal_index, void **slot) | - | ||||||||||||
4899 | { | - | ||||||||||||
4900 | const QMetaObject *senderMetaObject = sender->metaObject(); | - | ||||||||||||
4901 | signal_index = methodIndexToSignalIndex(&senderMetaObject, signal_index); | - | ||||||||||||
4902 | - | |||||||||||||
4903 | return QMetaObjectPrivate::disconnect(sender, signal_index, senderMetaObject, sender, -1, slot); | - | ||||||||||||
4904 | } | - | ||||||||||||
4905 | - | |||||||||||||
4906 | /*! \class QMetaObject::Connection | - | ||||||||||||
4907 | \inmodule QtCore | - | ||||||||||||
4908 | Represents a handle to a signal-slot connection. | - | ||||||||||||
4909 | It can be used to disconnect that connection, or check if | - | ||||||||||||
4910 | the connection was successful | - | ||||||||||||
4911 | - | |||||||||||||
4912 | \sa QObject::disconnect() | - | ||||||||||||
4913 | */ | - | ||||||||||||
4914 | - | |||||||||||||
4915 | /*! | - | ||||||||||||
4916 | Create a copy of the handle to the connection | - | ||||||||||||
4917 | */ | - | ||||||||||||
4918 | QMetaObject::Connection::Connection(const QMetaObject::Connection &other) : d_ptr(other.d_ptr) | - | ||||||||||||
4919 | { | - | ||||||||||||
4920 | if (d_ptr) | - | ||||||||||||
4921 | static_cast<QObjectPrivate::Connection *>(d_ptr)->ref(); | - | ||||||||||||
4922 | } | - | ||||||||||||
4923 | - | |||||||||||||
4924 | QMetaObject::Connection& QMetaObject::Connection::operator=(const QMetaObject::Connection& other) | - | ||||||||||||
4925 | { | - | ||||||||||||
4926 | if (other.d_ptr != d_ptr) { | - | ||||||||||||
4927 | if (d_ptr) | - | ||||||||||||
4928 | static_cast<QObjectPrivate::Connection *>(d_ptr)->deref(); | - | ||||||||||||
4929 | d_ptr = other.d_ptr; | - | ||||||||||||
4930 | if (other.d_ptr) | - | ||||||||||||
4931 | static_cast<QObjectPrivate::Connection *>(other.d_ptr)->ref(); | - | ||||||||||||
4932 | } | - | ||||||||||||
4933 | return *this; | - | ||||||||||||
4934 | } | - | ||||||||||||
4935 | - | |||||||||||||
4936 | /*! | - | ||||||||||||
4937 | Creates a Connection instance. | - | ||||||||||||
4938 | */ | - | ||||||||||||
4939 | - | |||||||||||||
4940 | QMetaObject::Connection::Connection() : d_ptr(0) {} | - | ||||||||||||
4941 | - | |||||||||||||
4942 | /*! | - | ||||||||||||
4943 | Destructor for QMetaObject::Connection. | - | ||||||||||||
4944 | */ | - | ||||||||||||
4945 | QMetaObject::Connection::~Connection() | - | ||||||||||||
4946 | { | - | ||||||||||||
4947 | if (d_ptr) | - | ||||||||||||
4948 | static_cast<QObjectPrivate::Connection *>(d_ptr)->deref(); | - | ||||||||||||
4949 | } | - | ||||||||||||
4950 | - | |||||||||||||
4951 | /*! \internal Returns true if the object is still connected */ | - | ||||||||||||
4952 | bool QMetaObject::Connection::isConnected_helper() const | - | ||||||||||||
4953 | { | - | ||||||||||||
4954 | Q_ASSERT(d_ptr); // we're only called from operator RestrictedBool() const | - | ||||||||||||
4955 | QObjectPrivate::Connection *c = static_cast<QObjectPrivate::Connection *>(d_ptr); | - | ||||||||||||
4956 | - | |||||||||||||
4957 | return c->receiver; | - | ||||||||||||
4958 | } | - | ||||||||||||
4959 | - | |||||||||||||
4960 | - | |||||||||||||
4961 | /*! | - | ||||||||||||
4962 | \fn QMetaObject::Connection::operator bool() const | - | ||||||||||||
4963 | - | |||||||||||||
4964 | Returns \c true if the connection is valid. | - | ||||||||||||
4965 | - | |||||||||||||
4966 | The connection is valid if the call to QObject::connect succeeded. | - | ||||||||||||
4967 | The connection is invalid if QObject::connect was not able to find | - | ||||||||||||
4968 | the signal or the slot, or if the arguments do not match. | - | ||||||||||||
4969 | */ | - | ||||||||||||
4970 | - | |||||||||||||
4971 | QT_END_NAMESPACE | - | ||||||||||||
4972 | - | |||||||||||||
4973 | #include "moc_qobject.cpp" | - | ||||||||||||
Source code | Switch to Preprocessed file |