QMetaMethod 類提供有關成員函數的元數據。 更多...
| 頭: |
#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 |
(從 6.5 起)
bool
|
invoke (QObject * obj , Args &&... arguments ) const |
(從 6.5 起)
bool
|
invoke (QObject * obj , QTemplatedMetaMethodReturnArgument<ReturnArg> ret , Args &&... arguments ) const |
(從 6.5 起)
bool
|
invoke (QObject * obj , Qt::ConnectionType type , Args &&... arguments ) const |
(從 6.5 起)
bool
|
invoke (QObject * obj , Qt::ConnectionType type , QTemplatedMetaMethodReturnArgument<ReturnArg> ret , Args &&... arguments ) const |
(從 6.5 起)
bool
|
invokeOnGadget (void * gadget , Args &&... arguments ) const |
(從 6.5 起)
bool
|
invokeOnGadget (void * gadget , QTemplatedMetaMethodReturnArgument<ReturnArg> ret , Args &&... arguments ) const |
(從 6.2 起)
bool
|
isConst () const |
| bool | isValid () const |
| int | methodIndex () const |
| QByteArray | methodSignature () const |
| QMetaMethod::MethodType | methodType () const |
| QByteArray | name () const |
| int | parameterCount () const |
(從 6.0 起)
QMetaType
|
parameterMetaType (int index ) const |
| QList<QByteArray> | parameterNames () const |
| int | parameterType (int index ) const |
(從 6.0 起)
QByteArray
|
parameterTypeName (int index ) const |
| QList<QByteArray> | parameterTypes () const |
(從 6.0 起)
int
|
relativeMethodIndex () const |
(從 6.0 起)
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 & lhs , const QMetaMethod & rhs ) |
| bool | operator== (const QMetaMethod & lhs , const QMetaMethod & rhs ) |
| Q_METAMETHOD_INVOKE_MAX_ARGS |
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
|
函數是構造函數。 |
[since 6.5]
template <typename... Args>
bool
QMetaMethod::
invoke
(
QObject
*
obj
,
Args
&&...
arguments
) const
[since 6.5]
template <typename ReturnArg, typename... Args>
bool
QMetaMethod::
invoke
(
QObject
*
obj
,
QTemplatedMetaMethodReturnArgument
<
ReturnArg
>
ret
,
Args
&&...
arguments
) const
[since 6.5]
template <typename... Args>
bool
QMetaMethod::
invoke
(
QObject
*
obj
,
Qt::ConnectionType
type
,
Args
&&...
arguments
) const
[since 6.5]
template <typename ReturnArg, typename... Args>
bool
QMetaMethod::
invoke
(
QObject
*
obj
,
Qt::ConnectionType
type
,
QTemplatedMetaMethodReturnArgument
<
ReturnArg
>
ret
,
Args
&&...
arguments
) const
Invokes this method on the object
object
。返迴
true
若成員可以被援引。返迴
false
若沒有這樣的成員或參數不匹配。
對於具有 QTemplatedMetaMethodReturnArgument 參數的重載,返迴值對於 member 函數調用被放置在 ret . For the overloads without such a member, the return value of the called function (if any) will be discarded. QTemplatedMetaMethodReturnArgument is an internal type you should not use directly. Instead, use the qReturnArg() function.
重載采用 Qt::ConnectionType type 參數允許明確選擇是否將同步援引:
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 copyable types, because Qt needs to copy the arguments to store them in an event behind the scenes. Since Qt 6.5, this function automatically registers the types being used; however, as a side-effect, it is not possible to make calls using types that are only forward-declared. Additionally, it is not possible to make asynchronous calls that use references to non-const-qualified types as parameters either.
要同步援引
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, qReturnArg(retVal), QString("sqrt"), 42, 9.7);
若 compute 槽不接受 1 個準確
QString
, one
int
,和 1 個
double
in the specified order, the call will fail. Note how it was necessary to be explicit about the type of the
QString
, as the character literal is not exactly the right type to match. If the method instead took a
QByteArray
,
qint64
,和
long double
, the call would need to be written as:
QString retVal; QByteArray normalizedSignature = QMetaObject::normalizedSignature("compute(QByteArray, qint64, long double)"); int methodIndex = obj->metaObject()->indexOfMethod(normalizedSignature); QMetaMethod method = obj->metaObject()->method(methodIndex); method.invoke(obj, Qt::DirectConnection, qReturnArg(retVal), QByteArray("sqrt"), qint64(42), 9.7L);
The same call can be executed using the Q_ARG () 和 Q_RETURN_ARG () macros, as in:
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));
警告: 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.
該函數在 Qt 6.5 引入。
另請參閱 Q_ARG (), Q_RETURN_ARG (), qRegisterMetaType (),和 QMetaObject::invokeMethod ().
[since 6.5]
template <typename... Args>
bool
QMetaMethod::
invokeOnGadget
(
void
*
gadget
,
Args
&&...
arguments
) const
[since 6.5]
template <typename ReturnArg, typename... Args>
bool
QMetaMethod::
invokeOnGadget
(
void
*
gadget
,
QTemplatedMetaMethodReturnArgument
<
ReturnArg
>
ret
,
Args
&&...
arguments
) const
Invokes this method on a
Q_GADGET
。返迴
true
若成員可以被援引。返迴
false
若沒有這樣的成員或參數不匹配。
指針 gadget must point to an instance of the gadget class.
援引始終是同步的。
For the overload with a QTemplatedMetaMethodReturnArgument parameter, the return value of the member 函數調用被放置在 ret . For the overload without it, the return value of the called function (if any) will be discarded. QTemplatedMetaMethodReturnArgument is an internal type you should not use directly. Instead, use the qReturnArg() function.
警告: 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.
該函數在 Qt 6.5 引入。
另請參閱 Q_ARG (), Q_RETURN_ARG (), qRegisterMetaType (),和 QMetaObject::invokeMethod ().
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]
template <typename PointerToMemberFunction>
QMetaMethod
QMetaMethod::
fromSignal
(
PointerToMemberFunction
signal
)
返迴的元方法相當於給定
signal
,或無效
QMetaMethod
if
signal
is
nullptr
or not a signal of the class.
範例:
QMetaMethod destroyedSignal = QMetaMethod::fromSignal(&QObject::destroyed);
[since 6.2]
bool
QMetaMethod::
isConst
() const
Returns whether the method is const qualified.
注意:
This method might erroneously return
false
for a const method if it belongs to a library compiled against an older version of Qt.
該函數在 Qt 6.2 引入。
返迴
true
if this method is valid (can be introspected and invoked), otherwise returns
false
.
返迴此方法的索引。
返迴此方法的簽名 (如
setValue(double)
).
另請參閱 parameterTypes () 和 parameterNames ().
返迴此方法 (信號、槽或方法) 的類型。
另請參閱 access ().
返迴此方法的名稱。
另請參閱 methodSignature () 和 parameterCount ().
返迴此方法的參數數。
另請參閱 parameterType () 和 parameterNames ().
[since 6.0]
QMetaType
QMetaMethod::
parameterMetaType
(
int
index
) const
Returns the metatype of the parameter at the given index .
若 index is smaller than zero or larger than parameterCount (), an invalid QMetaType 被返迴。
該函數在 Qt 6.0 引入。
另請參閱 parameterCount (), returnMetaType (),和 QMetaType .
返迴參數名稱的列錶。
另請參閱 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.
另請參閱 parameterCount (), parameterMetaType (), returnType (),和 QMetaType .
[since 6.0]
QByteArray
QMetaMethod::
parameterTypeName
(
int
index
) const
Returns the name of the type at position index If there is no parameter at index , returns an empty QByteArray
該函數在 Qt 6.0 引入。
另請參閱 parameterNames ().
Returns a list of parameter types.
另請參閱 parameterNames () 和 methodSignature ().
[since 6.0]
int
QMetaMethod::
relativeMethodIndex
() const
Returns this method's local index inside.
該函數在 Qt 6.0 引入。
[since 6.0]
QMetaType
QMetaMethod::
returnMetaType
() const
Returns the return type of this method.
該函數在 Qt 6.0 引入。
另請參閱 parameterMetaType (), QMetaType ,和 typeName ().
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.
另請參閱 parameterType (), QMetaType , typeName (),和 returnMetaType ().
Returns the method revision if one was specified by Q_REVISION , otherwise returns 0. Since Qt 6.0, non-zero values are encoded and can be decoded using QTypeRevision::fromEncodedVersion ().
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.
注意:
moc
expands preprocessor macros, so it is necessary to surround the definition with
#ifndef
Q_MOC_RUN
, as shown in the example above.
返迴此方法的返迴類型名稱。
另請參閱 returnType () 和 QMetaType::name ().
[noexcept]
bool
operator!=
(const
QMetaMethod
&
lhs
, const
QMetaMethod
&
rhs
)
這是重載函數。
返迴
true
若方法
lhs
不等於方法
rhs
,否則返迴
false
.
[noexcept]
bool
operator==
(const
QMetaMethod
&
lhs
, const
QMetaMethod
&
rhs
)
這是重載函數。
返迴
true
若方法
lhs
等於方法
rhs
,否則返迴
false
.
Equals maximum number of arguments available for execution of the method via QMetaMethod::invoke ()