QMetaMethod Class

QMetaMethod 클래스는 멤버 함수에 대한 메타 데이터를 제공합니다. 더 보기...

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
(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)

매크로

상세 설명

QMetaMethod에는 methodType(), methodSignature(), parameterTypes() 및 parameterNames() 목록, 반환 typeName(), tag() 및 access() 지정자가 있습니다. invoke ()를 사용하여 임의의 QObject 에서 메서드를 호출할 수 있습니다.

QMetaObject, QMetaEnum, QMetaPropertyQt의 프로퍼티 시스템도참조하십시오 .

멤버 형 문서

enum QMetaMethod::Access

이 열거형은 C++에서 사용되는 규칙에 따라 메서드의 액세스 수준을 설명합니다.

ConstantValue
QMetaMethod::Private0
QMetaMethod::Protected1
QMetaMethod::Public2

enum QMetaMethod::MethodType

상수설명
QMetaMethod::Method0함수는 일반 멤버 함수입니다.
QMetaMethod::Signal1함수는 신호입니다.
QMetaMethod::Slot2함수는 슬롯입니다.
QMetaMethod::Constructor3함수는 생성자입니다.

멤버 함수 문서

[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 매개변수가 있는 오버로드를 사용하면 호출이 동기식인지 아닌지를 명시적으로 선택할 수 있습니다:

  • typeQt::DirectConnection 인 경우 현재 스레드에서 멤버가 즉시 호출됩니다.
  • typeQt::QueuedConnection 인 경우 QEvent 가 전송되고 애플리케이션이 obj 이 생성되었거나 이동된 스레드의 이벤트 루프에 진입하자마자 멤버가 호출됩니다.
  • typeQt::BlockingQueuedConnection 인 경우 메서드는 Qt::QueuedConnection 와 동일한 방식으로 호출되지만 이벤트가 전달될 때까지 현재 스레드가 차단된다는 점을 제외하면 호출됩니다. 이 연결 유형을 사용하여 동일한 스레드에 있는 객체 간에 통신하면 교착 상태가 발생할 수 있습니다.
  • typeQt::AutoConnection 인 경우 obj 가 호출자와 같은 스레드에 있으면 멤버가 동기적으로 호출되고, 그렇지 않으면 비동기적으로 호출됩니다. 이는 type 매개 변수가 없는 오버로드의 동작입니다.

animateClick() 슬롯을 비동기적으로 호출하려면 QPushButton:

int methodIndex = pushButton->metaObject()->indexOfMethod("animateClick()");
QMetaMethod method = metaObject->method(methodIndex);
method.invoke(pushButton, Qt::QueuedConnection);

비동기 메서드 호출에서 매개변수는 복사 가능한 유형이어야 하는데, 그 이유는 Qt가 인수를 복사하여 이벤트에 저장해야 하기 때문입니다. Qt 6.5부터 이 함수는 사용 중인 형을 자동으로 등록하지만, 부작용으로 포워드 선언만 된 형을 사용하여 호출할 수 없습니다. 또한 정규화되지 않은 유형에 대한 참조를 매개변수로 사용하는 비동기 호출도 불가능합니다.

임의의 객체 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);

"계산" 슬롯이 지정된 순서대로 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));

경고: 이 메서드는 인자의 유효성을 테스트하지 않습니다: objectQMetaMethod 이 구성된 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() 함수를 사용하세요.

경고: 이 메서드는 인수의 유효성을 테스트하지 않습니다: gadgetQMetaObjectQMetaMethod 로 구성된 클래스의 인스턴스여야 합니다.

이 함수는 Qt 6.5에 도입되었습니다.

Q_ARG(), Q_RETURN_ARG(), qRegisterMetaType() 및 QMetaObject::invokeMethod()도 참조하십시오 .

QMetaMethod::Access QMetaMethod::access() const

이 메서드의 액세스 사양(비공개, 보호 또는 공개)을 반환합니다.

참고: 시그널은 항상 공개되지만, 이는 구현 세부 사항으로 간주해야 합니다. 클래스 외부에서 신호를 방출하는 것은 거의 항상 나쁜 생각입니다.

methodType()도 참조하세요 .

[static] template <typename PointerToMemberFunction> QMetaMethod QMetaMethod::fromSignal(PointerToMemberFunction signal)

주어진 signal 에 해당하는 메타 메서드를 반환하거나 signalnullptr 이거나 클래스의 신호가 아닌 경우 유효하지 않은 QMetaMethod 을 반환합니다.

예시:

QMetaMethod destroyedSignal = QMetaMethod::fromSignal(&QObject::destroyed);

[since 6.2] bool QMetaMethod::isConst() const

메서드가 const 자격이 있는지 여부를 반환합니다.

참고: 이 메서드는 이전 버전의 Qt에 대해 컴파일된 라이브러리에 속하는 경우, const 메서드에 대해 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

이 메서드의 반환 유형 이름을 반환합니다. 이 메서드가 생성자인 경우 이 함수는 빈 문자열을 반환합니다(생성자에는 반환 유형이 없음).

참고: Qt 7에서 이 함수는 생성자에 대해 널 포인터를 반환합니다.

returnType() 및 QMetaType::type()도 참조하세요 .

관련 비회원

[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 를 반환합니다.

매크로 문서

Q_METAMETHOD_INVOKE_MAX_ARGS

QMetaMethod::invoke()를 통해 메서드 실행에 사용할 수 있는 최대 인자 수와 같습니다.

© 2025 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.