QtCore/qobject.h Source File

qobject.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
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 
57 
59 
60 QT_MODULE(Core)
61 
62 class QEvent;
63 class QTimerEvent;
64 class QChildEvent;
65 struct QMetaObject;
66 class QVariant;
67 class QObjectPrivate;
68 class QObject;
69 class QThread;
70 class QWidget;
71 #ifndef QT_NO_REGEXP
72 class QRegExp;
73 #endif
74 #ifndef QT_NO_USERDATA
76 #endif
77 
79 
80 Q_CORE_EXPORT void qt_qFindChildren_helper(const QObject *parent, const QString &name, const QRegExp *re,
81  const QMetaObject &mo, QList<void *> *list);
82 Q_CORE_EXPORT QObject *qt_qFindChild_helper(const QObject *parent, const QString &name, const QMetaObject &mo);
83 
84 class
85 #if defined(__INTEL_COMPILER) && defined(Q_OS_WIN)
86 Q_CORE_EXPORT
87 #endif
89 public:
90  virtual ~QObjectData() = 0;
91  QObject *q_ptr;
92  QObject *parent;
93  QObjectList children;
94 
95  uint isWidget : 1;
96  uint pendTimer : 1;
97  uint blockSig : 1;
98  uint wasDeleted : 1;
99  uint ownObjectName : 1;
100  uint sendChildEvents : 1;
101  uint receiveChildEvents : 1;
102  uint inEventHandler : 1; //only used if QT_JAMBI_BUILD
103  uint inThreadChangeEvent : 1;
104  uint hasGuards : 1; //true iff there is one or more QPointer attached to this object
105  uint unused : 22;
107  QMetaObject *metaObject; // assert dynamic
108 };
109 
110 
111 class Q_CORE_EXPORT QObject
112 {
113  Q_OBJECT
114  Q_PROPERTY(QString objectName READ objectName WRITE setObjectName)
115  Q_DECLARE_PRIVATE(QObject)
116 
117 public:
118  Q_INVOKABLE explicit QObject(QObject *parent=0);
119  virtual ~QObject();
120 
121  virtual bool event(QEvent *);
122  virtual bool eventFilter(QObject *, QEvent *);
123 
124 #ifdef qdoc
125  static QString tr(const char *sourceText, const char *comment = 0, int n = -1);
126  static QString trUtf8(const char *sourceText, const char *comment = 0, int n = -1);
127  virtual const QMetaObject *metaObject() const;
128  static const QMetaObject staticMetaObject;
129 #endif
130 #ifdef QT_NO_TRANSLATION
131  static QString tr(const char *sourceText, const char *, int)
132  { return QString::fromLatin1(sourceText); }
133  static QString tr(const char *sourceText, const char * = 0)
134  { return QString::fromLatin1(sourceText); }
135 #ifndef QT_NO_TEXTCODEC
136  static QString trUtf8(const char *sourceText, const char *, int)
137  { return QString::fromUtf8(sourceText); }
138  static QString trUtf8(const char *sourceText, const char * = 0)
139  { return QString::fromUtf8(sourceText); }
140 #endif
141 #endif //QT_NO_TRANSLATION
142 
143  QString objectName() const;
144  void setObjectName(const QString &name);
145 
146  inline bool isWidgetType() const { return d_ptr->isWidget; }
147 
148  inline bool signalsBlocked() const { return d_ptr->blockSig; }
149  bool blockSignals(bool b);
150 
151  QThread *thread() const;
152  void moveToThread(QThread *thread);
153 
154  int startTimer(int interval);
155  void killTimer(int id);
156 
157  template<typename T>
158  inline T findChild(const QString &aName = QString()) const
159  { return static_cast<T>(qt_qFindChild_helper(this, aName, reinterpret_cast<T>(0)->staticMetaObject)); }
160 
161  template<typename T>
162  inline QList<T> findChildren(const QString &aName = QString()) const
163  {
164  QList<T> list;
165  union {
166  QList<T> *typedList;
167  QList<void *> *voidList;
168  } u;
169  u.typedList = &list;
170  qt_qFindChildren_helper(this, aName, 0, reinterpret_cast<T>(0)->staticMetaObject, u.voidList);
171  return list;
172  }
173 
174 #ifndef QT_NO_REGEXP
175  template<typename T>
176  inline QList<T> findChildren(const QRegExp &re) const
177  {
178  QList<T> list;
179  union {
180  QList<T> *typedList;
181  QList<void *> *voidList;
182  } u;
183  u.typedList = &list;
184  qt_qFindChildren_helper(this, QString(), &re, reinterpret_cast<T>(0)->staticMetaObject, u.voidList);
185  return list;
186  }
187 #endif
188 
189 #ifdef QT3_SUPPORT
190  QT3_SUPPORT QObject *child(const char *objName, const char *inheritsClass = 0,
191  bool recursiveSearch = true) const;
192  QT3_SUPPORT QObjectList queryList(const char *inheritsClass = 0,
193  const char *objName = 0,
194  bool regexpMatch = true,
195  bool recursiveSearch = true) const;
196 #endif
197  inline const QObjectList &children() const { return d_ptr->children; }
198 
199  void setParent(QObject *);
200  void installEventFilter(QObject *);
201  void removeEventFilter(QObject *);
202 
203 
204  static bool connect(const QObject *sender, const char *signal,
205  const QObject *receiver, const char *member, Qt::ConnectionType =
206 #ifdef qdoc
208 #else
209 #ifdef QT3_SUPPORT
211 #else
213 #endif
214 #endif
215  );
216 
217  static bool connect(const QObject *sender, const QMetaMethod &signal,
218  const QObject *receiver, const QMetaMethod &method,
220 #ifdef qdoc
222 #else
223 #ifdef QT3_SUPPORT
225 #else
227 #endif
228 #endif
229  );
230 
231  inline bool connect(const QObject *sender, const char *signal,
232  const char *member, Qt::ConnectionType type =
233 #ifdef qdoc
235 #else
236 #ifdef QT3_SUPPORT
238 #else
240 #endif
241 #endif
242  ) const;
243 
244  static bool disconnect(const QObject *sender, const char *signal,
245  const QObject *receiver, const char *member);
246  static bool disconnect(const QObject *sender, const QMetaMethod &signal,
247  const QObject *receiver, const QMetaMethod &member);
248  inline bool disconnect(const char *signal = 0,
249  const QObject *receiver = 0, const char *member = 0)
250  { return disconnect(this, signal, receiver, member); }
251  inline bool disconnect(const QObject *receiver, const char *member = 0)
252  { return disconnect(this, 0, receiver, member); }
253 
254  void dumpObjectTree();
255  void dumpObjectInfo();
256 
257 #ifndef QT_NO_PROPERTIES
258  bool setProperty(const char *name, const QVariant &value);
259  QVariant property(const char *name) const;
260  QList<QByteArray> dynamicPropertyNames() const;
261 #endif // QT_NO_PROPERTIES
262 
263 #ifndef QT_NO_USERDATA
264  static uint registerUserData();
265  void setUserData(uint id, QObjectUserData* data);
266  QObjectUserData* userData(uint id) const;
267 #endif // QT_NO_USERDATA
268 
269 Q_SIGNALS:
270  void destroyed(QObject * = 0);
271 
272 public:
273  inline QObject *parent() const { return d_ptr->parent; }
274 
275  inline bool inherits(const char *classname) const
276  { return const_cast<QObject *>(this)->qt_metacast(classname) != 0; }
277 
278 public Q_SLOTS:
279  void deleteLater();
280 
281 protected:
282  QObject *sender() const;
283  int senderSignalIndex() const;
284  int receivers(const char* signal) const;
285 
286  virtual void timerEvent(QTimerEvent *);
287  virtual void childEvent(QChildEvent *);
288  virtual void customEvent(QEvent *);
289 
290  virtual void connectNotify(const char *signal);
291  virtual void disconnectNotify(const char *signal);
292 
293 #ifdef QT3_SUPPORT
294 public:
295  QT3_SUPPORT_CONSTRUCTOR QObject(QObject *parent, const char *name);
296  inline QT3_SUPPORT void insertChild(QObject *o)
297  { if (o) o->setParent(this); }
298  inline QT3_SUPPORT void removeChild(QObject *o)
299  { if (o) o->setParent(0); }
300  inline QT3_SUPPORT bool isA(const char *classname) const
301  { return qstrcmp(classname, metaObject()->className()) == 0; }
302  inline QT3_SUPPORT const char *className() const { return metaObject()->className(); }
303  inline QT3_SUPPORT const char *name() const { return objectName().latin1_helper(); }
304  inline QT3_SUPPORT const char *name(const char *defaultName) const
305  { QString s = objectName(); return s.isEmpty()?defaultName:s.latin1_helper(); }
306  inline QT3_SUPPORT void setName(const char *aName) { setObjectName(QLatin1String(aName)); }
307 protected:
308  inline QT3_SUPPORT bool checkConnectArgs(const char *signal,
309  const QObject *,
310  const char *member)
311  { return QMetaObject::checkConnectArgs(signal, member); }
312  static inline QT3_SUPPORT QByteArray normalizeSignalSlot(const char *signalSlot)
313  { return QMetaObject::normalizedSignature(signalSlot); }
314 #endif
315 
316 protected:
317  QObject(QObjectPrivate &dd, QObject *parent = 0);
318 
319 protected:
321 
322  static const QMetaObject staticQtMetaObject;
323 
324  friend struct QMetaObject;
325  friend class QApplication;
326  friend class QApplicationPrivate;
327  friend class QCoreApplication;
328  friend class QCoreApplicationPrivate;
329  friend class QWidget;
330  friend class QThreadData;
331 
332 private:
333  Q_DISABLE_COPY(QObject)
334  Q_PRIVATE_SLOT(d_func(), void _q_reregisterTimers(void *))
335 };
336 
337 inline bool QObject::connect(const QObject *asender, const char *asignal,
338  const char *amember, Qt::ConnectionType atype) const
339 { return connect(asender, asignal, this, amember, atype); }
340 
341 #ifndef QT_NO_USERDATA
342 class Q_CORE_EXPORT QObjectUserData {
343 public:
344  virtual ~QObjectUserData();
345 };
346 #endif
347 
348 #ifdef qdoc
349 T qFindChild(const QObject *o, const QString &name = QString());
350 QList<T> qFindChildren(const QObject *oobj, const QString &name = QString());
351 QList<T> qFindChildren(const QObject *o, const QRegExp &re);
352 #endif
353 #ifdef QT_DEPRECATED
354 template<typename T>
355 inline QT_DEPRECATED T qFindChild(const QObject *o, const QString &name = QString())
356 { return o->findChild<T>(name); }
357 
358 template<typename T>
359 inline QT_DEPRECATED QList<T> qFindChildren(const QObject *o, const QString &name = QString())
360 {
361  return o->findChildren<T>(name);
362 }
363 
364 #ifndef QT_NO_REGEXP
365 template<typename T>
366 inline QT_DEPRECATED QList<T> qFindChildren(const QObject *o, const QRegExp &re)
367 {
368  return o->findChildren<T>(re);
369 }
370 #endif
371 
372 #endif //QT_DEPRECATED
373 
374 template <class T>
375 inline T qobject_cast(QObject *object)
376 {
377 #if !defined(QT_NO_QOBJECT_CHECK)
378  reinterpret_cast<T>(object)->qt_check_for_QOBJECT_macro(*reinterpret_cast<T>(object));
379 #endif
380  return static_cast<T>(reinterpret_cast<T>(object)->staticMetaObject.cast(object));
381 }
382 
383 template <class T>
384 inline T qobject_cast(const QObject *object)
385 {
386 #if !defined(QT_NO_QOBJECT_CHECK)
387  reinterpret_cast<T>(object)->qt_check_for_QOBJECT_macro(*reinterpret_cast<T>(const_cast<QObject *>(object)));
388 #endif
389  return static_cast<T>(reinterpret_cast<T>(object)->staticMetaObject.cast(object));
390 }
391 
392 
393 template <class T> inline const char * qobject_interface_iid()
394 { return 0; }
395 
396 #ifndef Q_MOC_RUN
397 # define Q_DECLARE_INTERFACE(IFace, IId) \
398  template <> inline const char *qobject_interface_iid<IFace *>() \
399  { return IId; } \
400  template <> inline IFace *qobject_cast<IFace *>(QObject *object) \
401  { return reinterpret_cast<IFace *>((object ? object->qt_metacast(IId) : 0)); } \
402  template <> inline IFace *qobject_cast<IFace *>(const QObject *object) \
403  { return reinterpret_cast<IFace *>((object ? const_cast<QObject *>(object)->qt_metacast(IId) : 0)); }
404 #endif // Q_MOC_RUN
405 
406 #ifndef QT_NO_DEBUG_STREAM
407 Q_CORE_EXPORT QDebug operator<<(QDebug, const QObject *);
408 #endif
409 
411 
413 
414 #endif
415 
416 #endif // QOBJECT_H
T qobject_cast(QObject *object)
Definition: qobject.h:375
GLuint GLuint GLsizei GLenum type
Definition: GLee.h:872
QObject * q_ptr
Definition: qobject.h:91
static QByteArray normalizedSignature(const char *method)
#define QT_END_NAMESPACE
Definition: qglobal.h:128
#define Q_PROPERTY(text)
Definition: qobjectdefs.h:80
#define QT_BEGIN_HEADER
Definition: qglobal.h:141
QList< QObject * > QObjectList
Definition: qobject.h:75
static QString fromUtf8(const char *, int size=-1)
T findChild(const QString &aName=QString()) const
Definition: qobject.h:158
QObject * cast(QObject *obj) const
static bool checkConnectArgs(const char *signal, const char *method)
QList< T > findChildren(const QRegExp &re) const
Definition: qobject.h:176
#define Q_INVOKABLE
Definition: qobjectdefs.h:90
Q_CORE_EXPORT int qstrcmp(const char *str1, const char *str2)
Q_CORE_EXPORT QDebug operator<<(QDebug, const QObject *)
Definition: qnamespace.h:54
bool isWidgetType() const
Definition: qobject.h:146
GLXDrawable GLXDrawable member
Definition: GLee.h:10556
#define Q_PRIVATE_SLOT(d, signature)
Definition: qobjectdefs.h:73
#define Q_SLOTS
Definition: qobjectdefs.h:71
#define inline
Definition: image.h:2490
#define Q_SIGNALS
Definition: qobjectdefs.h:72
void setParent(QObject *)
bool inherits(const char *classname) const
Definition: qobject.h:275
Definition: qdebug.h:62
QObject * parent
Definition: qobject.h:92
QScopedPointer< QObjectData > d_ptr
Definition: qobject.h:320
QList< T > findChildren(const QString &aName=QString()) const
Definition: qobject.h:162
ConnectionType
Definition: qnamespace.h:1469
const QObjectList & children() const
Definition: qobject.h:197
static QString fromLatin1(const char *, int size=-1)
#define QT_BEGIN_NAMESPACE
Definition: qglobal.h:127
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: GLee.h:880
static const QMetaObject staticQtMetaObject
Definition: qobject.h:322
int postedEvents
Definition: qobject.h:106
QObject * parent() const
Definition: qobject.h:273
GLenum GLsizei n
Definition: GLee.h:3432
Q_CORE_EXPORT QObject * qt_qFindChild_helper(const QObject *parent, const QString &name, const QMetaObject &mo)
#define Q_OBJECT
Definition: qobjectdefs.h:157
GLubyte GLubyte b
Definition: GLee.h:5404
QObjectList children
Definition: qobject.h:93
GLsizei const GLfloat * value
Definition: GLee.h:1742
bool disconnect(const QObject *receiver, const char *member=0)
Definition: qobject.h:251
GLuint const GLchar * name
Definition: GLee.h:1704
bool signalsBlocked() const
Definition: qobject.h:148
Q_CORE_EXPORT void qt_qFindChildren_helper(const QObject *parent, const QString &name, const QRegExp *re, const QMetaObject &mo, QList< void * > *list)
QMetaObject * metaObject
Definition: qobject.h:107
bool disconnect(const char *signal=0, const QObject *receiver=0, const char *member=0)
Definition: qobject.h:248
GLdouble s
Definition: GLee.h:1173
#define QT_END_HEADER
Definition: qglobal.h:142
const char * qobject_interface_iid()
Definition: qobject.h:393
bool isEmpty() const
Definition: qstring.h:704