src/corelib/kernel/qobject.h

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

Generated on Thu Mar 15 11:53:34 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1