The QMetaMethod 类提供有关成员函数的元数据。 更多...
| 头: | #include <QMetaMethod> |
| enum | Access { Private, Protected, Public } |
| enum | MethodType { Method, Signal, Slot, Constructor } |
| Access | access () const |
| bool | invoke (QObject * object , Qt::ConnectionType connectionType , 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 , 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 |
| int | methodIndex () const |
| MethodType | methodType () const |
| QList<QByteArray> | parameterNames () const |
| QList<QByteArray> | parameterTypes () const |
| const char * | signature () const |
| const char * | tag () const |
| const char * | typeName () const |
The QMetaMethod 类提供有关成员函数的元数据。
A QMetaMethod 拥有 methodType (), signature (), 列表化的 parameterTypes () 和 parameterNames (), a return typeName (), tag (), and an access () specifier. You can use invoke () to invoke the method on an arbitrary QObject .
A method will only be registered with the meta-object system if it is a slot, a signal, or declared with the Q_INVOKABLE macro. Constructors can also be registered with Q_INVOKABLE .
另请参阅 QMetaObject , QMetaEnum , QMetaProperty ,和 Qt 的特性系统 .
This enum describes the access level of a method, following the conventions used in C++.
| 常量 | 值 |
|---|---|
QMetaMethod::Private
|
0
|
QMetaMethod::Protected
|
1
|
QMetaMethod::Public
|
2
|
| 常量 | 值 | 描述 |
|---|---|---|
QMetaMethod::Method
|
0
|
函数是纯成员函数。 |
QMetaMethod::Signal
|
1
|
函数是信号。 |
QMetaMethod::Slot
|
2
|
函数是槽。 |
QMetaMethod::Constructor
|
3
|
函数是构造函数。 |
Returns the access specification of this method (private, protected, or public).
Signals are always protected, meaning that you can only emit them from the class or from a subclass.
另请参阅 methodType ().
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 ,和 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 () 和 Q_RETURN_ARG () 宏。 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().
要同步援引
compute(QString, int, double)
slot on some arbitrary object
obj
检索其返回值:
QString retVal; QByteArray normalizedSignature = QMetaObject::normalizedSignature("compute(QString, int, double)"); int methodIndex = obj->metaObject()->indexOfMethod(normalizedSignature); QMetaMethod method = 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.
若 compute 槽不接受恰好一个 QString , one int and one double in the specified order, the call will fail.
警告: 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.
另请参阅 Q_ARG (), Q_RETURN_ARG (), qRegisterMetaType (),和 QMetaObject::invokeMethod ().
此函数重载 invoke ().
This overload always invokes this method using the connection type Qt::AutoConnection .
此函数重载 invoke ().
此重载可以被使用,若对成员的返回值不感兴趣。
此函数重载 invoke ().
This overload invokes this method using the connection type Qt::AutoConnection 并忽略返回值。
返回此方法的索引。
该函数在 Qt 4.6 引入。
返回此方法 (信号、槽或方法) 的类型。
另请参阅 access ().
返回参数名称的列表。
另请参阅 parameterTypes () 和 signature ().
Returns a list of parameter types.
另请参阅 parameterNames () 和 signature ().
返回此方法的签名 (如
setValue(double)
).
另请参阅 parameterTypes () 和 parameterNames ().
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:
#define THISISTESTTAG // tag text ... private slots: THISISTESTTAG void testFunc();
and the information can be accessed by using:
MainWindow win; win.show(); int functionIndex = win.metaObject()->indexOfSlot("testFunc()"); QMetaMethod mm = metaObject()->method(functionIndex); qDebug() << mm.tag(); // prints THISISTESTTAG
For the moment,
moc
doesn't support any special tags.
Returns the return type of this method, or an empty string if the return type is void .