source: trunk/src/corelib/kernel/qobject.h@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 15.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QOBJECT_H
43#define QOBJECT_H
44
45#ifndef QT_NO_QOBJECT
46
47#include <QtCore/qobjectdefs.h>
48#include <QtCore/qstring.h>
49#include <QtCore/qbytearray.h>
50#include <QtCore/qlist.h>
51#ifdef QT_INCLUDE_COMPAT
52#include <QtCore/qcoreevent.h>
53#endif
54#include <QtCore/qscopedpointer.h>
55
56QT_BEGIN_HEADER
57
58QT_BEGIN_NAMESPACE
59
60QT_MODULE(Core)
61
62class QEvent;
63class QTimerEvent;
64class QChildEvent;
65struct QMetaObject;
66class QVariant;
67class QObjectPrivate;
68class QObject;
69class QThread;
70class QWidget;
71#ifndef QT_NO_REGEXP
72class QRegExp;
73#endif
74#ifndef QT_NO_USERDATA
75class QObjectUserData;
76#endif
77
78typedef QList<QObject*> QObjectList;
79
80#if defined Q_CC_MSVC && _MSC_VER < 1300
81template<typename T> inline T qFindChild(const QObject *o, const QString &name = QString(), T = 0);
82template<typename T> inline QList<T> qFindChildren(const QObject *o, const QString &name = QString(), T = 0);
83# ifndef QT_NO_REGEXP
84template<typename T> inline QList<T> qFindChildren(const QObject *o, const QRegExp &re, T = 0);
85# endif
86#else
87template<typename T> inline T qFindChild(const QObject *, const QString & = QString());
88template<typename T> inline QList<T> qFindChildren(const QObject *, const QString & = QString());
89# ifndef QT_NO_REGEXP
90template<typename T> inline QList<T> qFindChildren(const QObject *, const QRegExp &);
91# endif
92#endif
93
94class
95#if defined(__INTEL_COMPILER) && defined(Q_OS_WIN)
96Q_CORE_EXPORT
97#endif
98QObjectData {
99public:
100 virtual ~QObjectData() = 0;
101 QObject *q_ptr;
102 QObject *parent;
103 QObjectList children;
104
105 uint isWidget : 1;
106 uint pendTimer : 1;
107 uint blockSig : 1;
108 uint wasDeleted : 1;
109 uint ownObjectName : 1;
110 uint sendChildEvents : 1;
111 uint receiveChildEvents : 1;
112 uint inEventHandler : 1;
113 uint inThreadChangeEvent : 1;
114 uint hasGuards : 1; //true iff there is one or more QPointer attached to this object
115 uint unused : 22;
116 int postedEvents;
117 QMetaObject *metaObject; // assert dynamic
118};
119
120
121class Q_CORE_EXPORT QObject
122{
123 Q_OBJECT
124 Q_PROPERTY(QString objectName READ objectName WRITE setObjectName)
125 Q_DECLARE_PRIVATE(QObject)
126
127public:
128 Q_INVOKABLE explicit QObject(QObject *parent=0);
129 virtual ~QObject();
130
131 virtual bool event(QEvent *);
132 virtual bool eventFilter(QObject *, QEvent *);
133
134#ifdef qdoc
135 static QString tr(const char *sourceText, const char *comment = 0, int n = -1);
136 static QString trUtf8(const char *sourceText, const char *comment = 0, int n = -1);
137 virtual const QMetaObject *metaObject() const;
138 static const QMetaObject staticMetaObject;
139#endif
140#ifdef QT_NO_TRANSLATION
141 static QString tr(const char *sourceText, const char *, int)
142 { return QString::fromLatin1(sourceText); }
143 static QString tr(const char *sourceText, const char * = 0)
144 { return QString::fromLatin1(sourceText); }
145#ifndef QT_NO_TEXTCODEC
146 static QString trUtf8(const char *sourceText, const char *, int)
147 { return QString::fromUtf8(sourceText); }
148 static QString trUtf8(const char *sourceText, const char * = 0)
149 { return QString::fromUtf8(sourceText); }
150#endif
151#endif //QT_NO_TRANSLATION
152
153 QString objectName() const;
154 void setObjectName(const QString &name);
155
156 inline bool isWidgetType() const { return d_ptr->isWidget; }
157
158 inline bool signalsBlocked() const { return d_ptr->blockSig; }
159 bool blockSignals(bool b);
160
161 QThread *thread() const;
162 void moveToThread(QThread *thread);
163
164 int startTimer(int interval);
165 void killTimer(int id);
166
167#ifndef QT_NO_MEMBER_TEMPLATES
168 template<typename T>
169 inline T findChild(const QString &aName = QString()) const
170 { return qFindChild<T>(this, aName); }
171
172 template<typename T>
173 inline QList<T> findChildren(const QString &aName = QString()) const
174 { return qFindChildren<T>(this, aName); }
175
176#ifndef QT_NO_REGEXP
177 template<typename T>
178 inline QList<T> findChildren(const QRegExp &re) const
179 { return qFindChildren<T>(this, re); }
180#endif
181#endif
182
183#ifdef QT3_SUPPORT
184 QT3_SUPPORT QObject *child(const char *objName, const char *inheritsClass = 0,
185 bool recursiveSearch = true) const;
186 QT3_SUPPORT QObjectList queryList(const char *inheritsClass = 0,
187 const char *objName = 0,
188 bool regexpMatch = true,
189 bool recursiveSearch = true) const;
190#endif
191 inline const QObjectList &children() const { return d_ptr->children; }
192
193 void setParent(QObject *);
194 void installEventFilter(QObject *);
195 void removeEventFilter(QObject *);
196
197
198 static bool connect(const QObject *sender, const char *signal,
199 const QObject *receiver, const char *member, Qt::ConnectionType =
200#ifdef qdoc
201 Qt::AutoConnection
202#else
203#ifdef QT3_SUPPORT
204 Qt::AutoCompatConnection
205#else
206 Qt::AutoConnection
207#endif
208#endif
209 );
210 inline bool connect(const QObject *sender, const char *signal,
211 const char *member, Qt::ConnectionType type =
212#ifdef qdoc
213 Qt::AutoConnection
214#else
215#ifdef QT3_SUPPORT
216 Qt::AutoCompatConnection
217#else
218 Qt::AutoConnection
219#endif
220#endif
221 ) const;
222
223 static bool disconnect(const QObject *sender, const char *signal,
224 const QObject *receiver, const char *member);
225 inline bool disconnect(const char *signal = 0,
226 const QObject *receiver = 0, const char *member = 0)
227 { return disconnect(this, signal, receiver, member); }
228 inline bool disconnect(const QObject *receiver, const char *member = 0)
229 { return disconnect(this, 0, receiver, member); }
230
231 void dumpObjectTree();
232 void dumpObjectInfo();
233
234#ifndef QT_NO_PROPERTIES
235 bool setProperty(const char *name, const QVariant &value);
236 QVariant property(const char *name) const;
237 QList<QByteArray> dynamicPropertyNames() const;
238#endif // QT_NO_PROPERTIES
239
240#ifndef QT_NO_USERDATA
241 static uint registerUserData();
242 void setUserData(uint id, QObjectUserData* data);
243 QObjectUserData* userData(uint id) const;
244#endif // QT_NO_USERDATA
245
246Q_SIGNALS:
247 void destroyed(QObject * = 0);
248
249public:
250 inline QObject *parent() const { return d_ptr->parent; }
251
252 inline bool inherits(const char *classname) const
253 { return const_cast<QObject *>(this)->qt_metacast(classname) != 0; }
254
255public Q_SLOTS:
256 void deleteLater();
257
258protected:
259 QObject *sender() const;
260 int receivers(const char* signal) const;
261
262 virtual void timerEvent(QTimerEvent *);
263 virtual void childEvent(QChildEvent *);
264 virtual void customEvent(QEvent *);
265
266 virtual void connectNotify(const char *signal);
267 virtual void disconnectNotify(const char *signal);
268
269#ifdef QT3_SUPPORT
270public:
271 QT3_SUPPORT_CONSTRUCTOR QObject(QObject *parent, const char *name);
272 inline QT3_SUPPORT void insertChild(QObject *o)
273 { if (o) o->setParent(this); }
274 inline QT3_SUPPORT void removeChild(QObject *o)
275 { if (o) o->setParent(0); }
276 inline QT3_SUPPORT bool isA(const char *classname) const
277 { return qstrcmp(classname, metaObject()->className()) == 0; }
278 inline QT3_SUPPORT const char *className() const { return metaObject()->className(); }
279 inline QT3_SUPPORT const char *name() const { return objectName().latin1_helper(); }
280 inline QT3_SUPPORT const char *name(const char *defaultName) const
281 { QString s = objectName(); return s.isEmpty()?defaultName:s.latin1_helper(); }
282 inline QT3_SUPPORT void setName(const char *aName) { setObjectName(QLatin1String(aName)); }
283protected:
284 inline QT3_SUPPORT bool checkConnectArgs(const char *signal,
285 const QObject *,
286 const char *member)
287 { return QMetaObject::checkConnectArgs(signal, member); }
288 static inline QT3_SUPPORT QByteArray normalizeSignalSlot(const char *signalSlot)
289 { return QMetaObject::normalizedSignature(signalSlot); }
290#endif
291
292protected:
293 QObject(QObjectPrivate &dd, QObject *parent = 0);
294
295protected:
296 QScopedPointer<QObjectData> d_ptr;
297
298 static const QMetaObject staticQtMetaObject;
299
300 friend struct QMetaObject;
301 friend class QApplication;
302 friend class QApplicationPrivate;
303 friend class QCoreApplication;
304 friend class QCoreApplicationPrivate;
305 friend class QWidget;
306 friend class QThreadData;
307
308private:
309 Q_DISABLE_COPY(QObject)
310 Q_PRIVATE_SLOT(d_func(), void _q_reregisterTimers(void *))
311};
312
313inline bool QObject::connect(const QObject *asender, const char *asignal,
314 const char *amember, Qt::ConnectionType atype) const
315{ return connect(asender, asignal, this, amember, atype); }
316
317#ifndef QT_NO_USERDATA
318class Q_CORE_EXPORT QObjectUserData {
319public:
320 virtual ~QObjectUserData();
321};
322#endif
323
324Q_CORE_EXPORT void qt_qFindChildren_helper(const QObject *parent, const QString &name, const QRegExp *re,
325 const QMetaObject &mo, QList<void *> *list);
326Q_CORE_EXPORT QObject *qt_qFindChild_helper(const QObject *parent, const QString &name, const QMetaObject &mo);
327
328#if defined Q_CC_MSVC && _MSC_VER < 1300
329
330template<typename T>
331inline T qFindChild(const QObject *o, const QString &name, T)
332{ return static_cast<T>(qt_qFindChild_helper(o, name, ((T)0)->staticMetaObject)); }
333
334template<typename T>
335inline QList<T> qFindChildren(const QObject *o, const QString &name, T)
336{
337 QList<T> list;
338 union {
339 QList<T> *typedList;
340 QList<void *> *voidList;
341 } u;
342 u.typedList = &list;
343 qt_qFindChildren_helper(o, name, 0, ((T)0)->staticMetaObject, u.voidList);
344 return list;
345}
346
347template<typename T>
348inline T qFindChild(const QObject *o, const QString &name)
349{ return qFindChild<T>(o, name, T(0)); }
350
351template<typename T>
352inline T qFindChild(const QObject *o)
353{ return qFindChild<T>(o, QString(), T(0)); }
354
355template<typename T>
356inline QList<T> qFindChildren(const QObject *o, const QString &name)
357{ return qFindChildren<T>(o, name, T(0)); }
358
359template<typename T>
360inline QList<T> qFindChildren(const QObject *o)
361{ return qFindChildren<T>(o, QString(), T(0)); }
362
363#ifndef QT_NO_REGEXP
364template<typename T>
365inline QList<T> qFindChildren(const QObject *o, const QRegExp &re, T)
366{
367 QList<T> list;
368 union {
369 QList<T> *typedList;
370 QList<void *> *voidList;
371 } u;
372 u.typedList = &list;
373 qt_qFindChildren_helper(o, 0, &re, ((T)0)->staticMetaObject, u.voidList);
374 return list;
375}
376
377template<typename T>
378inline QList<T> qFindChildren(const QObject *o, const QRegExp &re)
379{ return qFindChildren<T>(o, re, T(0)); }
380
381#endif
382
383#ifdef Q_MOC_RUN
384# define Q_DECLARE_INTERFACE(IFace, IId) Q_DECLARE_INTERFACE(IFace, IId)
385#endif // Q_MOC_RUN
386
387
388template <class T> inline const char * qobject_interface_iid()
389{ return 0; }
390
391template <class T> inline T qobject_cast_helper(QObject *object, T)
392{ return static_cast<T>(((T)0)->staticMetaObject.cast(object)); }
393
394template <class T> inline T qobject_cast_helper(const QObject *object, T)
395{ return static_cast<T>(const_cast<const QObject *>(((T)0)->staticMetaObject.cast(const_cast<QObject *>(object)))); }
396
397template <class T>
398inline T qobject_cast(QObject *object)
399{ return qobject_cast_helper<T>(object, T(0)); }
400
401template <class T>
402inline T qobject_cast(const QObject *object)
403{ return qobject_cast_helper<T>(object, T(0)); }
404
405#ifndef Q_MOC_RUN
406# define Q_DECLARE_INTERFACE(IFace, IId) \
407 template <> inline const char *qobject_interface_iid<IFace *>() \
408 { return IId; } \
409 template <> inline IFace *qobject_cast_helper<IFace *>(QObject *object, IFace *) \
410 { return (IFace *)(object ? object->qt_metacast(IId) : 0); } \
411 template <> inline IFace *qobject_cast_helper<IFace *>(const QObject *object, IFace *) \
412 { return (IFace *)(object ? const_cast<QObject *>(object)->qt_metacast(IId) : 0); }
413#endif // Q_MOC_RUN
414
415#else
416
417template<typename T>
418inline T qFindChild(const QObject *o, const QString &name)
419{ return static_cast<T>(qt_qFindChild_helper(o, name, reinterpret_cast<T>(0)->staticMetaObject)); }
420
421template<typename T>
422inline QList<T> qFindChildren(const QObject *o, const QString &name)
423{
424 QList<T> list;
425 union {
426 QList<T> *typedList;
427 QList<void *> *voidList;
428 } u;
429 u.typedList = &list;
430 qt_qFindChildren_helper(o, name, 0, reinterpret_cast<T>(0)->staticMetaObject, u.voidList);
431 return list;
432}
433
434#ifndef QT_NO_REGEXP
435template<typename T>
436inline QList<T> qFindChildren(const QObject *o, const QRegExp &re)
437{
438 QList<T> list;
439 union {
440 QList<T> *typedList;
441 QList<void *> *voidList;
442 } u;
443 u.typedList = &list;
444 qt_qFindChildren_helper(o, QString(), &re, reinterpret_cast<T>(0)->staticMetaObject, u.voidList);
445 return list;
446}
447#endif
448
449template <class T>
450inline T qobject_cast(QObject *object)
451{
452#if !defined(QT_NO_MEMBER_TEMPLATES) && !defined(QT_NO_QOBJECT_CHECK)
453 reinterpret_cast<T>(0)->qt_check_for_QOBJECT_macro(*reinterpret_cast<T>(object));
454#endif
455 return static_cast<T>(reinterpret_cast<T>(0)->staticMetaObject.cast(object));
456}
457
458template <class T>
459inline T qobject_cast(const QObject *object)
460{
461 // this will cause a compilation error if T is not const
462 register T ptr = static_cast<T>(object);
463 Q_UNUSED(ptr);
464
465#if !defined(QT_NO_MEMBER_TEMPLATES) && !defined(QT_NO_QOBJECT_CHECK)
466 reinterpret_cast<T>(0)->qt_check_for_QOBJECT_macro(*reinterpret_cast<T>(const_cast<QObject *>(object)));
467#endif
468 return static_cast<T>(const_cast<QObject *>(reinterpret_cast<T>(0)->staticMetaObject.cast(const_cast<QObject *>(object))));
469}
470
471
472template <class T> inline const char * qobject_interface_iid()
473{ return 0; }
474
475#ifndef Q_MOC_RUN
476# define Q_DECLARE_INTERFACE(IFace, IId) \
477 template <> inline const char *qobject_interface_iid<IFace *>() \
478 { return IId; } \
479 template <> inline IFace *qobject_cast<IFace *>(QObject *object) \
480 { return reinterpret_cast<IFace *>((object ? object->qt_metacast(IId) : 0)); } \
481 template <> inline IFace *qobject_cast<IFace *>(const QObject *object) \
482 { return reinterpret_cast<IFace *>((object ? const_cast<QObject *>(object)->qt_metacast(IId) : 0)); }
483#endif // Q_MOC_RUN
484
485#endif
486
487#ifndef QT_NO_DEBUG_STREAM
488Q_CORE_EXPORT QDebug operator<<(QDebug, const QObject *);
489#endif
490
491QT_END_NAMESPACE
492
493QT_END_HEADER
494
495#endif
496
497#endif // QOBJECT_H
Note: See TracBrowser for help on using the repository browser.