The QMetaMethod 類提供有關成員函數的元數據。 更多...
| 頭: | #include <QMetaMethod> |
| qmake: | QT += core |
| 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( Q_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( Q_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 , Qt::ConnectionType connectionType , QGenericArgument val0 = QGenericArgument( Q_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 , QGenericArgument val0 = QGenericArgument( Q_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 , QGenericReturnArgument returnValue , QGenericArgument val0 = QGenericArgument( Q_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( Q_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 | isValid () const |
| int | methodIndex () const |
| QByteArray | methodSignature () const |
| MethodType | methodType () const |
| QByteArray | name () const |
| int | parameterCount () const |
| QList<QByteArray> | parameterNames () const |
| int | parameterType (int index ) const |
| QList<QByteArray> | parameterTypes () 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 ) |
| Q_METAMETHOD_INVOKE_MAX_ARGS |
The QMetaMethod 類提供有關成員函數的元數據。
A QMetaMethod 擁有 methodType (), methodSignature (), 列錶化的 parameterTypes () 和 parameterNames (), a return typeName (), tag (), and an access () specifier. You can use invoke () to invoke the method on an arbitrary QObject .
另請參閱 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 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.
另請參閱 methodType ().
[static]
QMetaMethod
QMetaMethod::
fromSignal
(
PointerToMemberFunction
signal
)
返迴的元方法相當於給定 signal ,或無效 QMetaMethod if signal 不是類的信號。
範例:
QMetaMethod destroyedSignal = QMetaMethod::fromSignal(&QObject::destroyed);
該函數在 Qt 5.0 引入。
Invokes this method on the object
object
。返迴
true
若成員可以被援引。返迴
false
若沒有這樣的成員或參數不匹配。
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)
槽在某些任意對象
obj
檢索其返迴值:
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.
若 compute 槽不接受 1 個準確 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 並忽略返迴值。
Invokes this method on a
Q_GADGET
。返迴
true
若成員可以被援引。返迴
false
若沒有這樣的成員或參數不匹配。
指針 gadget must point to an instance of the gadget class.
援引始終是同步的。
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 ,和 val9 ) to this method call.
警告: 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.
該函數在 Qt 5.5 引入。
另請參閱 Q_ARG (), Q_RETURN_ARG (), qRegisterMetaType (),和 QMetaObject::invokeMethod ().
這是重載函數。
這重載援引此方法為 gadget 並忽略返迴值。
該函數在 Qt 5.5 引入。
返迴
true
if this method is valid (can be introspected and invoked), otherwise returns
false
.
該函數在 Qt 5.0 引入。
返迴此方法的索引。
該函數在 Qt 4.6 引入。
返迴此方法的簽名 (如
setValue(double)
).
該函數在 Qt 5.0 引入。
另請參閱 parameterTypes () 和 parameterNames ().
返迴此方法 (信號、槽或方法) 的類型。
另請參閱 access ().
返迴此方法的名稱。
該函數在 Qt 5.0 引入。
另請參閱 methodSignature () 和 parameterCount ().
返迴此方法的參數數。
該函數在 Qt 5.0 引入。
另請參閱 parameterType () 和 parameterNames ().
返迴參數名稱的列錶。
另請參閱 parameterTypes () 和 methodSignature ().
Returns the type of the parameter at the given index .
The return value is one of the types that are registered with QMetaType ,或 QMetaType::UnknownType if the type is not registered.
該函數在 Qt 5.0 引入。
另請參閱 parameterCount (), returnType (),和 QMetaType .
Returns a list of parameter types.
另請參閱 parameterNames () 和 methodSignature ().
Returns the return type of this method.
The return value is one of the types that are registered with QMetaType ,或 QMetaType::UnknownType if the type is not registered.
該函數在 Qt 5.0 引入。
另請參閱 parameterType (), QMetaType ,和 typeName ().
Returns the method revision if one was specified by Q_REVISION , otherwise returns 0.
該函數在 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.
注意:
從 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.
返迴此方法的返迴類型名稱。
另請參閱 returnType () 和 QMetaType::type ().
這是重載函數。
返迴
true
若方法
m1
不等於方法
m2
,否則返迴
false
.
該函數在 Qt 5.0 引入。
這是重載函數。
返迴
true
若方法
m1
等於方法
m2
,否則返迴
false
.
該函數在 Qt 5.0 引入。
Equals maximum number of arguments available for execution of the method via QMetaMethod::invoke ()