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

Last change on this file since 866 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 13.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 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
328template<typename T>
329inline T qFindChild(const QObject *o, const QString &name)
330{ return static_cast<T>(qt_qFindChild_helper(o, name, reinterpret_cast<T>(0)->staticMetaObject)); }
331
332template<typename T>
333inline QList<T> qFindChildren(const QObject *o, const QString &name)
334{
335 QList<T> list;
336 union {
337 QList<T> *typedList;
338 QList<void *> *voidList;
339 } u;
340 u.typedList = &list;
341 qt_qFindChildren_helper(o, name, 0, reinterpret_cast<T>(0)->staticMetaObject, u.voidList);
342 return list;
343}
344
345#ifndef QT_NO_REGEXP
346template<typename T>
347inline QList<T> qFindChildren(const QObject *o, const QRegExp &re)
348{
349 QList<T> list;
350 union {
351 QList<T> *typedList;
352 QList<void *> *voidList;
353 } u;
354 u.typedList = &list;
355 qt_qFindChildren_helper(o, QString(), &re, reinterpret_cast<T>(0)->staticMetaObject, u.voidList);
356 return list;
357}
358#endif
359
360template <class T>
361inline T qobject_cast(QObject *object)
362{
363#if !defined(QT_NO_MEMBER_TEMPLATES) && !defined(QT_NO_QOBJECT_CHECK)
364 reinterpret_cast<T>(0)->qt_check_for_QOBJECT_macro(*reinterpret_cast<T>(object));
365#endif
366 return static_cast<T>(reinterpret_cast<T>(0)->staticMetaObject.cast(object));
367}
368
369template <class T>
370inline T qobject_cast(const QObject *object)
371{
372 // this will cause a compilation error if T is not const
373 register T ptr = static_cast<T>(object);
374 Q_UNUSED(ptr);
375
376#if !defined(QT_NO_MEMBER_TEMPLATES) && !defined(QT_NO_QOBJECT_CHECK)
377 reinterpret_cast<T>(0)->qt_check_for_QOBJECT_macro(*reinterpret_cast<T>(const_cast<QObject *>(object)));
378#endif
379 return static_cast<T>(const_cast<QObject *>(reinterpret_cast<T>(0)->staticMetaObject.cast(const_cast<QObject *>(object))));
380}
381
382
383template <class T> inline const char * qobject_interface_iid()
384{ return 0; }
385
386#ifndef Q_MOC_RUN
387# define Q_DECLARE_INTERFACE(IFace, IId) \
388 template <> inline const char *qobject_interface_iid<IFace *>() \
389 { return IId; } \
390 template <> inline IFace *qobject_cast<IFace *>(QObject *object) \
391 { return reinterpret_cast<IFace *>((object ? object->qt_metacast(IId) : 0)); } \
392 template <> inline IFace *qobject_cast<IFace *>(const QObject *object) \
393 { return reinterpret_cast<IFace *>((object ? const_cast<QObject *>(object)->qt_metacast(IId) : 0)); }
394#endif // Q_MOC_RUN
395
396#ifndef QT_NO_DEBUG_STREAM
397Q_CORE_EXPORT QDebug operator<<(QDebug, const QObject *);
398#endif
399
400QT_END_NAMESPACE
401
402QT_END_HEADER
403
404#endif
405
406#endif // QOBJECT_H
Note: See TracBrowser for help on using the repository browser.