📄 qmetaobject.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtCore module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qmetaobject.h"#include "qmetatype.h"#include "qobject.h"#include <qcoreapplication.h>#include <qcoreevent.h>#include <qdatastream.h>#include <qstringlist.h>#include <qthread.h>#include <qvarlengtharray.h>#include <qvariant.h>#include <qhash.h>#include <qdebug.h>#include <qsemaphore.h>#include "private/qobject_p.h"#include "private/qmetaobject_p.h"#include <ctype.h>/*! \class QMetaObject \brief The QMetaObject class contains meta-information about Qt objects. \ingroup objectmodel The Qt \l{Meta-Object System} in Qt is responsible for the signals and slots inter-object communication mechanism, runtime type information, and the Qt property system. A single QMetaObject instance is created for each QObject subclass that is used in an application, and this instance stores all the meta-information for the QObject subclass. This object is available as QObject::metaObject(). This class is not normally required for application programming, but it is useful if you write meta-applications, such as scripting engines or GUI builders. The functions you are most likely to find useful are these: \list \o className() returns the name of a class. \o superClass() returns the superclass's meta-object. \o method() and methodCount() provide information about a class's meta-methods (signals, slots and other member functions). \o enumerator() and enumeratorCount() and provide information about a class's enumerators. \o propertyCount() and property() provide information about a class's properties. \endlist The index functions indexOfMethod(), indexOfEnumerator(), and indexOfProperty() map names of member functions, enumerators, or properties to indexes in the meta-object. For example, Qt uses indexOfMethod() internally when you connect a signal to a slot. Classes can also have a list of \e{name}--\e{value} pairs of additional class information, stored in QMetaClassInfo objects. The number of pairs is returned by classInfoCount(), single pairs are returned by classInfo(), and you can search for pairs with indexOfClassInfo(). \sa QMetaClassInfo, QMetaEnum, QMetaMethod, QMetaProperty, QMetaType, {Meta-Object System}*//*! \enum QMetaObject::Call \internal \value InvokeSlot \value EmitSignal \value ReadProperty \value WriteProperty \value ResetProperty \value QueryPropertyDesignable \value QueryPropertyScriptable \value QueryPropertyStored \value QueryPropertyEditable \value QueryPropertyUser*//*! \enum QMetaMethod::Access \internal*/// do not touch without touching the moc as wellenum PropertyFlags { Invalid = 0x00000000, Readable = 0x00000001, Writable = 0x00000002, Resettable = 0x00000004, EnumOrFlag = 0x00000008, StdCppSet = 0x00000100,// Override = 0x00000200, Designable = 0x00001000, ResolveDesignable = 0x00002000, Scriptable = 0x00004000, ResolveScriptable = 0x00008000, Stored = 0x00010000, ResolveStored = 0x00020000, Editable = 0x00040000, ResolveEditable = 0x00080000, User = 0x00100000, ResolveUser = 0x00200000};enum MethodFlags { AccessPrivate = 0x00, AccessProtected = 0x01, AccessPublic = 0x02, AccessMask = 0x03, //mask MethodMethod = 0x00, MethodSignal = 0x04, MethodSlot = 0x08, MethodTypeMask = 0x0c, MethodCompatibility = 0x10, MethodCloned = 0x20, MethodScriptable = 0x40};struct QMetaObjectPrivate{ int revision; int className; int classInfoCount, classInfoData; int methodCount, methodData; int propertyCount, propertyData; int enumeratorCount, enumeratorData;};static inline const QMetaObjectPrivate *priv(const uint* data){ return reinterpret_cast<const QMetaObjectPrivate*>(data); }/*! \fn const char *QMetaObject::className() const Returns the class name. \sa superClass()*//*! \fn QMetaObject *QMetaObject::superClass() const Returns the meta-object of the superclass, or 0 if there is no such object. \sa className()*//*! \internal Returns \a obj if object \a obj inherits from this meta-object; otherwise returns 0.*/QObject *QMetaObject::cast(QObject *obj) const{ if (obj) { const QMetaObject *m = obj->metaObject(); do { if (m == this) return const_cast<QObject*>(obj); } while ((m = m->d.superdata)); } return 0;}#ifndef QT_NO_TRANSLATION/*! \internal*/QString QMetaObject::tr(const char *s, const char *c) const{ return QCoreApplication::translate(d.stringdata, s, c, QCoreApplication::CodecForTr);}/*! \internal*/QString QMetaObject::tr(const char *s, const char *c, int n) const{ return QCoreApplication::translate(d.stringdata, s, c, QCoreApplication::CodecForTr, n);}/*! \internal*/QString QMetaObject::trUtf8(const char *s, const char *c) const{ return QCoreApplication::translate(d.stringdata, s, c, QCoreApplication::UnicodeUTF8);}/*! \internal*/QString QMetaObject::trUtf8(const char *s, const char *c, int n) const{ return QCoreApplication::translate(d.stringdata, s, c, QCoreApplication::UnicodeUTF8, n);}#endif // QT_NO_TRANSLATION/*! Returns the method offset for this class; i.e. the index position of this class's first member function. The offset is the sum of all the methods in the class's superclasses (which is always positive since QObject has the deleteLater() slot and a destroyed() signal). \sa method(), methodCount(), indexOfMethod()*/int QMetaObject::methodOffset() const{ int offset = 0; const QMetaObject *m = d.superdata; while (m) { offset += priv(m->d.data)->methodCount; m = m->d.superdata; } return offset;}/*! Returns the enumerator offset for this class; i.e. the index position of this class's first enumerator. If the class has no superclasses with enumerators, the offset is 0; otherwise the offset is the sum of all the enumerators in the class's superclasses. \sa enumerator(), enumeratorCount(), indexOfEnumerator()*/int QMetaObject::enumeratorOffset() const{ int offset = 0; const QMetaObject *m = d.superdata; while (m) { offset += priv(m->d.data)->enumeratorCount; m = m->d.superdata; } return offset;}/*! Returns the property offset for this class; i.e. the index position of this class's first property. The offset is the sum of all the properties in the class's superclasses (which is always positive since QObject has the name() property). \sa property(), propertyCount(), indexOfProperty()*/int QMetaObject::propertyOffset() const{ int offset = 0; const QMetaObject *m = d.superdata; while (m) { offset += priv(m->d.data)->propertyCount; m = m->d.superdata; } return offset;}/*! Returns the class information offset for this class; i.e. the index position of this class's first class information item. If the class has no superclasses with class information, the offset is 0; otherwise the offset is the sum of all the class information items in the class's superclasses. \sa classInfo(), classInfoCount(), indexOfClassInfo()*/int QMetaObject::classInfoOffset() const{ int offset = 0; const QMetaObject *m = d.superdata; while (m) { offset += priv(m->d.data)->classInfoCount; m = m->d.superdata; } return offset;}/*! Returns the number of methods in this class. These include signals and slots. \sa method(), methodOffset(), indexOfMethod()*/int QMetaObject::methodCount() const{ int n = priv(d.data)->methodCount; const QMetaObject *m = d.superdata; while (m) { n += priv(m->d.data)->methodCount; m = m->d.superdata; } return n;}/*! Returns the number of enumerators in this class. \sa enumerator(), enumeratorOffset(), indexOfEnumerator()*/int QMetaObject::enumeratorCount() const{ int n = priv(d.data)->enumeratorCount; const QMetaObject *m = d.superdata; while (m) { n += priv(m->d.data)->enumeratorCount; m = m->d.superdata; } return n;}/*! Returns the number of properties in this class. \sa property(), propertyOffset(), indexOfProperty()*/int QMetaObject::propertyCount() const{ int n = priv(d.data)->propertyCount; const QMetaObject *m = d.superdata; while (m) { n += priv(m->d.data)->propertyCount; m = m->d.superdata; } return n;}/*! Returns the number of items of class information in this class. \sa classInfo(), classInfoOffset(), indexOfClassInfo()*/int QMetaObject::classInfoCount() const{ int n = priv(d.data)->classInfoCount; const QMetaObject *m = d.superdata; while (m) { n += priv(m->d.data)->classInfoCount; m = m->d.superdata; } return n;}/*! Finds \a method and returns its index; otherwise returns -1. Note that the \a method has to be in normalized form, as returned by normalizedSignature(). \sa method(), methodCount(), methodOffset(), normalizedSignature()*/int QMetaObject::indexOfMethod(const char *method) const{ int i = -1; const QMetaObject *m = this; while (m && i < 0) { for (i = priv(m->d.data)->methodCount-1; i >= 0; --i) if (strcmp(method, m->d.stringdata + m->d.data[priv(m->d.data)->methodData + 5*i]) == 0) { i += m->methodOffset(); break; } m = m->d.superdata; } return i;}/*! Finds \a signal and returns its index; otherwise returns -1. This is the same as indexOfMethod(), except that it will return -1 if the method exists but isn't a signal. Note that the \a signal has to be in normalized form, as returned by normalizedSignature(). \sa indexOfMethod(), normalizedSignature(), method(), methodCount(), methodOffset()*/int QMetaObject::indexOfSignal(const char *signal) const{ int i = -1; const QMetaObject *m = this; while (m && i < 0) { for (i = priv(m->d.data)->methodCount-1; i >= 0; --i) if ((m->d.data[priv(m->d.data)->methodData + 5*i + 4] & MethodTypeMask) == MethodSignal && strcmp(signal, m->d.stringdata + m->d.data[priv(m->d.data)->methodData + 5*i]) == 0) { i += m->methodOffset(); break; } m = m->d.superdata; }#ifndef QT_NO_DEBUG if (i >= 0 && m && m->d.superdata) { int conflict = m->d.superdata->indexOfMethod(signal); if (conflict >= 0) qWarning("QMetaObject::indexOfSignal:%s: Conflict with %s::%s", m->d.stringdata, m->d.superdata->d.stringdata, signal); }#endif return i;}/*! Finds \a slot and returns its index; otherwise returns -1. This is the same as indexOfMethod(), except that it will return -1 if the method exists but isn't a slot. \sa indexOfMethod(), method(), methodCount(), methodOffset()*/int QMetaObject::indexOfSlot(const char *slot) const{ int i = -1; const QMetaObject *m = this; while (m && i < 0) { for (i = priv(m->d.data)->methodCount-1; i >= 0; --i) if ((m->d.data[priv(m->d.data)->methodData + 5*i + 4] & MethodTypeMask) == MethodSlot && strcmp(slot, m->d.stringdata + m->d.data[priv(m->d.data)->methodData + 5*i]) == 0) { i += m->methodOffset(); break; } m = m->d.superdata; } return i;}static const QMetaObject *QMetaObject_findMetaObject(const QMetaObject *self, const char *name){ while (self) { if (strcmp(self->d.stringdata, name) == 0) return self; if (self->d.extradata) { const QMetaObject **e = self->d.extradata; while (*e) { if (const QMetaObject *m =QMetaObject_findMetaObject((*e), name)) return m; ++e; } } self = self->d.superdata; } return self;}/*! Finds enumerator \a name and returns its index; otherwise returns -1. \sa enumerator(), enumeratorCount(), enumeratorOffset()*/int QMetaObject::indexOfEnumerator(const char *name) const{ int i = -1; const QMetaObject *m = this; while (m && i < 0) { for (i = priv(m->d.data)->enumeratorCount-1; i >= 0; --i) if (strcmp(name, m->d.stringdata + m->d.data[priv(m->d.data)->enumeratorData + 4*i]) == 0) { i += m->enumeratorOffset(); break; } m = m->d.superdata; } return i;}/*! Finds property \a name and returns its index; otherwise returns
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -