Qt 4.8
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)
87 #endif
89 public:
90  virtual ~QObjectData() = 0;
94 
102  uint inEventHandler : 1; //only used if QT_JAMBI_BUILD
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 
112 {
113  Q_OBJECT
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;
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 public:
294 #ifdef QT3_SUPPORT
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 
323 
324  friend struct QMetaObject;
325  friend class QApplication;
326  friend class QApplicationPrivate;
327  friend class QCoreApplication;
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
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
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
T qobject_cast(QObject *object)
Definition: qobject.h:375
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
The QMetaObject class contains meta-information about Qt objects.
Definition: qobjectdefs.h:304
uint unused
Definition: qobject.h:105
The QApplication class manages the GUI application&#39;s control flow and main settings.
Definition: qapplication.h:99
int type
Definition: qmetatype.cpp:239
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
EventRef event
static QByteArray normalizedSignature(const char *method)
Normalizes the signature of the given method.
#define QT_MODULE(x)
Definition: qglobal.h:2783
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
QObject * q_ptr
Definition: qobject.h:91
QString objectName
the name of this object
Definition: qobject.h:114
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QList< T > findChildren(const QRegExp &re) const
Definition: qobject.h:176
#define Q_INVOKABLE
Definition: qobjectdefs.h:90
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
uint hasGuards
Definition: qobject.h:104
uint wasDeleted
Definition: qobject.h:98
static const QMetaObject staticMetaObject
This variable stores the meta-object for the class.
Definition: qobject.h:128
Q_CORE_EXPORT QDebug operator<<(QDebug, const QObject *)
Definition: qobject.cpp:4030
uint isWidget
Definition: qobject.h:95
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
#define Q_DISABLE_COPY(Class)
Disables the use of copy constructors and assignment operators for the given Class.
Definition: qglobal.h:2523
#define Q_PRIVATE_SLOT(d, signature)
Definition: qobjectdefs.h:73
quint16 u
#define Q_SLOTS
Definition: qobjectdefs.h:71
bool disconnect(const QObject *receiver, const char *member=0)
Definition: qobject.h:251
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_SIGNALS
Definition: qobjectdefs.h:72
QObjectList children
Definition: qobject.h:93
const char * className
Definition: qwizard.cpp:137
void setParent(QObject *)
Makes the object a child of parent.
Definition: qobject.cpp:1950
QObject * cast(QObject *obj) const
Returns obj if object obj inherits from this meta-object; otherwise returns 0.
ConnectionType
Definition: qnamespace.h:1469
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
uint blockSig
Definition: qobject.h:97
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
Q_CORE_EXPORT void qt_qFindChildren_helper(const QObject *parent, const QString &name, const QRegExp *re, const QMetaObject &mo, QList< void *> *list)
Definition: qobject.cpp:1900
const char * name
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
uint inThreadChangeEvent
Definition: qobject.h:103
static const char * data(const QByteArray &arr)
unsigned int uint
Definition: qglobal.h:996
T findChild(const QString &aName=QString()) const
Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object.
Definition: qobject.h:158
static bool checkConnectArgs(const char *signal, const char *method)
Returns true if the signal and method arguments are compatible; otherwise returns false...
QList< T > qFindChildren(const QObject *oobj, const QString &name=QString())
Definition: qobject.h:359
Q_CORE_EXPORT QObject * qt_qFindChild_helper(const QObject *parent, const QString &name, const QMetaObject &mo)
Definition: qobject.cpp:1924
bool inherits(const char *classname) const
Returns true if this object is an instance of a class that inherits className or a QObject subclass t...
Definition: qobject.h:275
#define Q_OBJECT
Definition: qobjectdefs.h:157
uint inEventHandler
Definition: qobject.h:102
The QChildEvent class contains event parameters for child object events.
Definition: qcoreevent.h:353
The QCoreApplication class provides an event loop for console Qt applications.
bool isWidgetType() const
Returns true if the object is a widget; otherwise returns false.
Definition: qobject.h:146
#define Q_CORE_EXPORT
Definition: qglobal.h:1449
The QTimerEvent class contains parameters that describe a timer event.
Definition: qcoreevent.h:341
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
QMetaObject * metaObject
Definition: qobject.h:107
const char * property
Definition: qwizard.cpp:138
const QObjectList & children() const
Returns a list of child objects.
Definition: qobject.h:197
static const QMetaObject staticQtMetaObject
Definition: qobject.h:322
QObject * parent
Definition: qobject.h:92
QScopedPointer< QObjectData > d_ptr
Definition: qobject.h:320
#define QT_DEPRECATED
Definition: qglobal.h:1094
bool disconnect(const char *signal=0, const QObject *receiver=0, const char *member=0)
Definition: qobject.h:248
#define Q_DECLARE_PRIVATE(Class)
Definition: qglobal.h:2467
int qstrcmp(const QByteArray &str1, const char *str2)
Definition: qbytearray.cpp:336
uint sendChildEvents
Definition: qobject.h:100
bool signalsBlocked() const
Returns true if signals are blocked; otherwise returns false.
Definition: qobject.h:148
uint pendTimer
Definition: qobject.h:96
int postedEvents
Definition: qobject.h:106
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
#define QT_END_HEADER
Definition: qglobal.h:137
The QThread class provides a platform-independent way to manage threads.
Definition: qthread.h:59
const char * qobject_interface_iid()
Definition: qobject.h:393
QList< T > findChildren(const QString &aName=QString()) const
Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects.
Definition: qobject.h:162
T qFindChild(const QObject *o, const QString &name=QString())
Definition: qobject.h:355
The QMetaMethod class provides meta-data about a member function.
Definition: qmetaobject.h:56
uint receiveChildEvents
Definition: qobject.h:101
uint ownObjectName
Definition: qobject.h:99
The QList class is a template class that provides lists.
Definition: qdatastream.h:62