1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtCore module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qmetaobject.h"
43 #include "qmetatype.h"
46 #include <qcoreapplication.h>
47 #include <qcoreevent.h>
48 #include <qdatastream.h>
49 #include <qstringlist.h>
51 #include <qvarlengtharray.h>
55 #include <qsemaphore.h>
57 #include "private/qobject_p.h"
58 #include "private/qmetaobject_p.h"
67 \brief The QMetaObject class contains meta-information about Qt
72 The Qt \l{Meta-Object System} in Qt is responsible for the
73 signals and slots inter-object communication mechanism, runtime
74 type information, and the Qt property system. A single
75 QMetaObject instance is created for each QObject subclass that is
76 used in an application, and this instance stores all the
77 meta-information for the QObject subclass. This object is
78 available as QObject::metaObject().
80 This class is not normally required for application programming,
81 but it is useful if you write meta-applications, such as scripting
82 engines or GUI builders.
84 The functions you are most likely to find useful are these:
86 \o className() returns the name of a class.
87 \o superClass() returns the superclass's meta-object.
88 \o method() and methodCount() provide information
89 about a class's meta-methods (signals, slots and other
90 \l{Q_INVOKABLE}{invokable} member functions).
91 \o enumerator() and enumeratorCount() and provide information about
92 a class's enumerators.
93 \o propertyCount() and property() provide information about a
95 \o constructor() and constructorCount() provide information
96 about a class's meta-constructors.
99 The index functions indexOfConstructor(), indexOfMethod(),
100 indexOfEnumerator(), and indexOfProperty() map names of constructors,
101 member functions, enumerators, or properties to indexes in the
102 meta-object. For example, Qt uses indexOfMethod() internally when you
103 connect a signal to a slot.
105 Classes can also have a list of \e{name}--\e{value} pairs of
106 additional class information, stored in QMetaClassInfo objects.
107 The number of pairs is returned by classInfoCount(), single pairs
108 are returned by classInfo(), and you can search for pairs with
111 \sa QMetaClassInfo, QMetaEnum, QMetaMethod, QMetaProperty, QMetaType,
116 \enum QMetaObject::Call
125 \value QueryPropertyDesignable
126 \value QueryPropertyScriptable
127 \value QueryPropertyStored
128 \value QueryPropertyEditable
129 \value QueryPropertyUser
130 \value CreateInstance
134 \enum QMetaMethod::Access
136 This enum describes the access level of a method, following the conventions used in C++.
143 static inline const QMetaObjectPrivate *priv(const uint* data)
144 { return reinterpret_cast<const QMetaObjectPrivate*>(data); }
150 Constructs a new instance of this class. You can pass up to ten arguments
151 (\a val0, \a val1, \a val2, \a val3, \a val4, \a val5, \a val6, \a val7,
152 \a val8, and \a val9) to the constructor. Returns the new object, or 0 if
153 no suitable constructor is available.
155 Note that only constructors that are declared with the Q_INVOKABLE
156 modifier are made available through the meta-object system.
158 \sa Q_ARG(), constructor()
160 QObject *QMetaObject::newInstance(QGenericArgument val0,
161 QGenericArgument val1,
162 QGenericArgument val2,
163 QGenericArgument val3,
164 QGenericArgument val4,
165 QGenericArgument val5,
166 QGenericArgument val6,
167 QGenericArgument val7,
168 QGenericArgument val8,
169 QGenericArgument val9) const
171 QByteArray constructorName = className();
173 int idx = constructorName.lastIndexOf(':');
175 constructorName.remove(0, idx+1); // remove qualified part
177 QVarLengthArray<char, 512> sig;
178 sig.append(constructorName.constData(), constructorName.length());
181 enum { MaximumParamCount = 10 };
182 const char *typeNames[] = {val0.name(), val1.name(), val2.name(), val3.name(), val4.name(),
183 val5.name(), val6.name(), val7.name(), val8.name(), val9.name()};
186 for (paramCount = 0; paramCount < MaximumParamCount; ++paramCount) {
187 int len = qstrlen(typeNames[paramCount]);
190 sig.append(typeNames[paramCount], len);
194 sig.append(')'); // no parameters
196 sig[sig.size() - 1] = ')';
199 int idx = indexOfConstructor(sig.constData());
201 QByteArray norm = QMetaObject::normalizedSignature(sig.constData());
202 idx = indexOfConstructor(norm.constData());
207 QVariant ret(QMetaType::QObjectStar, (void*)0);
208 void *param[] = {ret.data(), val0.data(), val1.data(), val2.data(), val3.data(), val4.data(),
209 val5.data(), val6.data(), val7.data(), val8.data(), val9.data()};
211 if (static_metacall(CreateInstance, idx, param) >= 0)
213 return *reinterpret_cast<QObject**>(param[0]);
219 int QMetaObject::static_metacall(Call cl, int idx, void **argv) const
221 const QMetaObjectExtraData *extra = reinterpret_cast<const QMetaObjectExtraData *>(d.extradata);
222 if (priv(d.data)->revision >= 6) {
223 if (!extra || !extra->static_metacall)
225 extra->static_metacall(0, cl, idx, argv);
227 } else if (priv(d.data)->revision >= 2) {
228 if (!extra || !extra->static_metacall)
230 typedef int (*OldMetacall)(QMetaObject::Call, int, void **);
231 OldMetacall o = reinterpret_cast<OldMetacall>(extra->static_metacall);
232 return o(cl, idx, argv);
240 int QMetaObject::metacall(QObject *object, Call cl, int idx, void **argv)
242 if (QMetaObject *mo = object->d_ptr->metaObject)
243 return static_cast<QAbstractDynamicMetaObject*>(mo)->metaCall(cl, idx, argv);
245 return object->qt_metacall(cl, idx, argv);
249 \fn const char *QMetaObject::className() const
251 Returns the class name.
257 \fn QMetaObject *QMetaObject::superClass() const
259 Returns the meta-object of the superclass, or 0 if there is no
268 Returns \a obj if object \a obj inherits from this
269 meta-object; otherwise returns 0.
271 QObject *QMetaObject::cast(QObject *obj) const
274 const QMetaObject *m = obj->metaObject();
278 } while ((m = m->d.superdata));
286 Returns \a obj if object \a obj inherits from this
287 meta-object; otherwise returns 0.
289 const QObject *QMetaObject::cast(const QObject *obj) const
292 const QMetaObject *m = obj->metaObject();
296 } while ((m = m->d.superdata));
301 #ifndef QT_NO_TRANSLATION
305 QString QMetaObject::tr(const char *s, const char *c) const
307 return QCoreApplication::translate(d.stringdata, s, c, QCoreApplication::CodecForTr);
313 QString QMetaObject::tr(const char *s, const char *c, int n) const
315 return QCoreApplication::translate(d.stringdata, s, c, QCoreApplication::CodecForTr, n);
321 QString QMetaObject::trUtf8(const char *s, const char *c) const
323 return QCoreApplication::translate(d.stringdata, s, c, QCoreApplication::UnicodeUTF8);
329 QString QMetaObject::trUtf8(const char *s, const char *c, int n) const
331 return QCoreApplication::translate(d.stringdata, s, c, QCoreApplication::UnicodeUTF8, n);
333 #endif // QT_NO_TRANSLATION
336 Returns the method offset for this class; i.e. the index position
337 of this class's first member function.
339 The offset is the sum of all the methods in the class's
340 superclasses (which is always positive since QObject has the
341 deleteLater() slot and a destroyed() signal).
343 \sa method(), methodCount(), indexOfMethod()
345 int QMetaObject::methodOffset() const
348 const QMetaObject *m = d.superdata;
350 offset += priv(m->d.data)->methodCount;
358 Returns the enumerator offset for this class; i.e. the index
359 position of this class's first enumerator.
361 If the class has no superclasses with enumerators, the offset is
362 0; otherwise the offset is the sum of all the enumerators in the
363 class's superclasses.
365 \sa enumerator(), enumeratorCount(), indexOfEnumerator()
367 int QMetaObject::enumeratorOffset() const
370 const QMetaObject *m = d.superdata;
372 offset += priv(m->d.data)->enumeratorCount;
379 Returns the property offset for this class; i.e. the index
380 position of this class's first property.
382 The offset is the sum of all the properties in the class's
383 superclasses (which is always positive since QObject has the
386 \sa property(), propertyCount(), indexOfProperty()
388 int QMetaObject::propertyOffset() const
391 const QMetaObject *m = d.superdata;
393 offset += priv(m->d.data)->propertyCount;
400 Returns the class information offset for this class; i.e. the
401 index position of this class's first class information item.
403 If the class has no superclasses with class information, the
404 offset is 0; otherwise the offset is the sum of all the class
405 information items in the class's superclasses.
407 \sa classInfo(), classInfoCount(), indexOfClassInfo()
409 int QMetaObject::classInfoOffset() const
412 const QMetaObject *m = d.superdata;
414 offset += priv(m->d.data)->classInfoCount;
423 Returns the number of constructors in this class.
425 \sa constructor(), indexOfConstructor()
427 int QMetaObject::constructorCount() const
429 if (priv(d.data)->revision < 2)
431 return priv(d.data)->constructorCount;
435 Returns the number of methods in this class, including the number of
436 properties provided by each base class. These include signals and slots
437 as well as normal member functions.
439 Use code like the following to obtain a QStringList containing the methods
440 specific to a given class:
442 \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp methodCount
444 \sa method(), methodOffset(), indexOfMethod()
446 int QMetaObject::methodCount() const
448 int n = priv(d.data)->methodCount;
449 const QMetaObject *m = d.superdata;
451 n += priv(m->d.data)->methodCount;
458 Returns the number of enumerators in this class.
460 \sa enumerator(), enumeratorOffset(), indexOfEnumerator()
462 int QMetaObject::enumeratorCount() const
464 int n = priv(d.data)->enumeratorCount;
465 const QMetaObject *m = d.superdata;
467 n += priv(m->d.data)->enumeratorCount;
474 Returns the number of properties in this class, including the number of
475 properties provided by each base class.
477 Use code like the following to obtain a QStringList containing the properties
478 specific to a given class:
480 \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp propertyCount
482 \sa property(), propertyOffset(), indexOfProperty()
484 int QMetaObject::propertyCount() const
486 int n = priv(d.data)->propertyCount;
487 const QMetaObject *m = d.superdata;
489 n += priv(m->d.data)->propertyCount;
496 Returns the number of items of class information in this class.
498 \sa classInfo(), classInfoOffset(), indexOfClassInfo()
500 int QMetaObject::classInfoCount() const
502 int n = priv(d.data)->classInfoCount;
503 const QMetaObject *m = d.superdata;
505 n += priv(m->d.data)->classInfoCount;
512 * helper function for indexOf{Method,Slot,Signal}, returns the relative index of the method within
514 * \a MethodType might be MethodSignal or MethodSlot, or 0 to match everything.
515 * \a normalizeStringData set to true if we should do a second pass for old moc generated files normalizing all the symbols.
517 template<int MethodType>
518 static inline int indexOfMethodRelative(const QMetaObject **baseObject,
520 bool normalizeStringData)
522 for (const QMetaObject *m = *baseObject; m; m = m->d.superdata) {
523 int i = (MethodType == MethodSignal && priv(m->d.data)->revision >= 4)
524 ? (priv(m->d.data)->signalCount - 1) : (priv(m->d.data)->methodCount - 1);
525 const int end = (MethodType == MethodSlot && priv(m->d.data)->revision >= 4)
526 ? (priv(m->d.data)->signalCount) : 0;
527 if (!normalizeStringData) {
528 for (; i >= end; --i) {
529 const char *stringdata = m->d.stringdata + m->d.data[priv(m->d.data)->methodData + 5*i];
530 if (method[0] == stringdata[0] && strcmp(method + 1, stringdata + 1) == 0) {
535 } else if (priv(m->d.data)->revision < 5) {
536 for (; i >= end; --i) {
537 const char *stringdata = (m->d.stringdata + m->d.data[priv(m->d.data)->methodData + 5 * i]);
538 const QByteArray normalizedSignature = QMetaObject::normalizedSignature(stringdata);
539 if (normalizedSignature == method) {
553 Finds \a constructor and returns its index; otherwise returns -1.
555 Note that the \a constructor has to be in normalized form, as returned
556 by normalizedSignature().
558 \sa constructor(), constructorCount(), normalizedSignature()
560 int QMetaObject::indexOfConstructor(const char *constructor) const
562 if (priv(d.data)->revision < 2)
564 for (int i = priv(d.data)->constructorCount-1; i >= 0; --i) {
565 const char *data = d.stringdata + d.data[priv(d.data)->constructorData + 5*i];
566 if (data[0] == constructor[0] && strcmp(constructor + 1, data + 1) == 0) {
574 Finds \a method and returns its index; otherwise returns -1.
576 Note that the \a method has to be in normalized form, as returned
577 by normalizedSignature().
579 \sa method(), methodCount(), methodOffset(), normalizedSignature()
581 int QMetaObject::indexOfMethod(const char *method) const
583 const QMetaObject *m = this;
584 int i = indexOfMethodRelative<0>(&m, method, false);
587 i = indexOfMethodRelative<0>(&m, method, true);
590 i += m->methodOffset();
595 Finds \a signal and returns its index; otherwise returns -1.
597 This is the same as indexOfMethod(), except that it will return
598 -1 if the method exists but isn't a signal.
600 Note that the \a signal has to be in normalized form, as returned
601 by normalizedSignature().
603 \sa indexOfMethod(), normalizedSignature(), method(), methodCount(), methodOffset()
605 int QMetaObject::indexOfSignal(const char *signal) const
607 const QMetaObject *m = this;
608 int i = QMetaObjectPrivate::indexOfSignalRelative(&m, signal, false);
611 i = QMetaObjectPrivate::indexOfSignalRelative(&m, signal, true);
614 i += m->methodOffset();
619 Same as QMetaObject::indexOfSignal, but the result is the local offset to the base object.
621 \a baseObject will be adjusted to the enclosing QMetaObject, or 0 if the signal is not found
623 int QMetaObjectPrivate::indexOfSignalRelative(const QMetaObject **baseObject,
625 bool normalizeStringData)
627 int i = indexOfMethodRelative<MethodSignal>(baseObject, signal, normalizeStringData);
629 const QMetaObject *m = *baseObject;
630 if (i >= 0 && m && m->d.superdata) {
631 int conflict = m->d.superdata->indexOfMethod(signal);
633 qWarning("QMetaObject::indexOfSignal: signal %s from %s redefined in %s",
634 signal, m->d.superdata->d.stringdata, m->d.stringdata);
641 Finds \a slot and returns its index; otherwise returns -1.
643 This is the same as indexOfMethod(), except that it will return
644 -1 if the method exists but isn't a slot.
646 \sa indexOfMethod(), method(), methodCount(), methodOffset()
648 int QMetaObject::indexOfSlot(const char *slot) const
650 const QMetaObject *m = this;
651 int i = QMetaObjectPrivate::indexOfSlotRelative(&m, slot, false);
653 i = QMetaObjectPrivate::indexOfSlotRelative(&m, slot, true);
655 i += m->methodOffset();
659 // same as indexOfSignalRelative but for slots.
660 int QMetaObjectPrivate::indexOfSlotRelative(const QMetaObject **m,
662 bool normalizeStringData)
664 return indexOfMethodRelative<MethodSlot>(m, slot, normalizeStringData);
667 static const QMetaObject *QMetaObject_findMetaObject(const QMetaObject *self, const char *name)
670 if (strcmp(self->d.stringdata, name) == 0)
672 if (self->d.extradata) {
673 #ifdef Q_NO_DATA_RELOCATION
674 const QMetaObjectAccessor *e;
675 Q_ASSERT(priv(self->d.data)->revision >= 2);
677 const QMetaObject **e;
678 if (priv(self->d.data)->revision < 2) {
679 e = (const QMetaObject**)(self->d.extradata);
683 const QMetaObjectExtraData *extra = (const QMetaObjectExtraData*)(self->d.extradata);
688 #ifdef Q_NO_DATA_RELOCATION
689 if (const QMetaObject *m =QMetaObject_findMetaObject(&((*e)()), name))
691 if (const QMetaObject *m =QMetaObject_findMetaObject((*e), name))
698 self = self->d.superdata;
704 Finds enumerator \a name and returns its index; otherwise returns
707 \sa enumerator(), enumeratorCount(), enumeratorOffset()
709 int QMetaObject::indexOfEnumerator(const char *name) const
711 const QMetaObject *m = this;
713 const QMetaObjectPrivate *d = priv(m->d.data);
714 for (int i = d->enumeratorCount - 1; i >= 0; --i) {
715 const char *prop = m->d.stringdata + m->d.data[d->enumeratorData + 4*i];
716 if (name[0] == prop[0] && strcmp(name + 1, prop + 1) == 0) {
717 i += m->enumeratorOffset();
727 Finds property \a name and returns its index; otherwise returns
730 \sa property(), propertyCount(), propertyOffset()
732 int QMetaObject::indexOfProperty(const char *name) const
734 const QMetaObject *m = this;
736 const QMetaObjectPrivate *d = priv(m->d.data);
737 for (int i = d->propertyCount-1; i >= 0; --i) {
738 const char *prop = m->d.stringdata + m->d.data[d->propertyData + 3*i];
739 if (name[0] == prop[0] && strcmp(name + 1, prop + 1) == 0) {
740 i += m->propertyOffset();
747 if (priv(this->d.data)->revision >= 3 && (priv(this->d.data)->flags & DynamicMetaObject)) {
748 QAbstractDynamicMetaObject *me =
749 const_cast<QAbstractDynamicMetaObject *>(static_cast<const QAbstractDynamicMetaObject *>(this));
751 return me->createProperty(name, 0);
758 Finds class information item \a name and returns its index;
759 otherwise returns -1.
761 \sa classInfo(), classInfoCount(), classInfoOffset()
763 int QMetaObject::indexOfClassInfo(const char *name) const
766 const QMetaObject *m = this;
768 for (i = priv(m->d.data)->classInfoCount-1; i >= 0; --i)
769 if (strcmp(name, m->d.stringdata
770 + m->d.data[priv(m->d.data)->classInfoData + 2*i]) == 0) {
771 i += m->classInfoOffset();
782 Returns the meta-data for the constructor with the given \a index.
784 \sa constructorCount(), newInstance()
786 QMetaMethod QMetaObject::constructor(int index) const
790 if (priv(d.data)->revision >= 2 && i >= 0 && i < priv(d.data)->constructorCount) {
792 result.handle = priv(d.data)->constructorData + 5*i;
798 Returns the meta-data for the method with the given \a index.
800 \sa methodCount(), methodOffset(), indexOfMethod()
802 QMetaMethod QMetaObject::method(int index) const
806 if (i < 0 && d.superdata)
807 return d.superdata->method(index);
810 if (i >= 0 && i < priv(d.data)->methodCount) {
812 result.handle = priv(d.data)->methodData + 5*i;
818 Returns the meta-data for the enumerator with the given \a index.
820 \sa enumeratorCount(), enumeratorOffset(), indexOfEnumerator()
822 QMetaEnum QMetaObject::enumerator(int index) const
825 i -= enumeratorOffset();
826 if (i < 0 && d.superdata)
827 return d.superdata->enumerator(index);
830 if (i >= 0 && i < priv(d.data)->enumeratorCount) {
832 result.handle = priv(d.data)->enumeratorData + 4*i;
838 Returns the meta-data for the property with the given \a index.
839 If no such property exists, a null QMetaProperty is returned.
841 \sa propertyCount(), propertyOffset(), indexOfProperty()
843 QMetaProperty QMetaObject::property(int index) const
846 i -= propertyOffset();
847 if (i < 0 && d.superdata)
848 return d.superdata->property(index);
850 QMetaProperty result;
851 if (i >= 0 && i < priv(d.data)->propertyCount) {
852 int handle = priv(d.data)->propertyData + 3*i;
853 int flags = d.data[handle + 2];
854 const char *type = d.stringdata + d.data[handle + 1];
856 result.handle = handle;
859 if (flags & EnumOrFlag) {
860 result.menum = enumerator(indexOfEnumerator(type));
861 if (!result.menum.isValid()) {
862 QByteArray enum_name = type;
863 QByteArray scope_name = d.stringdata;
864 int s = enum_name.lastIndexOf("::");
866 scope_name = enum_name.left(s);
867 enum_name = enum_name.mid(s + 2);
869 const QMetaObject *scope = 0;
870 if (scope_name == "Qt")
871 scope = &QObject::staticQtMetaObject;
873 scope = QMetaObject_findMetaObject(this, scope_name);
875 result.menum = scope->enumerator(scope->indexOfEnumerator(enum_name));
885 Returns the property that has the \c USER flag set to true.
887 \sa QMetaProperty::isUser()
889 QMetaProperty QMetaObject::userProperty() const
891 const int propCount = propertyCount();
892 for (int i = propCount - 1; i >= 0; --i) {
893 const QMetaProperty prop = property(i);
897 return QMetaProperty();
901 Returns the meta-data for the item of class information with the
906 \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 0
908 \sa classInfoCount(), classInfoOffset(), indexOfClassInfo()
910 QMetaClassInfo QMetaObject::classInfo(int index) const
913 i -= classInfoOffset();
914 if (i < 0 && d.superdata)
915 return d.superdata->classInfo(index);
917 QMetaClassInfo result;
918 if (i >= 0 && i < priv(d.data)->classInfoCount) {
920 result.handle = priv(d.data)->classInfoData + 2*i;
926 Returns true if the \a signal and \a method arguments are
927 compatible; otherwise returns false.
929 Both \a signal and \a method are expected to be normalized.
931 \sa normalizedSignature()
933 bool QMetaObject::checkConnectArgs(const char *signal, const char *method)
935 const char *s1 = signal;
936 const char *s2 = method;
937 while (*s1++ != '(') { } // scan to first '('
938 while (*s2++ != '(') { }
939 if (*s2 == ')' || qstrcmp(s1,s2) == 0) // method has no args or
940 return true; // exact match
941 int s1len = qstrlen(s1);
942 int s2len = qstrlen(s2);
943 if (s2len < s1len && strncmp(s1,s2,s2len-1)==0 && s1[s2len-1]==',')
944 return true; // method has less args
948 static void qRemoveWhitespace(const char *s, char *d)
951 while (*s && is_space(*s))
954 while (*s && !is_space(*s))
956 while (*s && is_space(*s))
958 if (*s && ((is_ident_char(*s) && is_ident_char(last))
959 || ((*s == ':') && (last == '<')))) {
966 static char *qNormalizeType(char *d, int &templdepth, QByteArray &result)
969 while (*d && (templdepth
970 || (*d != ',' && *d != ')'))) {
977 if (strncmp("void", t, d - t) != 0)
978 result += normalizeTypeInternal(t, d);
987 Normalizes a \a type.
989 See QMetaObject::normalizedSignature() for a description on how
994 \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 1
996 \sa normalizedSignature()
998 QByteArray QMetaObject::normalizedType(const char *type)
1002 if (!type || !*type)
1005 QVarLengthArray<char> stackbuf(qstrlen(type) + 1);
1006 qRemoveWhitespace(type, stackbuf.data());
1008 qNormalizeType(stackbuf.data(), templdepth, result);
1014 Normalizes the signature of the given \a method.
1016 Qt uses normalized signatures to decide whether two given signals
1017 and slots are compatible. Normalization reduces whitespace to a
1018 minimum, moves 'const' to the front where appropriate, removes
1019 'const' from value types and replaces const references with
1022 \sa checkConnectArgs(), normalizedType()
1024 QByteArray QMetaObject::normalizedSignature(const char *method)
1027 if (!method || !*method)
1029 int len = int(strlen(method));
1030 QVarLengthArray<char> stackbuf(len + 1);
1031 char *d = stackbuf.data();
1032 qRemoveWhitespace(method, d);
1034 result.reserve(len);
1039 if (argdepth == 1) {
1040 d = qNormalizeType(d, templdepth, result);
1041 if (!*d) //most likely an invalid signature.
1054 enum { MaximumParamCount = 11 }; // up to 10 arguments + 1 return value
1057 Invokes the \a member (a signal or a slot name) on the object \a
1058 obj. Returns true if the member could be invoked. Returns false
1059 if there is no such member or the parameters did not match.
1061 The invocation can be either synchronous or asynchronous,
1062 depending on \a type:
1065 \o If \a type is Qt::DirectConnection, the member will be invoked immediately.
1067 \o If \a type is Qt::QueuedConnection,
1068 a QEvent will be sent and the member is invoked as soon as the application
1069 enters the main event loop.
1071 \o If \a type is Qt::BlockingQueuedConnection, the method will be invoked in
1072 the same way as for Qt::QueuedConnection, except that the current thread
1073 will block until the event is delivered. Using this connection type to
1074 communicate between objects in the same thread will lead to deadlocks.
1076 \o If \a type is Qt::AutoConnection, the member is invoked
1077 synchronously if \a obj lives in the same thread as the
1078 caller; otherwise it will invoke the member asynchronously.
1081 The return value of the \a member function call is placed in \a
1082 ret. If the invocation is asynchronous, the return value cannot
1083 be evaluated. You can pass up to ten arguments (\a val0, \a val1,
1084 \a val2, \a val3, \a val4, \a val5, \a val6, \a val7, \a val8,
1085 and \a val9) to the \a member function.
1087 QGenericArgument and QGenericReturnArgument are internal
1088 helper classes. Because signals and slots can be dynamically
1089 invoked, you must enclose the arguments using the Q_ARG() and
1090 Q_RETURN_ARG() macros. Q_ARG() takes a type name and a
1091 const reference of that type; Q_RETURN_ARG() takes a type name
1092 and a non-const reference.
1094 You only need to pass the name of the signal or slot to this function,
1095 not the entire signature. For example, to asynchronously invoke
1096 the \l{QPushButton::animateClick()}{animateClick()} slot on a
1097 QPushButton, use the following code:
1099 \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 2
1101 With asynchronous method invocations, the parameters must be of
1102 types that are known to Qt's meta-object system, because Qt needs
1103 to copy the arguments to store them in an event behind the
1104 scenes. If you try to use a queued connection and get the error
1107 \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 3
1109 call qRegisterMetaType() to register the data type before you
1110 call invokeMethod().
1112 To synchronously invoke the \c compute(QString, int, double) slot on
1113 some arbitrary object \c obj retrieve its return value:
1115 \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 4
1117 If the "compute" slot does not take exactly one QString, one int
1118 and one double in the specified order, the call will fail.
1120 \sa Q_ARG(), Q_RETURN_ARG(), qRegisterMetaType(), QMetaMethod::invoke()
1122 bool QMetaObject::invokeMethod(QObject *obj,
1124 Qt::ConnectionType type,
1125 QGenericReturnArgument ret,
1126 QGenericArgument val0,
1127 QGenericArgument val1,
1128 QGenericArgument val2,
1129 QGenericArgument val3,
1130 QGenericArgument val4,
1131 QGenericArgument val5,
1132 QGenericArgument val6,
1133 QGenericArgument val7,
1134 QGenericArgument val8,
1135 QGenericArgument val9)
1140 QVarLengthArray<char, 512> sig;
1141 int len = qstrlen(member);
1144 sig.append(member, len);
1147 const char *typeNames[] = {ret.name(), val0.name(), val1.name(), val2.name(), val3.name(),
1148 val4.name(), val5.name(), val6.name(), val7.name(), val8.name(),
1152 for (paramCount = 1; paramCount < MaximumParamCount; ++paramCount) {
1153 len = qstrlen(typeNames[paramCount]);
1156 sig.append(typeNames[paramCount], len);
1159 if (paramCount == 1)
1160 sig.append(')'); // no parameters
1162 sig[sig.size() - 1] = ')';
1165 int idx = obj->metaObject()->indexOfMethod(sig.constData());
1167 QByteArray norm = QMetaObject::normalizedSignature(sig.constData());
1168 idx = obj->metaObject()->indexOfMethod(norm.constData());
1171 if (idx < 0 || idx >= obj->metaObject()->methodCount()) {
1172 qWarning("QMetaObject::invokeMethod: No such method %s::%s",
1173 obj->metaObject()->className(), sig.constData());
1176 QMetaMethod method = obj->metaObject()->method(idx);
1177 return method.invoke(obj, type, ret,
1178 val0, val1, val2, val3, val4, val5, val6, val7, val8, val9);
1181 /*! \fn bool QMetaObject::invokeMethod(QObject *obj, const char *member,
1182 QGenericReturnArgument ret,
1183 QGenericArgument val0 = QGenericArgument(0),
1184 QGenericArgument val1 = QGenericArgument(),
1185 QGenericArgument val2 = QGenericArgument(),
1186 QGenericArgument val3 = QGenericArgument(),
1187 QGenericArgument val4 = QGenericArgument(),
1188 QGenericArgument val5 = QGenericArgument(),
1189 QGenericArgument val6 = QGenericArgument(),
1190 QGenericArgument val7 = QGenericArgument(),
1191 QGenericArgument val8 = QGenericArgument(),
1192 QGenericArgument val9 = QGenericArgument());
1193 \overload invokeMethod()
1195 This overload always invokes the member using the connection type Qt::AutoConnection.
1198 /*! \fn bool QMetaObject::invokeMethod(QObject *obj, const char *member,
1199 Qt::ConnectionType type,
1200 QGenericArgument val0 = QGenericArgument(0),
1201 QGenericArgument val1 = QGenericArgument(),
1202 QGenericArgument val2 = QGenericArgument(),
1203 QGenericArgument val3 = QGenericArgument(),
1204 QGenericArgument val4 = QGenericArgument(),
1205 QGenericArgument val5 = QGenericArgument(),
1206 QGenericArgument val6 = QGenericArgument(),
1207 QGenericArgument val7 = QGenericArgument(),
1208 QGenericArgument val8 = QGenericArgument(),
1209 QGenericArgument val9 = QGenericArgument())
1211 \overload invokeMethod()
1213 This overload can be used if the return value of the member is of no interest.
1217 \fn bool QMetaObject::invokeMethod(QObject *obj, const char *member,
1218 QGenericArgument val0 = QGenericArgument(0),
1219 QGenericArgument val1 = QGenericArgument(),
1220 QGenericArgument val2 = QGenericArgument(),
1221 QGenericArgument val3 = QGenericArgument(),
1222 QGenericArgument val4 = QGenericArgument(),
1223 QGenericArgument val5 = QGenericArgument(),
1224 QGenericArgument val6 = QGenericArgument(),
1225 QGenericArgument val7 = QGenericArgument(),
1226 QGenericArgument val8 = QGenericArgument(),
1227 QGenericArgument val9 = QGenericArgument())
1229 \overload invokeMethod()
1231 This overload invokes the member using the connection type Qt::AutoConnection and
1232 ignores return values.
1238 \brief The QMetaMethod class provides meta-data about a member
1241 \ingroup objectmodel
1243 A QMetaMethod has a methodType(), a signature(), a list of
1244 parameterTypes() and parameterNames(), a return typeName(), a
1245 tag(), and an access() specifier. You can use invoke() to invoke
1246 the method on an arbitrary QObject.
1248 \sa QMetaObject, QMetaEnum, QMetaProperty, {Qt's Property System}
1252 \enum QMetaMethod::Attributes
1256 \value Compatibility
1262 \fn const QMetaObject *QMetaMethod::enclosingMetaObject() const
1267 \enum QMetaMethod::MethodType
1269 \value Method The function is a plain member function.
1270 \value Signal The function is a signal.
1271 \value Slot The function is a slot.
1272 \value Constructor The function is a constructor.
1276 \fn QMetaMethod::QMetaMethod()
1281 Returns the signature of this method (e.g.,
1282 \c{setValue(double)}).
1284 \sa parameterTypes(), parameterNames()
1286 const char *QMetaMethod::signature() const
1290 return mobj->d.stringdata + mobj->d.data[handle];
1294 Returns a list of parameter types.
1296 \sa parameterNames(), signature()
1298 QList<QByteArray> QMetaMethod::parameterTypes() const
1300 QList<QByteArray> list;
1303 const char *signature = mobj->d.stringdata + mobj->d.data[handle];
1304 while (*signature && *signature != '(')
1306 while (*signature && *signature != ')' && *++signature != ')') {
1307 const char *begin = signature;
1309 while (*signature && (level > 0 || *signature != ',') && *signature != ')') {
1310 if (*signature == '<')
1312 else if (*signature == '>')
1316 list += QByteArray(begin, signature - begin);
1322 Returns a list of parameter names.
1324 \sa parameterTypes(), signature()
1326 QList<QByteArray> QMetaMethod::parameterNames() const
1328 QList<QByteArray> list;
1331 const char *names = mobj->d.stringdata + mobj->d.data[handle + 1];
1333 // do we have one or zero arguments?
1334 const char *signature = mobj->d.stringdata + mobj->d.data[handle];
1335 while (*signature && *signature != '(')
1337 if (*++signature != ')')
1338 list += QByteArray();
1342 const char *begin = ++names;
1343 while (*names && *names != ',')
1345 list += QByteArray(begin, names - begin);
1353 Returns the return type of this method, or an empty string if the
1354 return type is \e void.
1356 const char *QMetaMethod::typeName() const
1360 return mobj->d.stringdata + mobj->d.data[handle + 2];
1364 Returns the tag associated with this method.
1366 Tags are special macros recognized by \c moc that make it
1367 possible to add extra information about a method. For the moment,
1368 \c moc doesn't support any special tags.
1370 const char *QMetaMethod::tag() const
1374 return mobj->d.stringdata + mobj->d.data[handle + 3];
1379 int QMetaMethod::attributes() const
1383 return ((mobj->d.data[handle + 4])>>4);
1389 Returns this method's index.
1391 int QMetaMethod::methodIndex() const
1395 return ((handle - priv(mobj->d.data)->methodData) / 5) + mobj->methodOffset();
1401 Returns the method revision if one was
1402 specified by Q_REVISION, otherwise returns 0.
1404 int QMetaMethod::revision() const
1408 if ((QMetaMethod::Access)(mobj->d.data[handle + 4] & MethodRevisioned)) {
1409 int offset = priv(mobj->d.data)->methodData
1410 + priv(mobj->d.data)->methodCount * 5
1411 + (handle - priv(mobj->d.data)->methodData) / 5;
1412 return mobj->d.data[offset];
1418 Returns the access specification of this method (private,
1419 protected, or public).
1421 Signals are always protected, meaning that you can only emit them
1422 from the class or from a subclass.
1426 QMetaMethod::Access QMetaMethod::access() const
1430 return (QMetaMethod::Access)(mobj->d.data[handle + 4] & AccessMask);
1434 Returns the type of this method (signal, slot, or method).
1438 QMetaMethod::MethodType QMetaMethod::methodType() const
1441 return QMetaMethod::Method;
1442 return (QMetaMethod::MethodType)((mobj->d.data[handle + 4] & MethodTypeMask)>>2);
1446 Invokes this method on the object \a object. Returns true if the member could be invoked.
1447 Returns false if there is no such member or the parameters did not match.
1449 The invocation can be either synchronous or asynchronous, depending on the
1453 \o If \a connectionType is Qt::DirectConnection, the member will be invoked immediately.
1455 \o If \a connectionType is Qt::QueuedConnection,
1456 a QEvent will be posted and the member is invoked as soon as the application
1457 enters the main event loop.
1459 \o If \a connectionType is Qt::AutoConnection, the member is invoked
1460 synchronously if \a object lives in the same thread as the
1461 caller; otherwise it will invoke the member asynchronously.
1464 The return value of this method call is placed in \a
1465 returnValue. If the invocation is asynchronous, the return value cannot
1466 be evaluated. You can pass up to ten arguments (\a val0, \a val1,
1467 \a val2, \a val3, \a val4, \a val5, \a val6, \a val7, \a val8,
1468 and \a val9) to this method call.
1470 QGenericArgument and QGenericReturnArgument are internal
1471 helper classes. Because signals and slots can be dynamically
1472 invoked, you must enclose the arguments using the Q_ARG() and
1473 Q_RETURN_ARG() macros. Q_ARG() takes a type name and a
1474 const reference of that type; Q_RETURN_ARG() takes a type name
1475 and a non-const reference.
1477 To asynchronously invoke the
1478 \l{QPushButton::animateClick()}{animateClick()} slot on a
1481 \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 6
1483 With asynchronous method invocations, the parameters must be of
1484 types that are known to Qt's meta-object system, because Qt needs
1485 to copy the arguments to store them in an event behind the
1486 scenes. If you try to use a queued connection and get the error
1489 \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 7
1491 call qRegisterMetaType() to register the data type before you
1492 call QMetaMethod::invoke().
1494 To synchronously invoke the \c compute(QString, int, double) slot on
1495 some arbitrary object \c obj retrieve its return value:
1497 \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 8
1499 QMetaObject::normalizedSignature() is used here to ensure that the format
1500 of the signature is what invoke() expects. E.g. extra whitespace is
1503 If the "compute" slot does not take exactly one QString, one int
1504 and one double in the specified order, the call will fail.
1506 \warning this method will not test the validity of the arguments: \a object
1507 must be an instance of the class of the QMetaObject of which this QMetaMethod
1508 has been constructed with. The arguments must have the same type as the ones
1509 expected by the method, else, the behaviour is undefined.
1511 \sa Q_ARG(), Q_RETURN_ARG(), qRegisterMetaType(), QMetaObject::invokeMethod()
1513 bool QMetaMethod::invoke(QObject *object,
1514 Qt::ConnectionType connectionType,
1515 QGenericReturnArgument returnValue,
1516 QGenericArgument val0,
1517 QGenericArgument val1,
1518 QGenericArgument val2,
1519 QGenericArgument val3,
1520 QGenericArgument val4,
1521 QGenericArgument val5,
1522 QGenericArgument val6,
1523 QGenericArgument val7,
1524 QGenericArgument val8,
1525 QGenericArgument val9) const
1527 if (!object || !mobj)
1530 Q_ASSERT(mobj->cast(object));
1532 // check return type
1533 if (returnValue.data()) {
1534 const char *retType = typeName();
1535 if (qstrcmp(returnValue.name(), retType) != 0) {
1536 // normalize the return value as well
1537 // the trick here is to make a function signature out of the return type
1538 // so that we can call normalizedSignature() and avoid duplicating code
1539 QByteArray unnormalized;
1540 int len = qstrlen(returnValue.name());
1542 unnormalized.reserve(len + 3);
1543 unnormalized = "_("; // the function is called "_"
1544 unnormalized.append(returnValue.name());
1545 unnormalized.append(')');
1547 QByteArray normalized = QMetaObject::normalizedSignature(unnormalized.constData());
1548 normalized.truncate(normalized.length() - 1); // drop the ending ')'
1550 if (qstrcmp(normalized.constData() + 2, retType) != 0)
1555 // check argument count (we don't allow invoking a method if given too few arguments)
1556 const char *typeNames[] = {
1570 for (paramCount = 1; paramCount < MaximumParamCount; ++paramCount) {
1571 if (qstrlen(typeNames[paramCount]) <= 0)
1574 int metaMethodArgumentCount = 0;
1576 // based on QMetaObject::parameterNames()
1577 const char *names = mobj->d.stringdata + mobj->d.data[handle + 1];
1579 // do we have one or zero arguments?
1580 const char *signature = mobj->d.stringdata + mobj->d.data[handle];
1581 while (*signature && *signature != '(')
1583 if (*++signature != ')')
1584 ++metaMethodArgumentCount;
1589 while (*names && *names != ',')
1591 ++metaMethodArgumentCount;
1595 if (paramCount <= metaMethodArgumentCount)
1598 // check connection type
1599 QThread *currentThread = QThread::currentThread();
1600 QThread *objectThread = object->thread();
1601 if (connectionType == Qt::AutoConnection) {
1602 connectionType = currentThread == objectThread
1603 ? Qt::DirectConnection
1604 : Qt::QueuedConnection;
1608 if (connectionType == Qt::BlockingQueuedConnection) {
1609 connectionType = Qt::DirectConnection;
1627 // recompute the methodIndex by reversing the arithmetic in QMetaObject::property()
1628 int idx_relative = ((handle - priv(mobj->d.data)->methodData) / 5);
1629 int idx_offset = mobj->methodOffset();
1630 QObjectPrivate::StaticMetaCallFunction callFunction =
1631 (QMetaObjectPrivate::get(mobj)->revision >= 6 && mobj->d.extradata)
1632 ? reinterpret_cast<const QMetaObjectExtraData *>(mobj->d.extradata)->static_metacall : 0;
1634 if (connectionType == Qt::DirectConnection) {
1636 callFunction(object, QMetaObject::InvokeMetaMethod, idx_relative, param);
1639 return QMetaObject::metacall(object, QMetaObject::InvokeMetaMethod, idx_relative + idx_offset, param) < 0;
1641 } else if (connectionType == Qt::QueuedConnection) {
1642 if (returnValue.data()) {
1643 qWarning("QMetaMethod::invoke: Unable to invoke methods with return values in "
1644 "queued connections");
1648 int nargs = 1; // include return type
1649 void **args = (void **) qMalloc(paramCount * sizeof(void *));
1651 int *types = (int *) qMalloc(paramCount * sizeof(int));
1653 types[0] = 0; // return type
1656 for (int i = 1; i < paramCount; ++i) {
1657 types[i] = QMetaType::type(typeNames[i]);
1659 args[i] = QMetaType::construct(types[i], param[i]);
1661 } else if (param[i]) {
1662 qWarning("QMetaMethod::invoke: Unable to handle unregistered datatype '%s'",
1664 for (int x = 1; x < i; ++x) {
1665 if (types[x] && args[x])
1666 QMetaType::destroy(types[x], args[x]);
1674 QCoreApplication::postEvent(object, new QMetaCallEvent(idx_offset, idx_relative, callFunction,
1675 0, -1, nargs, types, args));
1676 } else { // blocking queued connection
1677 #ifndef QT_NO_THREAD
1678 if (currentThread == objectThread) {
1679 qWarning("QMetaMethod::invoke: Dead lock detected in "
1680 "BlockingQueuedConnection: Receiver is %s(%p)",
1681 mobj->className(), object);
1684 QSemaphore semaphore;
1685 QCoreApplication::postEvent(object, new QMetaCallEvent(idx_offset, idx_relative, callFunction,
1686 0, -1, 0, 0, param, &semaphore));
1687 semaphore.acquire();
1688 #endif // QT_NO_THREAD
1693 /*! \fn bool QMetaMethod::invoke(QObject *object,
1694 QGenericReturnArgument returnValue,
1695 QGenericArgument val0 = QGenericArgument(0),
1696 QGenericArgument val1 = QGenericArgument(),
1697 QGenericArgument val2 = QGenericArgument(),
1698 QGenericArgument val3 = QGenericArgument(),
1699 QGenericArgument val4 = QGenericArgument(),
1700 QGenericArgument val5 = QGenericArgument(),
1701 QGenericArgument val6 = QGenericArgument(),
1702 QGenericArgument val7 = QGenericArgument(),
1703 QGenericArgument val8 = QGenericArgument(),
1704 QGenericArgument val9 = QGenericArgument()) const
1707 This overload always invokes this method using the connection type Qt::AutoConnection.
1710 /*! \fn bool QMetaMethod::invoke(QObject *object,
1711 Qt::ConnectionType connectionType,
1712 QGenericArgument val0 = QGenericArgument(0),
1713 QGenericArgument val1 = QGenericArgument(),
1714 QGenericArgument val2 = QGenericArgument(),
1715 QGenericArgument val3 = QGenericArgument(),
1716 QGenericArgument val4 = QGenericArgument(),
1717 QGenericArgument val5 = QGenericArgument(),
1718 QGenericArgument val6 = QGenericArgument(),
1719 QGenericArgument val7 = QGenericArgument(),
1720 QGenericArgument val8 = QGenericArgument(),
1721 QGenericArgument val9 = QGenericArgument()) const
1725 This overload can be used if the return value of the member is of no interest.
1729 \fn bool QMetaMethod::invoke(QObject *object,
1730 QGenericArgument val0 = QGenericArgument(0),
1731 QGenericArgument val1 = QGenericArgument(),
1732 QGenericArgument val2 = QGenericArgument(),
1733 QGenericArgument val3 = QGenericArgument(),
1734 QGenericArgument val4 = QGenericArgument(),
1735 QGenericArgument val5 = QGenericArgument(),
1736 QGenericArgument val6 = QGenericArgument(),
1737 QGenericArgument val7 = QGenericArgument(),
1738 QGenericArgument val8 = QGenericArgument(),
1739 QGenericArgument val9 = QGenericArgument()) const
1743 This overload invokes this method using the
1744 connection type Qt::AutoConnection and ignores return values.
1749 \brief The QMetaEnum class provides meta-data about an enumerator.
1751 \ingroup objectmodel
1753 Use name() for the enumerator's name. The enumerator's keys (names
1754 of each enumerated item) are returned by key(); use keyCount() to find
1755 the number of keys. isFlag() returns whether the enumerator is
1756 meant to be used as a flag, meaning that its values can be combined
1757 using the OR operator.
1759 The conversion functions keyToValue(), valueToKey(), keysToValue(),
1760 and valueToKeys() allow conversion between the integer
1761 representation of an enumeration or set value and its literal
1762 representation. The scope() function returns the class scope this
1763 enumerator was declared in.
1765 \sa QMetaObject, QMetaMethod, QMetaProperty
1769 \fn bool QMetaEnum::isValid() const
1771 Returns true if this enum is valid (has a name); otherwise returns
1778 \fn const QMetaObject *QMetaEnum::enclosingMetaObject() const
1784 \fn QMetaEnum::QMetaEnum()
1789 Returns the name of the enumerator (without the scope).
1791 For example, the Qt::AlignmentFlag enumeration has \c
1792 AlignmentFlag as the name and \l Qt as the scope.
1794 \sa isValid(), scope()
1796 const char *QMetaEnum::name() const
1800 return mobj->d.stringdata + mobj->d.data[handle];
1804 Returns the number of keys.
1808 int QMetaEnum::keyCount() const
1812 return mobj->d.data[handle + 2];
1817 Returns the key with the given \a index, or 0 if no such key exists.
1819 \sa keyCount(), value(), valueToKey()
1821 const char *QMetaEnum::key(int index) const
1825 int count = mobj->d.data[handle + 2];
1826 int data = mobj->d.data[handle + 3];
1827 if (index >= 0 && index < count)
1828 return mobj->d.stringdata + mobj->d.data[data + 2*index];
1833 Returns the value with the given \a index; or returns -1 if there
1836 \sa keyCount(), key(), keyToValue()
1838 int QMetaEnum::value(int index) const
1842 int count = mobj->d.data[handle + 2];
1843 int data = mobj->d.data[handle + 3];
1844 if (index >= 0 && index < count)
1845 return mobj->d.data[data + 2*index + 1];
1851 Returns true if this enumerator is used as a flag; otherwise returns
1854 When used as flags, enumerators can be combined using the OR
1857 \sa keysToValue(), valueToKeys()
1859 bool QMetaEnum::isFlag() const
1861 return mobj && mobj->d.data[handle + 1];
1866 Returns the scope this enumerator was declared in.
1868 For example, the Qt::AlignmentFlag enumeration has \c Qt as
1869 the scope and \c AlignmentFlag as the name.
1873 const char *QMetaEnum::scope() const
1875 return mobj?mobj->d.stringdata : 0;
1879 Returns the integer value of the given enumeration \a key, or -1
1880 if \a key is not defined.
1882 For flag types, use keysToValue().
1884 \sa valueToKey(), isFlag(), keysToValue()
1886 int QMetaEnum::keyToValue(const char *key) const
1891 const char *qualified_key = key;
1892 const char *s = key + qstrlen(key);
1893 while (s > key && *s != ':')
1895 if (s > key && *(s-1)==':') {
1896 scope = s - key - 1;
1899 int count = mobj->d.data[handle + 2];
1900 int data = mobj->d.data[handle + 3];
1901 for (int i = 0; i < count; ++i)
1902 if ((!scope || (qstrlen(mobj->d.stringdata) == scope && strncmp(qualified_key, mobj->d.stringdata, scope) == 0))
1903 && strcmp(key, mobj->d.stringdata + mobj->d.data[data + 2*i]) == 0)
1904 return mobj->d.data[data + 2*i + 1];
1909 Returns the string that is used as the name of the given
1910 enumeration \a value, or 0 if \a value is not defined.
1912 For flag types, use valueToKeys().
1914 \sa isFlag(), valueToKeys()
1916 const char* QMetaEnum::valueToKey(int value) const
1920 int count = mobj->d.data[handle + 2];
1921 int data = mobj->d.data[handle + 3];
1922 for (int i = 0; i < count; ++i)
1923 if (value == (int)mobj->d.data[data + 2*i + 1])
1924 return mobj->d.stringdata + mobj->d.data[data + 2*i];
1929 Returns the value derived from combining together the values of
1930 the \a keys using the OR operator, or -1 if \a keys is not
1931 defined. Note that the strings in \a keys must be '|'-separated.
1933 \sa isFlag(), valueToKey(), valueToKeys()
1935 int QMetaEnum::keysToValue(const char *keys) const
1939 QStringList l = QString::fromLatin1(keys).split(QLatin1Char('|'));
1940 //#### TODO write proper code, do not use QStringList
1942 int count = mobj->d.data[handle + 2];
1943 int data = mobj->d.data[handle + 3];
1944 for (int li = 0; li < l.size(); ++li) {
1945 QString trimmed = l.at(li).trimmed();
1946 QByteArray qualified_key = trimmed.toLatin1();
1947 const char *key = qualified_key.constData();
1949 const char *s = key + qstrlen(key);
1950 while (s > key && *s != ':')
1952 if (s > key && *(s-1)==':') {
1953 scope = s - key - 1;
1957 for (i = count-1; i >= 0; --i)
1958 if ((!scope || (qstrlen(mobj->d.stringdata) == scope && strncmp(qualified_key.constData(), mobj->d.stringdata, scope) == 0))
1959 && strcmp(key, mobj->d.stringdata + mobj->d.data[data + 2*i]) == 0) {
1960 value |= mobj->d.data[data + 2*i + 1];
1970 Returns a byte array of '|'-separated keys that represents the
1973 \sa isFlag(), valueToKey(), keysToValue()
1975 QByteArray QMetaEnum::valueToKeys(int value) const
1980 int count = mobj->d.data[handle + 2];
1981 int data = mobj->d.data[handle + 3];
1983 for(int i = 0; i < count; i++) {
1984 int k = mobj->d.data[data + 2*i + 1];
1985 if ((k != 0 && (v & k) == k ) || (k == value)) {
1987 if (!keys.isEmpty())
1989 keys += mobj->d.stringdata + mobj->d.data[data + 2*i];
1995 static QByteArray qualifiedName(const QMetaEnum &e)
1997 return QByteArray(e.scope()) + "::" + e.name();
2001 \class QMetaProperty
2002 \brief The QMetaProperty class provides meta-data about a property.
2004 \ingroup objectmodel
2006 Property meta-data is obtained from an object's meta-object. See
2007 QMetaObject::property() and QMetaObject::propertyCount() for
2010 \section1 Property Meta-Data
2012 A property has a name() and a type(), as well as various
2013 attributes that specify its behavior: isReadable(), isWritable(),
2014 isDesignable(), isScriptable(), and isStored().
2016 If the property is an enumeration, isEnumType() returns true; if the
2017 property is an enumeration that is also a flag (i.e. its values
2018 can be combined using the OR operator), isEnumType() and
2019 isFlagType() both return true. The enumerator for these types is
2020 available from enumerator().
2022 The property's values are set and retrieved with read(), write(),
2023 and reset(); they can also be changed through QObject's set and get
2024 functions. See QObject::setProperty() and QObject::property() for
2027 \section1 Copying and Assignment
2029 QMetaProperty objects can be copied by value. However, each copy will
2030 refer to the same underlying property meta-data.
2032 \sa QMetaObject, QMetaEnum, QMetaMethod, {Qt's Property System}
2036 \fn bool QMetaProperty::isValid() const
2038 Returns true if this property is valid (readable); otherwise
2045 \fn const QMetaObject *QMetaProperty::enclosingMetaObject() const
2052 QMetaProperty::QMetaProperty()
2053 : mobj(0), handle(0), idx(0)
2059 Returns this property's name.
2061 \sa type(), typeName()
2063 const char *QMetaProperty::name() const
2067 int handle = priv(mobj->d.data)->propertyData + 3*idx;
2068 return mobj->d.stringdata + mobj->d.data[handle];
2072 Returns the name of this property's type.
2076 const char *QMetaProperty::typeName() const
2080 int handle = priv(mobj->d.data)->propertyData + 3*idx;
2081 return mobj->d.stringdata + mobj->d.data[handle + 1];
2085 Returns this property's type. The return value is one
2086 of the values of the QVariant::Type enumeration.
2088 \sa userType(), typeName(), name()
2090 QVariant::Type QMetaProperty::type() const
2093 return QVariant::Invalid;
2094 int handle = priv(mobj->d.data)->propertyData + 3*idx;
2095 uint flags = mobj->d.data[handle + 2];
2097 uint type = flags >> 24;
2098 if (type == 0xff) // special value for QVariant
2099 type = QVariant::LastType;
2101 return QVariant::Type(type);
2103 int enumMetaTypeId = QMetaType::type(qualifiedName(menum));
2104 if (enumMetaTypeId == 0)
2105 return QVariant::Int;
2107 #ifdef QT_COORD_TYPE
2108 // qreal metatype must be resolved at runtime.
2109 if (strcmp(typeName(), "qreal") == 0)
2110 return QVariant::Type(qMetaTypeId<qreal>());
2113 return QVariant::UserType;
2119 Returns this property's user type. The return value is one
2120 of the values that are registered with QMetaType, or 0 if
2121 the type is not registered.
2123 \sa type(), QMetaType, typeName()
2125 int QMetaProperty::userType() const
2127 QVariant::Type tp = type();
2128 if (tp != QVariant::UserType)
2131 int enumMetaTypeId = QMetaType::type(qualifiedName(menum));
2132 return enumMetaTypeId;
2134 return QMetaType::type(typeName());
2140 Returns this property's index.
2142 int QMetaProperty::propertyIndex() const
2146 return idx + mobj->propertyOffset();
2150 Returns true if the property's type is an enumeration value that
2151 is used as a flag; otherwise returns false.
2153 Flags can be combined using the OR operator. A flag type is
2154 implicitly also an enum type.
2156 \sa isEnumType(), enumerator(), QMetaEnum::isFlag()
2159 bool QMetaProperty::isFlagType() const
2161 return isEnumType() && menum.isFlag();
2165 Returns true if the property's type is an enumeration value;
2166 otherwise returns false.
2168 \sa enumerator(), isFlagType()
2170 bool QMetaProperty::isEnumType() const
2174 int handle = priv(mobj->d.data)->propertyData + 3*idx;
2175 int flags = mobj->d.data[handle + 2];
2176 return (flags & EnumOrFlag) && menum.name();
2182 Returns true if the property has a C++ setter function that
2183 follows Qt's standard "name" / "setName" pattern. Designer and uic
2184 query hasStdCppSet() in order to avoid expensive
2185 QObject::setProperty() calls. All properties in Qt [should] follow
2188 bool QMetaProperty::hasStdCppSet() const
2192 int handle = priv(mobj->d.data)->propertyData + 3*idx;
2193 int flags = mobj->d.data[handle + 2];
2194 return (flags & StdCppSet);
2198 Returns the enumerator if this property's type is an enumerator
2199 type; otherwise the returned value is undefined.
2201 \sa isEnumType(), isFlagType()
2203 QMetaEnum QMetaProperty::enumerator() const
2209 Reads the property's value from the given \a object. Returns the value
2210 if it was able to read it; otherwise returns an invalid variant.
2212 \sa write(), reset(), isReadable()
2214 QVariant QMetaProperty::read(const QObject *object) const
2216 if (!object || !mobj)
2219 uint t = QVariant::Int;
2222 try to create a QVariant that can be converted to this enum
2223 type (only works if the enum has already been registered
2226 int enumMetaTypeId = QMetaType::type(qualifiedName(menum));
2227 if (enumMetaTypeId != 0)
2230 int handle = priv(mobj->d.data)->propertyData + 3*idx;
2231 uint flags = mobj->d.data[handle + 2];
2232 const char *typeName = mobj->d.stringdata + mobj->d.data[handle + 1];
2234 if (t == 0xff) // special value for QVariant
2235 t = QVariant::LastType;
2236 if (t == QVariant::Invalid)
2237 t = QMetaType::type(typeName);
2238 if (t == QVariant::Invalid)
2239 t = QVariant::nameToType(typeName);
2240 if (t == QVariant::Invalid || t == QVariant::UserType) {
2241 if (t == QVariant::Invalid)
2242 qWarning("QMetaProperty::read: Unable to handle unregistered datatype '%s' for property '%s::%s'", typeName, mobj->className(), name());
2247 // the status variable is changed by qt_metacall to indicate what it did
2248 // this feature is currently only used by QtDBus and should not be depended
2249 // upon. Don't change it without looking into QDBusAbstractInterface first
2250 // -1 (unchanged): normal qt_metacall, result stored in argv[0]
2251 // changed: result stored directly in value
2254 void *argv[] = { 0, &value, &status };
2255 if (t == QVariant::LastType) {
2258 value = QVariant(t, (void*)0);
2259 argv[0] = value.data();
2261 QMetaObject::metacall(const_cast<QObject*>(object), QMetaObject::ReadProperty,
2262 idx + mobj->propertyOffset(), argv);
2266 if (t != QVariant::LastType && argv[0] != value.data())
2267 // pointer or reference
2268 return QVariant((QVariant::Type)t, argv[0]);
2273 Writes \a value as the property's value to the given \a object. Returns
2274 true if the write succeeded; otherwise returns false.
2276 \sa read(), reset(), isWritable()
2278 bool QMetaProperty::write(QObject *object, const QVariant &value) const
2280 if (!object || !isWritable())
2284 uint t = QVariant::Invalid;
2286 if (v.type() == QVariant::String) {
2288 v = QVariant(menum.keysToValue(value.toByteArray()));
2290 v = QVariant(menum.keyToValue(value.toByteArray()));
2291 } else if (v.type() != QVariant::Int && v.type() != QVariant::UInt) {
2292 int enumMetaTypeId = QMetaType::type(qualifiedName(menum));
2293 if ((enumMetaTypeId == 0) || (v.userType() != enumMetaTypeId) || !v.constData())
2295 v = QVariant(*reinterpret_cast<const int *>(v.constData()));
2297 v.convert(QVariant::Int);
2299 int handle = priv(mobj->d.data)->propertyData + 3*idx;
2300 uint flags = mobj->d.data[handle + 2];
2302 if (t == 0xff) // special value for QVariant
2303 t = QVariant::LastType;
2304 if (t == QVariant::Invalid) {
2305 const char *typeName = mobj->d.stringdata + mobj->d.data[handle + 1];
2306 const char *vtypeName = value.typeName();
2307 if (vtypeName && strcmp(typeName, vtypeName) == 0)
2308 t = value.userType();
2310 t = QVariant::nameToType(typeName);
2312 if (t == QVariant::Invalid)
2314 if (t != QVariant::LastType && t != (uint)value.userType() && (t < QMetaType::User && !v.convert((QVariant::Type)t)))
2318 // the status variable is changed by qt_metacall to indicate what it did
2319 // this feature is currently only used by QtDBus and should not be depended
2320 // upon. Don't change it without looking into QDBusAbstractInterface first
2321 // -1 (unchanged): normal qt_metacall, result stored in argv[0]
2322 // changed: result stored directly in value, return the value of status
2324 // the flags variable is used by the declarative module to implement
2325 // interception of property writes.
2327 void *argv[] = { 0, &v, &status, &flags };
2328 if (t == QVariant::LastType)
2332 QMetaObject::metacall(object, QMetaObject::WriteProperty, idx + mobj->propertyOffset(), argv);
2337 Resets the property for the given \a object with a reset method.
2338 Returns true if the reset worked; otherwise returns false.
2340 Reset methods are optional; only a few properties support them.
2344 bool QMetaProperty::reset(QObject *object) const
2346 if (!object || !mobj || !isResettable())
2348 void *argv[] = { 0 };
2349 QMetaObject::metacall(object, QMetaObject::ResetProperty, idx + mobj->propertyOffset(), argv);
2354 Returns true if this property can be reset to a default value; otherwise
2359 bool QMetaProperty::isResettable() const
2363 int flags = mobj->d.data[handle + 2];
2364 return flags & Resettable;
2368 Returns true if this property is readable; otherwise returns false.
2370 \sa isWritable(), read(), isValid()
2372 bool QMetaProperty::isReadable() const
2376 int flags = mobj->d.data[handle + 2];
2377 return flags & Readable;
2381 Returns true if this property has a corresponding change notify signal;
2382 otherwise returns false.
2386 bool QMetaProperty::hasNotifySignal() const
2390 int flags = mobj->d.data[handle + 2];
2391 return flags & Notify;
2397 Returns the QMetaMethod instance of the property change notifying signal if
2398 one was specified, otherwise returns an invalid QMetaMethod.
2400 \sa hasNotifySignal()
2402 QMetaMethod QMetaProperty::notifySignal() const
2404 int id = notifySignalIndex();
2406 return mobj->method(id);
2408 return QMetaMethod();
2414 Returns the index of the property change notifying signal if one was
2415 specified, otherwise returns -1.
2417 \sa hasNotifySignal()
2419 int QMetaProperty::notifySignalIndex() const
2421 if (hasNotifySignal()) {
2422 int offset = priv(mobj->d.data)->propertyData +
2423 priv(mobj->d.data)->propertyCount * 3 + idx;
2424 return mobj->d.data[offset] + mobj->methodOffset();
2433 Returns the property revision if one was
2434 specified by REVISION, otherwise returns 0.
2436 int QMetaProperty::revision() const
2440 int flags = mobj->d.data[handle + 2];
2441 if (flags & Revisioned) {
2442 int offset = priv(mobj->d.data)->propertyData +
2443 priv(mobj->d.data)->propertyCount * 3 + idx;
2444 // Revision data is placed after NOTIFY data, if present.
2445 // Iterate through properties to discover whether we have NOTIFY signals.
2446 for (int i = 0; i < priv(mobj->d.data)->propertyCount; ++i) {
2447 int handle = priv(mobj->d.data)->propertyData + 3*i;
2448 if (mobj->d.data[handle + 2] & Notify) {
2449 offset += priv(mobj->d.data)->propertyCount;
2453 return mobj->d.data[offset];
2460 Returns true if this property is writable; otherwise returns
2463 \sa isReadable(), write()
2465 bool QMetaProperty::isWritable() const
2469 int flags = mobj->d.data[handle + 2];
2470 return flags & Writable;
2475 Returns true if this property is designable for the given \a object;
2476 otherwise returns false.
2478 If no \a object is given, the function returns false if the
2479 \c{Q_PROPERTY()}'s \c DESIGNABLE attribute is false; otherwise
2480 returns true (if the attribute is true or is a function or expression).
2482 \sa isScriptable(), isStored()
2484 bool QMetaProperty::isDesignable(const QObject *object) const
2488 int flags = mobj->d.data[handle + 2];
2489 bool b = flags & Designable;
2491 void *argv[] = { &b };
2492 QMetaObject::metacall(const_cast<QObject*>(object), QMetaObject::QueryPropertyDesignable,
2493 idx + mobj->propertyOffset(), argv);
2501 Returns true if the property is scriptable for the given \a object;
2502 otherwise returns false.
2504 If no \a object is given, the function returns false if the
2505 \c{Q_PROPERTY()}'s \c SCRIPTABLE attribute is false; otherwise returns
2506 true (if the attribute is true or is a function or expression).
2508 \sa isDesignable(), isStored()
2510 bool QMetaProperty::isScriptable(const QObject *object) const
2514 int flags = mobj->d.data[handle + 2];
2515 bool b = flags & Scriptable;
2517 void *argv[] = { &b };
2518 QMetaObject::metacall(const_cast<QObject*>(object), QMetaObject::QueryPropertyScriptable,
2519 idx + mobj->propertyOffset(), argv);
2525 Returns true if the property is stored for \a object; otherwise returns
2528 If no \a object is given, the function returns false if the
2529 \c{Q_PROPERTY()}'s \c STORED attribute is false; otherwise returns
2530 true (if the attribute is true or is a function or expression).
2532 \sa isDesignable(), isScriptable()
2534 bool QMetaProperty::isStored(const QObject *object) const
2538 int flags = mobj->d.data[handle + 2];
2539 bool b = flags & Stored;
2541 void *argv[] = { &b };
2542 QMetaObject::metacall(const_cast<QObject*>(object), QMetaObject::QueryPropertyStored,
2543 idx + mobj->propertyOffset(), argv);
2549 Returns true if this property is designated as the \c USER
2550 property, i.e., the one that the user can edit for \a object or
2551 that is significant in some other way. Otherwise it returns
2552 false. e.g., the \c text property is the \c USER editable property
2555 If \a object is null, the function returns false if the \c
2556 {Q_PROPERTY()}'s \c USER attribute is false. Otherwise it returns
2559 \sa QMetaObject::userProperty(), isDesignable(), isScriptable()
2561 bool QMetaProperty::isUser(const QObject *object) const
2565 int flags = mobj->d.data[handle + 2];
2566 bool b = flags & User;
2568 void *argv[] = { &b };
2569 QMetaObject::metacall(const_cast<QObject*>(object), QMetaObject::QueryPropertyUser,
2570 idx + mobj->propertyOffset(), argv);
2577 Returns true if the property is constant; otherwise returns false.
2579 A property is constant if the \c{Q_PROPERTY()}'s \c CONSTANT attribute
2582 bool QMetaProperty::isConstant() const
2586 int flags = mobj->d.data[handle + 2];
2587 return flags & Constant;
2592 Returns true if the property is final; otherwise returns false.
2594 A property is final if the \c{Q_PROPERTY()}'s \c FINAL attribute
2597 bool QMetaProperty::isFinal() const
2601 int flags = mobj->d.data[handle + 2];
2602 return flags & Final;
2608 Returns true if the property is editable for the given \a object;
2609 otherwise returns false.
2611 If no \a object is given, the function returns false if the
2612 \c{Q_PROPERTY()}'s \c EDITABLE attribute is false; otherwise returns
2613 true (if the attribute is true or is a function or expression).
2615 \sa isDesignable(), isScriptable(), isStored()
2617 bool QMetaProperty::isEditable(const QObject *object) const
2621 int flags = mobj->d.data[handle + 2];
2622 bool b = flags & Editable;
2624 void *argv[] = { &b };
2625 QMetaObject::metacall(const_cast<QObject*>(object), QMetaObject::QueryPropertyEditable,
2626 idx + mobj->propertyOffset(), argv);
2632 \class QMetaClassInfo
2634 \brief The QMetaClassInfo class provides additional information
2637 \ingroup objectmodel
2639 Class information items are simple \e{name}--\e{value} pairs that
2640 are specified using Q_CLASSINFO() in the source code. The
2641 information can be retrieved using name() and value(). For example:
2643 \snippet doc/src/snippets/code/src_corelib_kernel_qmetaobject.cpp 5
2645 This mechanism is free for you to use in your Qt applications. Qt
2646 doesn't use it for any of its classes.
2653 \fn QMetaClassInfo::QMetaClassInfo()
2658 \fn const QMetaObject *QMetaClassInfo::enclosingMetaObject() const
2663 Returns the name of this item.
2667 const char *QMetaClassInfo::name() const
2671 return mobj->d.stringdata + mobj->d.data[handle];
2675 Returns the value of this item.
2679 const char* QMetaClassInfo::value() const
2683 return mobj->d.stringdata + mobj->d.data[handle + 1];
2687 \macro QGenericArgument Q_ARG(Type, const Type &value)
2688 \relates QMetaObject
2690 This macro takes a \a Type and a \a value of that type and
2691 returns a \l QGenericArgument object that can be passed to
2692 QMetaObject::invokeMethod().
2698 \macro QGenericReturnArgument Q_RETURN_ARG(Type, Type &value)
2699 \relates QMetaObject
2701 This macro takes a \a Type and a non-const reference to a \a
2702 value of that type and returns a QGenericReturnArgument object
2703 that can be passed to QMetaObject::invokeMethod().
2709 \class QGenericArgument
2711 \brief The QGenericArgument class is an internal helper class for
2712 marshalling arguments.
2714 This class should never be used directly. Please use the \l Q_ARG()
2717 \sa Q_ARG(), QMetaObject::invokeMethod(), QGenericReturnArgument
2721 \fn QGenericArgument::QGenericArgument(const char *name, const void *data)
2723 Constructs a QGenericArgument object with the given \a name and \a data.
2727 \fn QGenericArgument::data () const
2729 Returns the data set in the constructor.
2733 \fn QGenericArgument::name () const
2735 Returns the name set in the constructor.
2739 \class QGenericReturnArgument
2741 \brief The QGenericReturnArgument class is an internal helper class for
2742 marshalling arguments.
2744 This class should never be used directly. Please use the
2745 Q_RETURN_ARG() macro instead.
2747 \sa Q_RETURN_ARG(), QMetaObject::invokeMethod(), QGenericArgument
2751 \fn QGenericReturnArgument::QGenericReturnArgument(const char *name, void *data)
2753 Constructs a QGenericReturnArgument object with the given \a name
2758 If the local_method_index is a cloned method, return the index of the original.
2760 Example: if the index of "destroyed()" is passed, the index of "destroyed(QObject*)" is returned
2762 int QMetaObjectPrivate::originalClone(const QMetaObject *mobj, int local_method_index)
2764 Q_ASSERT(local_method_index < get(mobj)->methodCount);
2765 int handle = get(mobj)->methodData + 5 * local_method_index;
2766 while (mobj->d.data[handle + 4] & MethodCloned) {
2767 Q_ASSERT(local_method_index > 0);
2769 local_method_index--;
2771 return local_method_index;