qobject.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtCore module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QOBJECT_H
00043 #define QOBJECT_H
00044 
00045 #ifndef QT_NO_QOBJECT
00046 
00047 #include <QtCore/qobjectdefs.h>
00048 #include <QtCore/qstring.h>
00049 #include <QtCore/qbytearray.h>
00050 #include <QtCore/qlist.h>
00051 #ifdef QT_INCLUDE_COMPAT
00052 #include <QtCore/qcoreevent.h>
00053 #endif
00054 #include <QtCore/qscopedpointer.h>
00055 
00056 QT_BEGIN_HEADER
00057 
00058 QT_BEGIN_NAMESPACE
00059 
00060 QT_MODULE(Core)
00061 
00062 class QEvent;
00063 class QTimerEvent;
00064 class QChildEvent;
00065 struct QMetaObject;
00066 class QVariant;
00067 class QObjectPrivate;
00068 class QObject;
00069 class QThread;
00070 class QWidget;
00071 #ifndef QT_NO_REGEXP
00072 class QRegExp;
00073 #endif
00074 #ifndef QT_NO_USERDATA
00075 class QObjectUserData;
00076 #endif
00077 
00078 typedef QList<QObject*> QObjectList;
00079 
00080 #if defined Q_CC_MSVC && _MSC_VER < 1300
00081 template<typename T> inline T qFindChild(const QObject *o, const QString &name = QString(), T = 0);
00082 template<typename T> inline QList<T> qFindChildren(const QObject *o, const QString &name = QString(), T = 0);
00083 # ifndef QT_NO_REGEXP
00084 template<typename T> inline QList<T> qFindChildren(const QObject *o, const QRegExp &re, T = 0);
00085 # endif
00086 #else
00087 template<typename T> inline T qFindChild(const QObject *, const QString & = QString());
00088 template<typename T> inline QList<T> qFindChildren(const QObject *, const QString & = QString());
00089 # ifndef QT_NO_REGEXP
00090 template<typename T> inline QList<T> qFindChildren(const QObject *, const QRegExp &);
00091 # endif
00092 #endif
00093 
00094 class
00095 #if defined(__INTEL_COMPILER) && defined(Q_OS_WIN)
00096 Q_CORE_EXPORT
00097 #endif
00098 QObjectData {
00099 public:
00100     virtual ~QObjectData() = 0;
00101     QObject *q_ptr;
00102     QObject *parent;
00103     QObjectList children;
00104 
00105     uint isWidget : 1;
00106     uint pendTimer : 1;
00107     uint blockSig : 1;
00108     uint wasDeleted : 1;
00109     uint ownObjectName : 1;
00110     uint sendChildEvents : 1;
00111     uint receiveChildEvents : 1;
00112     uint inEventHandler : 1;
00113     uint inThreadChangeEvent : 1;
00114     uint hasGuards : 1; //true iff there is one or more QPointer attached to this object
00115     uint unused : 22;
00116     int postedEvents;
00117     QMetaObject *metaObject; // assert dynamic
00118 };
00119 
00120 
00121 class Q_CORE_EXPORT QObject
00122 {
00123     Q_OBJECT
00124     Q_PROPERTY(QString objectName READ objectName WRITE setObjectName)
00125     Q_DECLARE_PRIVATE(QObject)
00126 
00127 public:
00128     Q_INVOKABLE explicit QObject(QObject *parent=0);
00129     virtual ~QObject();
00130 
00131     virtual bool event(QEvent *);
00132     virtual bool eventFilter(QObject *, QEvent *);
00133 
00134 #ifdef qdoc
00135     static QString tr(const char *sourceText, const char *comment = 0, int n = -1);
00136     static QString trUtf8(const char *sourceText, const char *comment = 0, int n = -1);
00137     virtual const QMetaObject *metaObject() const;
00138     static const QMetaObject staticMetaObject;
00139 #endif
00140 #ifdef QT_NO_TRANSLATION
00141     static QString tr(const char *sourceText, const char *, int)
00142         { return QString::fromLatin1(sourceText); }
00143     static QString tr(const char *sourceText, const char * = 0)
00144         { return QString::fromLatin1(sourceText); }
00145 #ifndef QT_NO_TEXTCODEC
00146     static QString trUtf8(const char *sourceText, const char *, int)
00147         { return QString::fromUtf8(sourceText); }
00148     static QString trUtf8(const char *sourceText, const char * = 0)
00149         { return QString::fromUtf8(sourceText); }
00150 #endif
00151 #endif //QT_NO_TRANSLATION
00152 
00153     QString objectName() const;
00154     void setObjectName(const QString &name);
00155 
00156     inline bool isWidgetType() const { return d_ptr->isWidget; }
00157 
00158     inline bool signalsBlocked() const { return d_ptr->blockSig; }
00159     bool blockSignals(bool b);
00160 
00161     QThread *thread() const;
00162     void moveToThread(QThread *thread);
00163 
00164     int startTimer(int interval);
00165     void killTimer(int id);
00166 
00167 #ifndef QT_NO_MEMBER_TEMPLATES
00168     template<typename T>
00169     inline T findChild(const QString &aName = QString()) const
00170     { return qFindChild<T>(this, aName); }
00171 
00172     template<typename T>
00173     inline QList<T> findChildren(const QString &aName = QString()) const
00174     { return qFindChildren<T>(this, aName); }
00175 
00176 #ifndef QT_NO_REGEXP
00177     template<typename T>
00178     inline QList<T> findChildren(const QRegExp &re) const
00179     { return qFindChildren<T>(this, re); }
00180 #endif
00181 #endif
00182 
00183 #ifdef QT3_SUPPORT
00184     QT3_SUPPORT QObject *child(const char *objName, const char *inheritsClass = 0,
00185                    bool recursiveSearch = true) const;
00186     QT3_SUPPORT QObjectList queryList(const char *inheritsClass = 0,
00187                           const char *objName = 0,
00188                           bool regexpMatch = true,
00189                           bool recursiveSearch = true) const;
00190 #endif
00191     inline const QObjectList &children() const { return d_ptr->children; }
00192 
00193     void setParent(QObject *);
00194     void installEventFilter(QObject *);
00195     void removeEventFilter(QObject *);
00196 
00197 
00198     static bool connect(const QObject *sender, const char *signal,
00199                         const QObject *receiver, const char *member, Qt::ConnectionType =
00200 #ifdef qdoc
00201                         Qt::AutoConnection
00202 #else
00203 #ifdef QT3_SUPPORT
00204                         Qt::AutoCompatConnection
00205 #else
00206                         Qt::AutoConnection
00207 #endif
00208 #endif
00209         );
00210     inline bool connect(const QObject *sender, const char *signal,
00211                         const char *member, Qt::ConnectionType type =
00212 #ifdef qdoc
00213                         Qt::AutoConnection
00214 #else
00215 #ifdef QT3_SUPPORT
00216                         Qt::AutoCompatConnection
00217 #else
00218                         Qt::AutoConnection
00219 #endif
00220 #endif
00221         ) const;
00222 
00223     static bool disconnect(const QObject *sender, const char *signal,
00224                            const QObject *receiver, const char *member);
00225     inline bool disconnect(const char *signal = 0,
00226                            const QObject *receiver = 0, const char *member = 0)
00227         { return disconnect(this, signal, receiver, member); }
00228     inline bool disconnect(const QObject *receiver, const char *member = 0)
00229         { return disconnect(this, 0, receiver, member); }
00230 
00231     void dumpObjectTree();
00232     void dumpObjectInfo();
00233 
00234 #ifndef QT_NO_PROPERTIES
00235     bool setProperty(const char *name, const QVariant &value);
00236     QVariant property(const char *name) const;
00237     QList<QByteArray> dynamicPropertyNames() const;
00238 #endif // QT_NO_PROPERTIES
00239 
00240 #ifndef QT_NO_USERDATA
00241     static uint registerUserData();
00242     void setUserData(uint id, QObjectUserData* data);
00243     QObjectUserData* userData(uint id) const;
00244 #endif // QT_NO_USERDATA
00245 
00246 Q_SIGNALS:
00247     void destroyed(QObject * = 0);
00248 
00249 public:
00250     inline QObject *parent() const { return d_ptr->parent; }
00251 
00252     inline bool inherits(const char *classname) const
00253         { return const_cast<QObject *>(this)->qt_metacast(classname) != 0; }
00254 
00255 public Q_SLOTS:
00256     void deleteLater();
00257 
00258 protected:
00259     QObject *sender() const;
00260     int receivers(const char* signal) const;
00261 
00262     virtual void timerEvent(QTimerEvent *);
00263     virtual void childEvent(QChildEvent *);
00264     virtual void customEvent(QEvent *);
00265 
00266     virtual void connectNotify(const char *signal);
00267     virtual void disconnectNotify(const char *signal);
00268 
00269 #ifdef QT3_SUPPORT
00270 public:
00271     QT3_SUPPORT_CONSTRUCTOR QObject(QObject *parent, const char *name);
00272     inline QT3_SUPPORT void insertChild(QObject *o)
00273         { if (o) o->setParent(this); }
00274     inline QT3_SUPPORT void removeChild(QObject *o)
00275         { if (o) o->setParent(0); }
00276     inline QT3_SUPPORT bool isA(const char *classname) const
00277         { return qstrcmp(classname, metaObject()->className()) == 0; }
00278     inline QT3_SUPPORT const char *className() const { return metaObject()->className(); }
00279     inline QT3_SUPPORT const char *name() const { return objectName().latin1_helper(); }
00280     inline QT3_SUPPORT const char *name(const char *defaultName) const
00281         { QString s = objectName(); return s.isEmpty()?defaultName:s.latin1_helper(); }
00282     inline QT3_SUPPORT void setName(const char *aName) { setObjectName(QLatin1String(aName)); }
00283 protected:
00284     inline QT3_SUPPORT bool checkConnectArgs(const char *signal,
00285                                   const QObject *,
00286                                   const char *member)
00287         { return QMetaObject::checkConnectArgs(signal, member); }
00288     static inline QT3_SUPPORT QByteArray normalizeSignalSlot(const char *signalSlot)
00289         { return QMetaObject::normalizedSignature(signalSlot); }
00290 #endif
00291 
00292 protected:
00293     QObject(QObjectPrivate &dd, QObject *parent = 0);
00294 
00295 protected:
00296     QScopedPointer<QObjectData> d_ptr;
00297 
00298     static const QMetaObject staticQtMetaObject;
00299 
00300     friend struct QMetaObject;
00301     friend class QApplication;
00302     friend class QApplicationPrivate;
00303     friend class QCoreApplication;
00304     friend class QCoreApplicationPrivate;
00305     friend class QWidget;
00306     friend class QThreadData;
00307 
00308 private:
00309     Q_DISABLE_COPY(QObject)
00310     Q_PRIVATE_SLOT(d_func(), void _q_reregisterTimers(void *))
00311 };
00312 
00313 inline bool QObject::connect(const QObject *asender, const char *asignal,
00314                              const char *amember, Qt::ConnectionType atype) const
00315 { return connect(asender, asignal, this, amember, atype); }
00316 
00317 #ifndef QT_NO_USERDATA
00318 class Q_CORE_EXPORT QObjectUserData {
00319 public:
00320     virtual ~QObjectUserData();
00321 };
00322 #endif
00323 
00324 Q_CORE_EXPORT void qt_qFindChildren_helper(const QObject *parent, const QString &name, const QRegExp *re,
00325                                            const QMetaObject &mo, QList<void *> *list);
00326 Q_CORE_EXPORT QObject *qt_qFindChild_helper(const QObject *parent, const QString &name, const QMetaObject &mo);
00327 
00328 template<typename T>
00329 inline T qFindChild(const QObject *o, const QString &name)
00330 { return static_cast<T>(qt_qFindChild_helper(o, name, reinterpret_cast<T>(0)->staticMetaObject)); }
00331 
00332 template<typename T>
00333 inline QList<T> qFindChildren(const QObject *o, const QString &name)
00334 {
00335     QList<T> list;
00336     union {
00337         QList<T> *typedList;
00338         QList<void *> *voidList;
00339     } u;
00340     u.typedList = &list;
00341     qt_qFindChildren_helper(o, name, 0, reinterpret_cast<T>(0)->staticMetaObject, u.voidList);
00342     return list;
00343 }
00344 
00345 #ifndef QT_NO_REGEXP
00346 template<typename T>
00347 inline QList<T> qFindChildren(const QObject *o, const QRegExp &re)
00348 {
00349     QList<T> list;
00350     union {
00351         QList<T> *typedList;
00352         QList<void *> *voidList;
00353     } u;
00354     u.typedList = &list;
00355     qt_qFindChildren_helper(o, QString(), &re, reinterpret_cast<T>(0)->staticMetaObject, u.voidList);
00356     return list;
00357 }
00358 #endif
00359 
00360 template <class T>
00361 inline T qobject_cast(QObject *object)
00362 {
00363 #if !defined(QT_NO_MEMBER_TEMPLATES) && !defined(QT_NO_QOBJECT_CHECK)
00364     reinterpret_cast<T>(0)->qt_check_for_QOBJECT_macro(*reinterpret_cast<T>(object));
00365 #endif
00366     return static_cast<T>(reinterpret_cast<T>(0)->staticMetaObject.cast(object));
00367 }
00368 
00369 template <class T>
00370 inline T qobject_cast(const QObject *object)
00371 {
00372     // this will cause a compilation error if T is not const
00373     register T ptr = static_cast<T>(object);
00374     Q_UNUSED(ptr);
00375 
00376 #if !defined(QT_NO_MEMBER_TEMPLATES) && !defined(QT_NO_QOBJECT_CHECK)
00377     reinterpret_cast<T>(0)->qt_check_for_QOBJECT_macro(*reinterpret_cast<T>(const_cast<QObject *>(object)));
00378 #endif
00379     return static_cast<T>(const_cast<QObject *>(reinterpret_cast<T>(0)->staticMetaObject.cast(const_cast<QObject *>(object))));
00380 }
00381 
00382 
00383 template <class T> inline const char * qobject_interface_iid()
00384 { return 0; }
00385 
00386 #ifndef Q_MOC_RUN
00387 #  define Q_DECLARE_INTERFACE(IFace, IId) \
00388     template <> inline const char *qobject_interface_iid<IFace *>() \
00389     { return IId; } \
00390     template <> inline IFace *qobject_cast<IFace *>(QObject *object) \
00391     { return reinterpret_cast<IFace *>((object ? object->qt_metacast(IId) : 0)); } \
00392     template <> inline IFace *qobject_cast<IFace *>(const QObject *object) \
00393     { return reinterpret_cast<IFace *>((object ? const_cast<QObject *>(object)->qt_metacast(IId) : 0)); }
00394 #endif // Q_MOC_RUN
00395 
00396 #ifndef QT_NO_DEBUG_STREAM
00397 Q_CORE_EXPORT QDebug operator<<(QDebug, const QObject *);
00398 #endif
00399 
00400 QT_END_NAMESPACE
00401 
00402 QT_END_HEADER
00403 
00404 #endif
00405 
00406 #endif // QOBJECT_H