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

Last change on this file since 72 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

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