QMetaMethod Class
QMetaMethod 클래스는 멤버 함수에 대한 메타데이터를 제공합니다. 더 보기...
| 헤더: | #include <QMetaMethod> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
| qmake: | QT += core |
QMetaMethod 비교
| 범주 | 비교 가능한 유형 |
|---|---|
| 동등성 | QMetaMethod |
공개 유형
| enum | Access { Private, Protected, Public } |
| enum | MethodType { Method, Signal, Slot, Constructor } |
공개 함수
| QMetaMethod::Access | access() const |
(since 6.5) bool | invoke(QObject *obj, Args &&... arguments) const |
(since 6.5) bool | invoke(QObject *obj, QTemplatedMetaMethodReturnArgument<ReturnArg> ret, Args &&... arguments) const |
(since 6.5) bool | invoke(QObject *obj, Qt::ConnectionType type, Args &&... arguments) const |
(since 6.5) bool | invoke(QObject *obj, Qt::ConnectionType type, QTemplatedMetaMethodReturnArgument<ReturnArg> ret, Args &&... arguments) const |
(since 6.5) bool | invokeOnGadget(void *gadget, Args &&... arguments) const |
(since 6.5) bool | invokeOnGadget(void *gadget, QTemplatedMetaMethodReturnArgument<ReturnArg> ret, Args &&... arguments) const |
(since 6.2) bool | isConst() const |
| bool | isValid() const |
| int | methodIndex() const |
| QByteArray | methodSignature() const |
| QMetaMethod::MethodType | methodType() const |
| QByteArray | name() const |
(since 6.9) QByteArrayView | nameView() const |
| int | parameterCount() const |
(since 6.0) QMetaType | parameterMetaType(int index) const |
| QList<QByteArray> | parameterNames() const |
| int | parameterType(int index) const |
(since 6.0) QByteArray | parameterTypeName(int index) const |
| QList<QByteArray> | parameterTypes() const |
(since 6.0) int | relativeMethodIndex() const |
(since 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) |
매크로
(until 6.13) | Q_METAMETHOD_INVOKE_MAX_ARGS |
상세 설명
QMetaMethod에는 methodType(), methodSignature(), parameterTypes() 및 parameterNames() 목록, 반환 typeName(), tag() 및 access() 지정자가 있습니다. invoke()을 사용하여 임의의 QObject 에서 메서드를 호출할 수 있습니다.
QMetaObject, QMetaEnum, QMetaProperty 및 Qt의 속성 시스템도 참조하십시오 .
멤버 유형 문서
enum QMetaMethod::Access
이 열거형은 C++에서 사용되는 규칙에 따라 메서드의 접근 권한 수준을 나타냅니다.
| 상수 | 값 |
|---|---|
QMetaMethod::Private | 0 |
QMetaMethod::Protected | 1 |
QMetaMethod::Public | 2 |
enum QMetaMethod::MethodType
| 상수 | 값 | 설명 |
|---|---|---|
QMetaMethod::Method | QMETHOD_CODE | 이 함수는 일반 멤버 함수입니다. |
QMetaMethod::Signal | 1 | 이 함수는 신호입니다. |
QMetaMethod::Slot | 2 | 이 함수는 슬롯입니다. |
QMetaMethod::Constructor | 3 | 이 함수는 생성자입니다. |
멤버 함수 문서
QMetaMethod::Access QMetaMethod::access() const
이 메서드의 접근 지정(private, protected 또는 public)을 반환합니다.
참고: 시그널은 항상 public이지만, 이는 구현 세부 사항으로 간주해야 합니다. 클래스 외부에서 시그널을 발산하는 것은 거의 항상 좋지 않은 방법입니다.
methodType()도 참조하십시오 .
[static] template <typename PointerToMemberFunction> QMetaMethod QMetaMethod::fromSignal(PointerToMemberFunction signal)
주어진 ` signal`에 해당하는 메타 메서드를 반환하거나, ` signal `가 ` nullptr `이거나 해당 클래스의 신호가 아닌 경우 유효하지 않은 ` QMetaMethod `을 반환합니다.
예시:
QMetaMethod destroyedSignal = QMetaMethod::fromSignal(&QObject::destroyed);[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
object 객체에 대해 이 메서드를 호출합니다. 멤버를 호출할 수 있는 경우 true 를 반환합니다. 해당 멤버가 없거나 매개변수가 일치하지 않는 경우 false 를 반환합니다.
QTemplatedMetaMethodReturnArgument 매개변수를 갖는 오버로드의 경우, member 함수 호출의 반환 값이 ret 에 저장됩니다. 해당 멤버가 없는 오버로드의 경우, 호출된 함수의 반환 값(있는 경우)은 무시됩니다. QTemplatedMetaMethodReturnArgument는 직접 사용해서는 안 되는 내부 유형입니다. 대신 qReturnArg() 함수를 사용하십시오.
Qt::ConnectionType type 매개변수를 갖는 오버로드는 호출이 동기식일지 여부를 명시적으로 선택할 수 있게 해줍니다:
- type 가 Qt::DirectConnection 인 경우, 해당 멤버는 현재 스레드에서 즉시 호출됩니다.
- type 가 Qt::QueuedConnection 인 경우, QEvent 가 전송되며, 애플리케이션이 obj 가 생성되었거나 이동된 스레드의 이벤트 루프에 진입하는 즉시 멤버가 호출됩니다.
- type 가 Qt::BlockingQueuedConnection 인 경우, Qt::QueuedConnection 와 동일한 방식으로 메서드가 호출되지만, 이벤트가 전달될 때까지 현재 스레드가 차단된다는 점이 다릅니다. 동일한 스레드 내의 객체 간 통신에 이 연결 유형을 사용하면 교착 상태가 발생합니다.
- type 가 Qt::AutoConnection 인 경우, obj 가 호출자와 동일한 스레드에 존재하면 멤버가 동기식으로 호출되며, 그렇지 않으면 비동기식으로 호출됩니다. 이는 type 매개변수가 없는 오버로드의 동작입니다.
QPushButton 의 animateClick() 슬롯을 비동기적으로 호출하려면:
int methodIndex = pushButton->metaObject()->indexOfMethod("animateClick()");
QMetaMethod method = metaObject->method(methodIndex);
method.invoke(pushButton, Qt::QueuedConnection);비동기 메서드 호출의 경우, Qt가 내부적으로 인수를 이벤트에 저장하기 위해 복사해야 하므로 매개변수는 복사 가능한 유형이어야 합니다. Qt 6.5부터는 이 함수가 사용되는 타입을 자동으로 등록하지만, 부작용으로 인해 선선언된 타입만 사용하여 호출하는 것은 불가능합니다. 또한, const로 지정되지 않은 타입에 대한 참조를 매개변수로 사용하는 비동기 호출도 불가능합니다.
임의의 객체 obj 에서 ` compute(QString, int, double) ` 슬롯을 동기적으로 호출하고 반환 값을 가져오려면:
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" 슬롯이 지정된 순서대로 정확히 하나의 ` QString`, 하나의 ` int`, 그리고 하나의 ` double `을 인수로 받지 않는다면 호출은 실패합니다. 문자 리터럴은 정확히 일치하는 유형이 아니므로, QString 의 유형을 명시적으로 지정해야 했다는 점에 유의하십시오. 만약 메서드가 대신 QByteArray, qint64, long double 를 받는다면, 호출은 다음과 같이 작성되어야 합니다:
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);Q_ARG() 및 Q_RETURN_ARG() 매크로를 사용하여 다음과 같이 동일한 호출을 실행할 수 있습니다:
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));경고: 이 방법은 인수의 유효성을 검사하지 않습니다. object 은 이 QMetaMethod 가 생성된 QMetaObject 의 클래스 인스턴스여야 합니다.
이 함수들은 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
Q_GADGET 에 대해 이 메서드를 호출합니다. 멤버를 호출할 수 있는 경우 true 를 반환합니다. 해당 멤버가 없거나 매개변수가 일치하지 않는 경우 false 를 반환합니다.
gadget 포인터는 가젯 클래스의 인스턴스를 가리켜야 합니다.
호출은 항상 동기식으로 수행됩니다.
QTemplatedMetaMethodReturnArgument 매개변수가 있는 오버로드의 경우, member 함수 호출의 반환 값이 ret 에 저장됩니다. 이 매개변수가 없는 오버로드의 경우, 호출된 함수의 반환 값(있는 경우)은 무시됩니다. QTemplatedMetaMethodReturnArgument는 직접 사용해서는 안 되는 내부 유형입니다. 대신 qReturnArg() 함수를 사용하십시오.
경고: 이 메서드는 인자의 유효성을 검사하지 않습니다. 즉, ` gadget `는 이 ` QMetaMethod `가 생성될 때 사용된 ` QMetaObject `의 클래스 인스턴스여야 합니다.
이 함수들은 Qt 6.5에서 도입되었습니다.
Q_ARG(), Q_RETURN_ARG(), qRegisterMetaType(), QMetaObject::invokeMethod()도 참조하십시오 .
[since 6.2] bool QMetaMethod::isConst() const
해당 메서드가 const로 선언되었는지 여부를 반환합니다.
참고: 이 메서드는 const 메서드가 이전 버전의 Qt로 컴파일된 라이브러리에 속해 있는 경우, 잘못된 결과로 ` false `를 반환할 수 있습니다.
이 함수는 Qt 6.2에서 도입되었습니다.
bool QMetaMethod::isValid() const
이 메서드가 유효한 경우(인트로스펙션 및 호출이 가능한 경우) ` true `를 반환하고, 그렇지 않은 경우 ` false`를 반환합니다.
int QMetaMethod::methodIndex() const
이 메서드의 인덱스를 반환합니다.
QByteArray QMetaMethod::methodSignature() const
이 메서드의 시그니처를 반환합니다(예: setValue(double)).
parameterTypes() 및 parameterNames()도 참조하십시오 .
QMetaMethod::MethodType QMetaMethod::methodType() const
이 메서드의 유형(신호, 슬롯 또는 메서드)을 반환합니다.
access()도 참조하십시오 .
QByteArray QMetaMethod::name() const
이 메서드의 이름을 반환합니다.
methodSignature() 및 parameterCount()도 참조하십시오 .
[since 6.9] QByteArrayView QMetaMethod::nameView() const
이 메서드의 이름을 반환합니다. 반환된 ` QByteArrayView `는 해당 메서드가 속한 클래스의 메타 객체가 유효한 동안 유효합니다.
이 함수는 Qt 6.9에서 도입되었습니다.
name도 참조하십시오 .
int QMetaMethod::parameterCount() const
이 메서드의 매개변수 개수를 반환합니다.
parameterType() 및 parameterNames()도 참조하십시오 .
[since 6.0] QMetaType QMetaMethod::parameterMetaType(int index) const
지정된 index 에 있는 매개변수의 메타타입을 반환합니다.
index 가 0보다 작거나 parameterCount()보다 큰 경우, 유효하지 않은 QMetaType 가 반환됩니다.
이 함수는 Qt 6.0에서 도입되었습니다.
parameterCount(), returnMetaType() 및 QMetaType도 참조하십시오 .
QList<QByteArray> QMetaMethod::parameterNames() const
매개변수 이름 목록을 반환합니다.
parameterTypes() 및 methodSignature()도 참조하십시오 .
int QMetaMethod::parameterType(int index) const
지정된 index 에 있는 매개변수의 유형을 반환합니다.
반환 값은 QMetaType 에 등록된 유형 중 하나이거나, 해당 유형이 등록되지 않은 경우 QMetaType::UnknownType 입니다.
parameterCount(), parameterMetaType(), returnType(), QMetaType도 참조하십시오 .
[since 6.0] QByteArray QMetaMethod::parameterTypeName(int index) const
index 위치에 있는 타입의 이름을 반환합니다. index 위치에 매개변수가 없는 경우, 빈 값을 반환합니다. QByteArray
이 함수는 Qt 6.0에서 도입되었습니다.
parameterNames()도 참조하십시오 .
QList<QByteArray> QMetaMethod::parameterTypes() const
매개변수 유형의 목록을 반환합니다.
parameterNames() 및 methodSignature()도 참조하십시오 .
[since 6.0] int QMetaMethod::relativeMethodIndex() const
이 메서드 내부의 로컬 인덱스를 반환합니다.
이 함수는 Qt 6.0에서 도입되었습니다.
[since 6.0] QMetaType QMetaMethod::returnMetaType() const
이 메서드의 반환 유형을 반환합니다.
이 함수는 Qt 6.0에서 도입되었습니다.
parameterMetaType(), QMetaType 및 typeName()도 참조하십시오 .
int QMetaMethod::returnType() const
이 메서드의 반환 유형을 반환합니다.
반환 값은 QMetaType 에 등록된 유형 중 하나이거나, 유형이 등록되지 않은 경우 QMetaType::UnknownType 에 반환됩니다.
parameterType(), QMetaType, typeName() 및 returnMetaType()도 참조하십시오 .
int QMetaMethod::revision() const
Q_REVISION 에서 메서드 리비전이 지정된 경우 해당 리비전을 반환하고, 그렇지 않은 경우 0을 반환합니다. Qt 6.0부터는 0이 아닌 값이 인코딩되며, QTypeRevision::fromEncodedVersion()를 사용하여 디코딩할 수 있습니다.
const char *QMetaMethod::tag() const
이 메서드와 연결된 태그를 반환합니다.
태그는 moc 에서 인식하는 특수 매크로로, 메서드에 대한 추가 정보를 추가할 수 있게 해줍니다.
태그 정보는 함수 선언에서 다음과 같은 방식으로 추가할 수 있습니다:
// 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();그리고 이 정보는 다음을 사용하여 확인할 수 있습니다:
MainWindow win;
win.show();
int functionIndex = win.metaObject()->indexOfSlot("testFunc()");
QMetaMethod mm = win.metaObject()->method(functionIndex);
qDebug() << mm.tag(); // prints MY_CUSTOM_TAG현재로서는 moc 가 모든 태그를 추출하고 기록하지만, 그 어떤 태그도 특별히 처리하지는 않습니다. 태그를 사용하여 메서드에 각기 다른 주석을 달고, 애플리케이션의 구체적인 요구 사항에 따라 처리할 수 있습니다.
참고: moc 는 전처리기 매크로를 확장하므로, 위 예제에서 볼 수 있듯이 정의 부분을 #ifndef Q_MOC_RUN 로 감싸야 합니다.
const char *QMetaMethod::typeName() const
이 메서드의 반환형 이름을 반환합니다. 이 메서드가 생성자인 경우, 이 함수는 빈 문자열을 반환합니다(생성자에는 반환형이 없습니다).
참고: Qt7에서는 이 함수가 생성자에 대해 null 포인터를 반환합니다.
returnType() 및 QMetaType::name()도 참조하십시오 .
관련 비회원 함수
[noexcept] bool operator!=(const QMetaMethod &lhs, const QMetaMethod &rhs)
lhs 메서드가 rhs 메서드와 같지 않은 경우 true 을 반환하고, 그렇지 않은 경우 false 을 반환합니다.
[noexcept] bool operator==(const QMetaMethod &lhs, const QMetaMethod &rhs)
lhs 메서드와 rhs 메서드가 동일할 경우 true 를 반환하고, 그렇지 않은 경우 false 를 반환합니다.
매크로 설명서
[until 6.13] Q_METAMETHOD_INVOKE_MAX_ARGS
이 매크로는 버전 6.13에서 사용 중단될 예정입니다.
이는 QMetaMethod::invoke()의 기존 구현을 통해 메서드를 실행할 때 사용할 수 있는 인수의 최대 개수와 같습니다. 새로운 구현(Qt 6.5부터)은 가변 개수 인수를 사용하므로 상한이 없습니다.
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.