The QMetaMethod class provides meta-data about a member function. More...
Header: | #include <QMetaMethod> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
enum | Access { Private, Protected, Public } |
enum | MethodType { Method, Signal, Slot, Constructor } |
QMetaMethod::Access | access() const |
bool | invoke(QObject *object, Qt::ConnectionType connectionType, QGenericReturnArgument returnValue, QGenericArgument val0 = QGenericArgument(nullptr), QGenericArgument val1 = QGenericArgument(), QGenericArgument val2 = QGenericArgument(), QGenericArgument val3 = QGenericArgument(), QGenericArgument val4 = QGenericArgument(), QGenericArgument val5 = QGenericArgument(), QGenericArgument val6 = QGenericArgument(), QGenericArgument val7 = QGenericArgument(), QGenericArgument val8 = QGenericArgument(), QGenericArgument val9 = QGenericArgument()) const |
bool | invoke(QObject *object, QGenericReturnArgument returnValue, QGenericArgument val0 = QGenericArgument(0), QGenericArgument val1 = QGenericArgument(), QGenericArgument val2 = QGenericArgument(), QGenericArgument val3 = QGenericArgument(), QGenericArgument val4 = QGenericArgument(), QGenericArgument val5 = QGenericArgument(), QGenericArgument val6 = QGenericArgument(), QGenericArgument val7 = QGenericArgument(), QGenericArgument val8 = QGenericArgument(), QGenericArgument val9 = QGenericArgument()) const |
bool | invoke(QObject *object, Qt::ConnectionType connectionType, QGenericArgument val0 = QGenericArgument(0), QGenericArgument val1 = QGenericArgument(), QGenericArgument val2 = QGenericArgument(), QGenericArgument val3 = QGenericArgument(), QGenericArgument val4 = QGenericArgument(), QGenericArgument val5 = QGenericArgument(), QGenericArgument val6 = QGenericArgument(), QGenericArgument val7 = QGenericArgument(), QGenericArgument val8 = QGenericArgument(), QGenericArgument val9 = QGenericArgument()) const |
bool | invoke(QObject *object, QGenericArgument val0 = QGenericArgument(0), QGenericArgument val1 = QGenericArgument(), QGenericArgument val2 = QGenericArgument(), QGenericArgument val3 = QGenericArgument(), QGenericArgument val4 = QGenericArgument(), QGenericArgument val5 = QGenericArgument(), QGenericArgument val6 = QGenericArgument(), QGenericArgument val7 = QGenericArgument(), QGenericArgument val8 = QGenericArgument(), QGenericArgument val9 = QGenericArgument()) const |
bool | invokeOnGadget(void *gadget, QGenericReturnArgument returnValue, QGenericArgument val0 = QGenericArgument(nullptr), QGenericArgument val1 = QGenericArgument(), QGenericArgument val2 = QGenericArgument(), QGenericArgument val3 = QGenericArgument(), QGenericArgument val4 = QGenericArgument(), QGenericArgument val5 = QGenericArgument(), QGenericArgument val6 = QGenericArgument(), QGenericArgument val7 = QGenericArgument(), QGenericArgument val8 = QGenericArgument(), QGenericArgument val9 = QGenericArgument()) const |
bool | invokeOnGadget(void *gadget, QGenericArgument val0 = QGenericArgument(0), QGenericArgument val1 = QGenericArgument(), QGenericArgument val2 = QGenericArgument(), QGenericArgument val3 = QGenericArgument(), QGenericArgument val4 = QGenericArgument(), QGenericArgument val5 = QGenericArgument(), QGenericArgument val6 = QGenericArgument(), QGenericArgument val7 = QGenericArgument(), QGenericArgument val8 = QGenericArgument(), QGenericArgument val9 = QGenericArgument()) const |
bool | isConst() const |
bool | isValid() const |
int | methodIndex() const |
QByteArray | methodSignature() const |
QMetaMethod::MethodType | methodType() const |
QByteArray | name() const |
int | parameterCount() const |
QMetaType | parameterMetaType(int index) const |
QList<QByteArray> | parameterNames() const |
int | parameterType(int index) const |
QByteArray | parameterTypeName(int index) const |
QList<QByteArray> | parameterTypes() const |
int | relativeMethodIndex() const |
QMetaType | returnMetaType() const |
int | returnType() const |
int | revision() const |
const char * | tag() const |
const char * | typeName() const |
QMetaMethod | fromSignal(PointerToMemberFunction signal) |
bool | operator!=(const QMetaMethod &m1, const QMetaMethod &m2) |
bool | operator==(const QMetaMethod &m1, const QMetaMethod &m2) |
A QMetaMethod has a methodType(), a methodSignature(), a list of parameterTypes() and parameterNames(), a return typeName(), a tag(), and an access() specifier. You can use invoke() to invoke the method on an arbitrary QObject.
See also QMetaObject, QMetaEnum, QMetaProperty, and Qt's Property System.
This enum describes the access level of a method, following the conventions used in C++.
Constant | Value |
---|---|
QMetaMethod::Private |
0 |
QMetaMethod::Protected |
1 |
QMetaMethod::Public |
2 |
Constant | Value | Description |
---|---|---|
QMetaMethod::Method |
0 |
The function is a plain member function. |
QMetaMethod::Signal |
1 |
The function is a signal. |
QMetaMethod::Slot |
2 |
The function is a slot. |
QMetaMethod::Constructor |
3 |
The function is a constructor. |
Returns the access specification of this method (private, protected, or public).
Note: Signals are always public, but you should regard that as an implementation detail. It is almost always a bad idea to emit a signal from outside its class.
See also methodType().
[static, since 5.0]
template <typename PointerToMemberFunction> QMetaMethod QMetaMethod::fromSignal(PointerToMemberFunction signal)Returns the meta-method that corresponds to the given signal, or an invalid QMetaMethod if signal is not a signal of the class.
Example:
QMetaMethod destroyedSignal = QMetaMethod::fromSignal(&QObject::destroyed);
This function was introduced in Qt 5.0.
Invokes this method on the object object. Returns true
if the member could be invoked. Returns false
if there is no such member or the parameters did not match.
The invocation can be either synchronous or asynchronous, depending on the connectionType:
The return value of this method call is placed in returnValue. If the invocation is asynchronous, the return value cannot be evaluated. You can pass up to ten arguments (val0, val1, val2, val3, val4, val5, val6, val7, val8, and val9) to this method call.
QGenericArgument and QGenericReturnArgument are internal helper classes. Because signals and slots can be dynamically invoked, you must enclose the arguments using the Q_ARG() and Q_RETURN_ARG() macros. Q_ARG() takes a type name and a const reference of that type; Q_RETURN_ARG() takes a type name and a non-const reference.
To asynchronously invoke the animateClick() slot on a QPushButton:
int methodIndex = pushButton->metaObject()->indexOfMethod("animateClick()"); QMetaMethod method = metaObject->method(methodIndex); method.invoke(pushButton, Qt::QueuedConnection);
With asynchronous method invocations, the parameters must be of types that are known to Qt's meta-object system, because Qt needs to copy the arguments to store them in an event behind the scenes. If you try to use a queued connection and get the error message
QMetaMethod::invoke: Unable to handle unregistered datatype 'MyType'
call qRegisterMetaType() to register the data type before you call QMetaMethod::invoke().
To synchronously invoke the compute(QString, int, double)
slot on some arbitrary object obj
retrieve its return value:
QString retVal; QByteArray normalizedSignature = QMetaObject::normalizedSignature("compute(QString, int, double)"); int methodIndex = obj->metaObject()->indexOfMethod(normalizedSignature); QMetaMethod method = obj->metaObject()->method(methodIndex); method.invoke(obj, Qt::DirectConnection, Q_RETURN_ARG(QString, retVal), Q_ARG(QString, "sqrt"), Q_ARG(int, 42), Q_ARG(double, 9.7));
QMetaObject::normalizedSignature() is used here to ensure that the format of the signature is what invoke() expects. E.g. extra whitespace is removed.
If the "compute" slot does not take exactly one QString, one int and one double in the specified order, the call will fail.
Warning: this method will not test the validity of the arguments: object must be an instance of the class of the QMetaObject of which this QMetaMethod has been constructed with. The arguments must have the same type as the ones expected by the method, else, the behaviour is undefined.
See also Q_ARG(), Q_RETURN_ARG(), qRegisterMetaType(), and QMetaObject::invokeMethod().
This function overloads invoke().
This overload always invokes this method using the connection type Qt::AutoConnection.
This function overloads invoke().
This overload can be used if the return value of the member is of no interest.
This function overloads invoke().
This overload invokes this method using the connection type Qt::AutoConnection and ignores return values.
[since 5.5]
bool QMetaMethod::invokeOnGadget(void *gadget, QGenericReturnArgument returnValue, QGenericArgument val0 = QGenericArgument(nullptr), QGenericArgument val1 = QGenericArgument(), QGenericArgument val2 = QGenericArgument(),
QGenericArgument val3 = QGenericArgument(), QGenericArgument val4 =
QGenericArgument(), QGenericArgument val5 = QGenericArgument(), QGenericArgument
val6 = QGenericArgument(), QGenericArgument val7 = QGenericArgument(), QGenericArgument val8 = QGenericArgument(), QGenericArgument val9 = QGenericArgument()) constInvokes this method on a Q_GADGET. Returns true
if the member could be invoked. Returns false
if there is no such member or the parameters did not match.
The pointer gadget must point to an instance of the gadget class.
The invocation is always synchronous.
The return value of this method call is placed in returnValue. You can pass up to ten arguments (val0, val1, val2, val3, val4, val5, val6, val7, val8, and val9) to this method call.
Warning: this method will not test the validity of the arguments: gadget must be an instance of the class of the QMetaObject of which this QMetaMethod has been constructed with. The arguments must have the same type as the ones expected by the method, else, the behavior is undefined.
This function was introduced in Qt 5.5.
See also Q_ARG(), Q_RETURN_ARG(), qRegisterMetaType(), and QMetaObject::invokeMethod().
[since 5.5]
bool QMetaMethod::invokeOnGadget(void *gadget, QGenericArgument val0 = QGenericArgument(0), QGenericArgument val1 = QGenericArgument(), QGenericArgument val2 = QGenericArgument(), QGenericArgument val3 = QGenericArgument(),
QGenericArgument val4 = QGenericArgument(), QGenericArgument val5 =
QGenericArgument(), QGenericArgument val6 = QGenericArgument(), QGenericArgument
val7 = QGenericArgument(), QGenericArgument val8 = QGenericArgument(), QGenericArgument val9 = QGenericArgument()) constThis is an overloaded function.
This overload invokes this method for a gadget and ignores return values.
This function was introduced in Qt 5.5.
[since 6.2]
bool QMetaMethod::isConst() constReturns whether the method is const qualified.
Note: This method might erroneously return false
for a const method if it belongs to a library compiled against an older version of Qt.
This function was introduced in Qt 6.2.
[since 5.0]
bool QMetaMethod::isValid() constReturns true
if this method is valid (can be introspected and invoked), otherwise returns false
.
This function was introduced in Qt 5.0.
Returns this method's index.
[since 5.0]
QByteArray QMetaMethod::methodSignature() constReturns the signature of this method (e.g., setValue(double)
).
This function was introduced in Qt 5.0.
See also parameterTypes() and parameterNames().
Returns the type of this method (signal, slot, or method).
See also access().
[since 5.0]
QByteArray QMetaMethod::name() constReturns the name of this method.
This function was introduced in Qt 5.0.
See also methodSignature() and parameterCount().
[since 5.0]
int QMetaMethod::parameterCount() constReturns the number of parameters of this method.
This function was introduced in Qt 5.0.
See also parameterType() and parameterNames().
[since 6.0]
QMetaType QMetaMethod::parameterMetaType(int
index) constReturns the metatype of the parameter at the given index.
If the index is smaller than zero or larger than parameterCount(), an invalid QMetaType is returned.
This function was introduced in Qt 6.0.
See also parameterCount(), returnMetaType(), and QMetaType.
Returns a list of parameter names.
See also parameterTypes() and methodSignature().
[since 5.0]
int QMetaMethod::parameterType(int index) constReturns the type of the parameter at the given index.
The return value is one of the types that are registered with QMetaType, or QMetaType::UnknownType if the type is not registered.
This function was introduced in Qt 5.0.
See also parameterCount(), parameterMetaType(), returnType(), and QMetaType.
[since 6.0]
QByteArray QMetaMethod::parameterTypeName(int
index) constReturns the name of the type at position index If there is no parameter at index, returns an empty QByteArray
This function was introduced in Qt 6.0.
See also parameterNames().
Returns a list of parameter types.
See also parameterNames() and methodSignature().
[since 6.0]
int QMetaMethod::relativeMethodIndex() constReturns this method's local index inside.
This function was introduced in Qt 6.0.
[since 6.0]
QMetaType QMetaMethod::returnMetaType() constReturns the return type of this method.
This function was introduced in Qt 6.0.
See also parameterMetaType(), QMetaType, and typeName().
[since 5.0]
int QMetaMethod::returnType() constReturns the return type of this method.
The return value is one of the types that are registered with QMetaType, or QMetaType::UnknownType if the type is not registered.
This function was introduced in Qt 5.0.
See also parameterType(), QMetaType, typeName(), and returnMetaType().
[since 5.1]
int QMetaMethod::revision() constReturns the method revision if one was specified by Q_REVISION, otherwise returns 0.
This function was introduced in Qt 5.1.
Returns the tag associated with this method.
Tags are special macros recognized by moc
that make it possible to add extra information about a method.
Tag information can be added in the following way in the function declaration:
// In the class MainWindow declaration #ifndef Q_MOC_RUN // define the tag text as empty, so the compiler doesn't see it # define MY_CUSTOM_TAG #endif ... private slots: MY_CUSTOM_TAG void testFunc();
and the information can be accessed by using:
MainWindow win; win.show(); int functionIndex = win.metaObject()->indexOfSlot("testFunc()"); QMetaMethod mm = win.metaObject()->method(functionIndex); qDebug() << mm.tag(); // prints MY_CUSTOM_TAG
For the moment, moc
will extract and record all tags, but it will not handle any of them specially. You can use the tags to annotate your methods differently, and treat them according to the specific needs of
your application.
Note: Since Qt 5.0, moc
expands preprocessor macros, so it is necessary to surround the definition with #ifndef
Q_MOC_RUN
, as shown in the example above. This was not required
in Qt 4. The code as shown above works with Qt 4 too.
Returns the return type name of this method.
See also returnType() and QMetaType::type().
[since 5.0]
bool operator!=(const QMetaMethod &m1, const QMetaMethod &m2)This is an overloaded function.
Returns true
if method m1 is not equal to method m2, otherwise returns false
.
This function was introduced in Qt 5.0.
[since 5.0]
bool operator==(const QMetaMethod &m1, const QMetaMethod
&m2)This is an overloaded function.
Returns true
if method m1 is equal to method m2, otherwise returns false
.
This function was introduced in Qt 5.0.
Equals maximum number of arguments available for execution of the method via QMetaMethod::invoke()